answersLogoWhite

0


Best Answer

class base

{

base():m_data(0){}

base(int i):m_data(i){}

base(const base& b):m_data(b.m_data){}

private:

int m_data;

};

class derived : public base

{

derived():base(){} // call base class default constructor

derived(int i):base(i){} // call base class overloaded constructor

derived(const derived& d):base(d){} // call base class copy constructor

};

User Avatar

Wiki User

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

Wiki User

10y ago

The only "feature" of a derived class constructor is that it can, optionally, call any specific base class constructor via the derived class constructor initialisation list, as shown in the following example:

class base

{

public: // or protected constructors

base():m_data(0){}

base(const base& rhs):m_data(rhs.m_data){}

base(const int i):m_data(i){}

};

class derived

{

public:

derived():base(){} // calls the base class default constructor

derived(const derived& rhs):base(rhs){} // calls the base class copy constructor

derived(const int i):base(i){} // calls the base class overloaded constructor

};

Note that the initialisation list of a constructor is the most efficient method of initialising member variables. If you choose to initialise members from the body of the constructor, you are effectively initialising the members twice, once via the implicit initialisation list, and then again via explicit initialisation in the body of the constructor. By using explicit initialisation, you only initialise once, at the point of instantiation.

By explicitly invoking base class constructors via the initialisation list, you achieve the same efficiency with base class construction. If you do not specify a base class constructor, the default constructor is executed implicitly, which may not be the most efficient method of initialising the base class, especially if you then have to call other base class methods in order to initialise it properly, before initialising your derived class.

There will be times when the initialisation list is not sufficient to initialise your classes properly, but you should always try to do as much initialisation as you possibly can via the initialisation list, to ensure the most efficient construction possible.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

Any class that has one or more pure-virtual functions is an abstract class.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Features of constructor of a derived class in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is the method of constructor overloading in c plus plus?

Constructor overloading, just like any function's overloading, is where more than one configuration of parameters exists for the function. Based on the number and type of the parameters, different versions of the function can be resolved by the linker. This is typically used in the constructor as the default constructor (no parameters), the copy constructor (one reference parameter of the same type as the class), and the conversion constructor (any other combination of parameters).


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

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


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 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 use of private constructor in c plus plus?

Private construction prevents objects from the class from being instantiated other than via a static member function of the class, a friend function or a friend class.

Related questions

What is the method of constructor overloading in c plus plus?

Constructor overloading, just like any function's overloading, is where more than one configuration of parameters exists for the function. Based on the number and type of the parameters, different versions of the function can be resolved by the linker. This is typically used in the constructor as the default constructor (no parameters), the copy constructor (one reference parameter of the same type as the class), and the conversion constructor (any other combination of parameters).


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

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


Can a c plus plus class be derived from a Java class?

No.


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.


Is it mandatory to use the construtors in a class in c plus plus?

No. If you do not provide a default constructor, the compiler will provide a default constructor that simply allocates memory for the class, but it will not initialize the members of the class. If you do not provide a copy constructor, then the compiler will provide a copy constructor that allocates memory for the class, and then copies the member's data from class to class. This is bad if the class contains pointers, because only the pointer will be copied - the objects to which the pointers point will not be copied - and you could wind up deleting an object and then using it after deletion, with potentially devastating consequences. So, yes, it is mandatory, from a good practices point of view, and just plain mandatory when the class has pointers, to always provide a default constructor and a copy constructor, along with the appropriate destructor.


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 use of private constructor in c plus plus?

Private construction prevents objects from the class from being instantiated other than via a static member function of the class, a friend function or a friend class.


What are the properties of class in c plus plus?

A constructor is not a function. A function is a type, as specified by its return type, and must return a value of that type unless the type is void. A constructor does not return anything, not even void. The purpose of a constructor is to both allocate and initialise memory for an object of the type being constructed. If a valid object cannot be constructed for any reason, the constructor must throw an exception. If the object's class has no data members (attributes), the class does not require a constructor. This is typically the case for most abstract data types and base classes which are used purely as interfaces. Constructors differ from functions in that all constructors have an initialisation section that is used specifically to initialise non-static data members. The body of the constructor is rarely used except to perform initialisations that cannot be more easily performed by the initialisation section. A class may have more than one constructor to provide alternative methods of construction based upon the number and type of arguments supplied (if any). When no arguments are required or all arguments have default values then the constructor is known as the default constructor. If the constructor has only one argument the constructor is known as a conversion constructor (because the argument is converted to an object of the class). However, if the constructor argument is a constant reference to an object of the same class, then it is known as a copy constructor, and when the constructor argument is an rvalue reference, it is known as a move constructor. If copy and/or move constructors are provided for a class, the equivalent assignment operators should also be provided for that class. All other constructors are known as user-defined constructors.


Can you declare constructor as a private in c plus plus?

Yes. However, like any other private member, a private constructor can only be accessed by the class itself (typically via a static member function) or by one of its friends. There are very few cases where private constructors are appropriate, one of the most common being the need to suppress the compiler-generated copy construction of a base class. However, since C++11, suppressed constructors can simply be deleted, thus making error messages much more meaningful to users of your class. For example, instead of the following: class A { public: A (); // default constructor private: A (const A&); // suppress copy constructor (can still be invoked by the class and its friends) // ... }; You'd now use the following: class A { public: A (); // default constructor A (const A&) =delete; // suppress copy constructor (cannot be invoked at all) // ... };


In C plus plus What function is called to initialize a class?

Class initialisation is normally handled by the class constructor(s). Every constructor has an optional initialisation section between the declaration and the body of the constructor. This is generally used to call specific base class constructors, but can be used to initialise any member variables via their own constructors. Member variables may alternatively be initialised in the body of the constructor, but this is really only necessary when member pointers need to be allocated new memory. For those classes that have many members and many constructors, the initialisation may be handled by a private member method called by each constructor in order to simplify maintenance during development. However, when the class is finalised, the private member method will generally be replaced with formal initialisation sections in each constructor.


Write a program in c plus plus with constructor?

// constructor program to add two number's // program written by SuNiL kUmAr #include<iostream.h> #include<conio.h> class constructor { private: int a,b; public: constructor(int m,int n); int sum(); }; constructor::constructor(int m,int n) { a=m; b=n; } int constructor::sum() { int s; s=a+b; return (s); } int main() { int x,y; clrscr(); cout<<"enter two number's to add \n"; cin>>x>>y; class constructor k (x,y); cout<<"sum of two number's is = "<<k.sum(); getch(); return (0); }


Definition of class in c plus plus?

A constructor is a function defined in a class with the name same as the class name and is used to automatically initialized the class data members. It is never preceded by a return type not even void. eg - class abc { int a, b; abc() //constructor { a=0; b=0; } }; main() { abc a; } The time we create object of the class in the main function the data members of the class are automatically initialised. Constructor can even take parameter. Those constructor are known as parameterised constructor.