Inheritance in C++ - Part III
Constructors and inheritance
We know that when an object is created a special function called constructor is called implicitly, whose work is to initialize the object. In case of a derived class object, is the constructor called? If so, which one? Base or derived?
Both are called. First the base class constructor is called, then derived class constructor is called.
#include<iostream>
using namespace std;
class shape
{
public:
shape()
{
cout<<"shape constructor"<<endl;
}
};
class circle : public shape
{
public:
circle()
{
cout<<"circle constructor"<<endl;
}
};
int main()
{
circle c1;
}
OUTPUT
shape constructor
derived constructor
But what if the base class constructor is non-default that is it takes one or more parameters? If you remember, if a class has such constructor, compiler does not provide trivial default constructor and creation of object must provide the parameter. Now coming to derived class object creation, circle c1 as shown in previous code will give an error something like this.
Correct method is to call the constructor of base class in the header of derived class constructor, as shown below.
class shape
{
int color;
public:
shape(int c)
{
color = c;
cout<<"shape constructor"
}
};
class circle:public shape
{
public:
circle:shape(0)
{
cout<<"circle constructor";
}
};
Destructors:
When a derived class object is deleted, i.e. the function or block it is defined is completed, both destructors are called. But in this case first derived class destructor is called followed by base class destructor.
Download the programs from here
Classes with only default constructors
Classes with non-default constructors
We know that when an object is created a special function called constructor is called implicitly, whose work is to initialize the object. In case of a derived class object, is the constructor called? If so, which one? Base or derived?
Both are called. First the base class constructor is called, then derived class constructor is called.
#include<iostream>
using namespace std;
class shape
{
public:
shape()
{
cout<<"shape constructor"<<endl;
}
};
class circle : public shape
{
public:
circle()
{
cout<<"circle constructor"<<endl;
}
};
int main()
{
circle c1;
}
OUTPUT
shape constructor
derived constructor
But what if the base class constructor is non-default that is it takes one or more parameters? If you remember, if a class has such constructor, compiler does not provide trivial default constructor and creation of object must provide the parameter. Now coming to derived class object creation, circle c1 as shown in previous code will give an error something like this.
t.cpp: In constructor 'circle::circle()': Line 22: error: no matching function for call to 'shape::shape()' compilation terminated due to -Wfatal-errors.
Correct method is to call the constructor of base class in the header of derived class constructor, as shown below.
class shape
{
int color;
public:
shape(int c)
{
color = c;
cout<<"shape constructor"
}
};
class circle:public shape
{
public:
circle:shape(0)
{
cout<<"circle constructor";
}
};
Destructors:
When a derived class object is deleted, i.e. the function or block it is defined is completed, both destructors are called. But in this case first derived class destructor is called followed by base class destructor.
Download the programs from here
Classes with only default constructors
Classes with non-default constructors
Comments
Post a Comment