Constructor and inheritance
- The compiler automatically call a base class constructor before executing the derived class constructor.
- The compiler’s default action is to call the default constructor in the base class.
- If you want to specify which of several base class constructors should be called during the creation of a derived class object.
- In such situations, you must define specifically which base class constructor the compiler will call.
- This is done by specifying the arguments to the selected base class constructor in the definition of the derived class constructor.
class Rectangle
{
private :
float width;
float length;
public:
length = 0;
Rectangle ()
{
width = 0;
}
{
Rectangle (float len, float wid)
length = len;
width = wid;
}
class Box : public Rectangle
float area()
{
return length * width ;
}
};
{
private :
Box (float len, float wid, float ht) : Rectangle(len, wid)
float height;
public:
Box ()
{
height = 0;
}
{
height = ht;
}
Box cx(4,8,5);
float volume()
{
return area() * height;
}
};
int main ()
{
Box bx;
cout << bx.volume() << endl;
cout << cx.volume() << endl;
return 0;
}
Expected Output :-
0
160
// Program to illustrate the concept of Constructors.
#include <iostream.h>
#include <conio.h>
class construct {
public:
int a, b;
// Default Constructor
construct()
{
a = 10;
b = 20;
}
};
int main()
{
// Default constructor called automatically
// when the object is created
construct c;
cout << "a: " << c.a << endl
<< "b: " << c.b;
return 1;
}
Expected Output:-
a: 10
b: 20
// C++ program to demonstrate implementation of Inheritance.
#include <iostream.h>
#include <conio.h>
//Base class
class Parent
{
public:
int id_p;
};
// Sub class inheriting from Base Class(Parent)
class Child : public Parent
{
public:
int id_c;
};
//main function
int main()
{
Child obj1;
// An object of class child has all data members
// and member functions of class parent
obj1.id_c = 7;
obj1.id_p = 91;
cout << "Child id is " << obj1.id_c << endl;
cout << "Parent id is " << obj1.id_p << endl;
return 0;
}
Expected Output:-
Child id is 7
Parent id is 91
Overriding Base Class Functions
- A derived class can override a member function of its base class by defining a derived class member function with the same name and parameter list.
- It is often useful for a derived class to define its own version of a member function inherited from its base class.
- This may be done to specialize the member function to the needs of the derived class.
- When this happens, the base class member function is said to be overridden by the derived class.
class mother
{
public:
void display ()
{
cout << "mother: display function\n";
}
};
class daughter : public mother
{
cout << "daughter: display function\n\n";
public:
void display ()
{
}
};
int main ()
}
{
daughter rita;
rita.display();
return 0;
}
Expected Output:-
daughter: display function
daughter: display function
Gaining Access to an Overridden Function
- It is occasionally useful to be able to call the overridden version.
- This is done by using the scope resolution operator to specify the class of the overridden member function being accessed.
class daughter : public mother
{
public:
void display ()
{
cout << "daughter: display function\n\n";
mother::display();
}
};
Expected Output:-
daughter: display function
mother: display function
Tags:
c++ by mskuthar
constructor
Constructor and inheritance
Inheritance in c++
mskuthar
www.mskuthar.blogspot.com