Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Constructor Calling and Inheritance in C++, Assignments of Computer Science

The rules for constructor calling in inheritance in c++, including the default constructor of the base class, calling a parametrized constructor of the base class, private members of the base class in the derived class constructor, copy constructor by reference, private constructor, and virtual base class. Examples are provided for each concept.

What you will learn

  • How is the default constructor of the base class called when creating an object of the derived class?
  • What is the role of a virtual base class in multiple-path inheritance?
  • What are the rules for constructor calling in inheritance in C++?

Typology: Assignments

2020/2021

Uploaded on 05/22/2021

anivartak-jain
anivartak-jain 🇮🇳

2 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Constructor in Inheritance
Default constructor of Base class
Default Constructor of Base class will be called when object of Derived class
is created.
If Base class is having nay user-defined constructor then it must also have
user-defined default constructor
Calling Parametrised constructor of Base class
It should be called from constructor of Derived class
Example:
class Cuboid : public Rectangle
{private :
int height;
public:
Cuboid(int lint b, int h); // Base class constructor should not be called from prototype
};
Cuboid::Cuboid(int lint b, int h) : Rectangle(l , b) // calling base class constructor
{height=h;
}
Private member of Base in Derived class Constructor
No. Private members of Base class are not accessible in derived class.
Private members of Base class are inherited to Derived class, but not
accessible.
Copy Constructor by reference
Copy constructor must take parameter by reference. If it is taking parameter
by value then it has to create an object and it will class constructor again.
It may become recursive call to the constructor. Constructor calling
constructor.
Private Constructor
pf2

Partial preview of the text

Download Constructor Calling and Inheritance in C++ and more Assignments Computer Science in PDF only on Docsity!

Constructor in Inheritance

Default constructor of Base class

• Default Constructor of Base class will be called when object of Derived class

is created.

• If Base class is having nay user-defined constructor then it must also have

user-defined default constructor

Calling Parametrised constructor of Base class

It should be called from constructor of Derived class

Example:

class Cuboid : public Rectangle

private :

int height;

public:

Cuboid(int lint b, int h); // Base class constructor should not be called from prototype

Cuboid::Cuboid(int lint b, int h) : Rectangle(l , b) // calling base class constructor

height=h;

Private member of Base in Derived class Constructor

No. Private members of Base class are not accessible in derived class.

Private members of Base class are inherited to Derived class, but not

accessible.

Copy Constructor by reference

• Copy constructor must take parameter by reference. If it is taking parameter

by value then it has to create an object and it will class constructor again.

• It may become recursive call to the constructor. Constructor calling

constructor.

Private Constructor

Yes constructor can be declared as private, but we cannot create the object directly. It can be done using static functions Example: class Test { int x,y; Test(int a, int b) { x=a; y=b; } public: static Test * CreateObject() // this function will create an object. { Test t=new Test(10,10); return t; } }; int main() { Test *t=Test::CeateObject(); } Virtual Base class I multiple-path inheritance, a derived class may get the duplicate features via multiple parent classes. To avoid duplicacy we make parent class as virtual