answersLogoWhite

0


Best Answer

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.

User Avatar

Wiki User

9y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

10y ago
Template Functions

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 Classes

The 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.

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

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.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

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.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

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.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

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.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

* case sensitive

* not suitable for large amount of codings

*

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

advantages and disadvantages of constructer

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Compared with what? There are no merits or demerits. Construction is the only way to initialise an instance of a class.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the advantage and disadvantage of constructor in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

True or False A C plus plus class constructor cannot return a function value?

True - A C++ constructor cannot return a value.


What is the difference between implicit and explicit call of constructor in c plus plus?

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.


What is the difference between constructor and friend function in c plus plus?

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.


How do you invoke the constructor function in c plus plus?

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.


What is the use of learning C?

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.

Related questions

Can you write own constructor in c plus plus?

Yes.


True or False A C plus plus class constructor cannot return a function value?

True - A C++ constructor cannot return a value.


What is the difference between implicit and explicit call of constructor in c plus plus?

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.


Can constructor be declared as constant in c plus plus?

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.


What is the difference between constructor and friend function in c plus plus?

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.


How can a constructor be invoked at the time of inheritance in C Plus Plus?

It cannot. Inheritance is a compile-time operation. Constructors are invoked at runtime at the point of instantiation.


How do you invoke the constructor function in c plus plus?

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.


Javas major disadvantage over c and c plus plus?

In C it's easier to work with hardware directly. Also C programs are usually more efficient.


Constructor and destructor invocation in c?

Not possible in C.


What do you mean by initialisation of objects in c plus plus?

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.


What is the use of learning C?

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.


What is the advantage and disadvantage of c sharp?

It's flexible and up to date, but usually it has to be executed on virtual machine (.NET).