Objects allow you to establish invariants and limit the type of operations that can be performed upon the internal representation of the object, exposing only as much as is needed to use the object -- no more and no less. This greatly reduces the chances of the programmer making an error, creating more robust code more easily and with fewer runtime checks and much greater efficiency.
There aren't any demerits to a template function compared to the many demerits of a macro function. The only real pitfall is that types are not automatically promoted as they are with a macro function, however that is actually a good thing because macros cannot enlist the help of the compiler to assure type safety. Take the following example:
#define MAX(a,b) (a)>(b)?(a):(b)
While the above macro can automatically cater for any mixture of types, (int, int), (int, char), (float, double), etc, the problem is that with mixed types there's no guarantee the return type will be suitable for the caller. Even if the compiler picks up a problem, it cannot help you identify the problem because the macro no longer exists by the time the compiler spots the error, and it can't highlight the error because it's looking at the precompiled code rather than the code you actually wrote. Remember that the compiler exists to not only build your programs, but to identify silly little bugs like this. With macro functions you lose this most basic functionality, thus the onus shifts to the programmer to ensure such bugs do not arise.
With function templates you automatically enlist the help of the compiler thus ensuring your function calls are 100% type safe. And if you should mix your types inappropriately, at least the compiler can identify the problem allowing you to cast your types more appropriately.
Template ClassesThe only real demerit to a template class is that the class implementation (both the declaration and its definition) must be completely visible to the compiler before the the template is first used. All this really means is that all the code must be placed within the header file rather than separated between header and source files as would be expected of a non-template class. There are ways around this (such as including the source file in the header), however it's hardly a major problem when it's an expected feature of the language.
Binary files allow you to read and write any type of data, including text data, both in ASCII and UNICODE formats. Although all data can be converted to and from an ASCII character array, and could therefore be read and written from a plain-text file, the conversion process alone would make reading and writing mixed data a highly inefficient process. With binary files, no conversion is necessary -- you simply read the data directly into a data buffer of the required type (the type alone determines how many bytes to read or write). This makes it possible to serialise objects simply by reading and writing data members to and from a binary file.
The only merit to an inline function is that you retain the function in your source code (making your code easier to read and manage), whilst the compiler eliminates the expense of an actual function call by inline expanding your function at each call site. The demerit of inline expansion is that it can increase code size to the point that any benefits gained by removing the function calls are outweighed by the reduced performance of the larger code size. As a result, the compiler is free to veto any inline expansion when there is no benefit to be gained in doing so. Declaring a function for inline expansion is merely a hint to the compiler, nothing more.
The merit of a friend function is that it allows an external non-member function to gain private access to the class in which it is declared a friend, thus extending the interface of the class. The demerit of a friend is when it is used inappropriately, which has the potential to undermine encapsulation.
Constructors exist purely to initialise member variables. Although you can use the constructor body to call member methods in order to perform that initialisation, this is highly inefficient, particularly when the member variable is a complex type (non-primitive). Constructors have initialisation lists specifically for the purpose of initialising member variables at the point of instantiation, which is far more efficient than initialising via the constructor body, which would result in complex types being initialised twice (once during implicit initialisation and then again during explicit initialisation in the constructor body).
However, if you specify two or more constructors, each has to have its own specific initialisation list, even if those lists are largely the same for all constructors, which not only results in code duplication but also increases code maintenance. When you add a new member, you must update the initialisation lists of all constructors. Thus the advantage of constructors is increased efficiency, while the disadvantage is increased maintenance. A lot of young programmers often counter this by creating a common initialisation member function which is then called from each constructor's body. While this reduces code maintenance, it increases the inefficiency. There's nothing wrong with having a common initialisation function, but you shouldn't call it from the constructor.
Initialisation lists also allow derived classes to invoke specific base class constructors. When not specified, a derived class constructor will automatically call the base class' default constructor, which may not be the most efficient method of construction. This is particularly important when defining a copy constructor. In this case you will want to call the base class copy constructor, not the base class default constructor, in order to maintain efficiency.
It should be noted that when you do not declare any constructors, the compiler will generate both a default constructor and a copy constructor, both of which will call the appropriate base class constructors if the class is derived. If you define any constructor, you will automatically lose the compiler-generated default constructor and must define your own if you require one. The copy constructor is always generated for you unless you explicitly declare your own, but you must then call the base class copy constructor explicitly, otherwise the base class default constructor is called implicitly.
* case sensitive
* not suitable for large amount of codings
*
advantages and disadvantages of constructer
Compared with what? There are no merits or demerits. Construction is the only way to initialise an instance of a class.
True - A C++ constructor cannot return a value.
An implicit constructor call will always call the default constructor, whereas explicit constructor calls allow to chose the best constructor and passing of arguments into the constructor.
A constructor is a method that fires when the object is instantiated. A friend function is a function that has special access to the object. They are two different types of things, and cannot be further differenced.
There is no such thing as a constructor function in C++ (constructors have no return value, not even void, and cannot be called like regular functions). Constructors are invoked rather than called directly, either by declaring a static variable of the class type, or via the C++ new operator.
C and C++ will help you understand the inner workings of a computer with operations such as dealing with memory and pointers. It will do anything that you tell it to do, which is both an advantage and a disadvantage.
Yes.
True - A C++ constructor cannot return a value.
An implicit constructor call will always call the default constructor, whereas explicit constructor calls allow to chose the best constructor and passing of arguments into the constructor.
No. Constructors initialise objects and, by definition, must be able to modify the member variables. Uninitialised members are a disaster waiting to happen even without a constructor declared const! Thankfully, the compiler won't permit a const constructor.
A constructor is a method that fires when the object is instantiated. A friend function is a function that has special access to the object. They are two different types of things, and cannot be further differenced.
It cannot. Inheritance is a compile-time operation. Constructors are invoked at runtime at the point of instantiation.
There is no such thing as a constructor function in C++ (constructors have no return value, not even void, and cannot be called like regular functions). Constructors are invoked rather than called directly, either by declaring a static variable of the class type, or via the C++ new operator.
In C it's easier to work with hardware directly. Also C programs are usually more efficient.
Not possible in C.
Initialization of objects means to provide an initial value for the object. This is usually done by the constructor, or it can be done with an assignment statement.
C and C++ will help you understand the inner workings of a computer with operations such as dealing with memory and pointers. It will do anything that you tell it to do, which is both an advantage and a disadvantage.
It's flexible and up to date, but usually it has to be executed on virtual machine (.NET).