About Lesson
Constructor in Inheritance
1. Constructor Never Inherited
2. Constructor of Parent class always be called first before child class
constructor
C++
/*
Inheritance is a technique to acquire the members of existing class into new
class. New class is known as child class or derive class or sub class and
existing is known as parent class or base class or super class.
syntax:
class parent_class
{
private:
.......
....
protected:
..........
......
public:
.............
....
};
class child_class : type_of_inheritance parent_class_name
{
private:
...............
....
protected:
......
....
public:
.......
....
};
Type of Inheritance
1. private - parent class members become private in child class
2. protected - parent class members become proctected in child class
3. public - parent class proctected members become proctected in child class
and public members become public
Single Level
A
|
|
B
Multi Level
A
|
|
B
|
|
C
Multiple
A........B
|
|
C
Hierarchical
A
|_____
| |
B C
| ___
D E
Hybrid
Multilevel + Multiple
A
|
|
B ---- C
|
|
D
*/
#include<conio.h>
#include<iostream.h>
class A
{
private:
void amsg()
{
cout<<"\n Hello from Private msg of A";
}
protected:
void amsgpro()
{
cout<<"\n Hello from A pro";
}
public:
void amsgpub()
{
cout<<"\n Hello From public msg of A";
}
};
class B: public A
{
};
void main()
{
B b1;
clrscr();
//b1.amsg();
//b1.amsgpro();
b1.amsgpub();
getch();
}
C++
#include<iostream.h>
#include<conio.h>
/*
Inheritance - Acquire the members of existing class into new one.
Existing class - Parent class or base class or super class
New class - Child class or Derive class or sub class
child class has all the members of parent class but it also has new one.
Inheritance Types
Single Level
A
|
B
........ Multi Level
A
|
B
|
C
--------------Multiple Inheritance
A.....B
|
C
---------- Hierarichal
A
|
B----C
| |
D E
| --
F G
--------------- Hybrid Inheritance
Multiple + Multi Level
A
|
B
|
C ---D
|
E
Syntax:
class Parent
{
private:
.....
protected:
......
public:
......
};
class Child : public Parent
{
private:
.....
protected:
.......
public:
.....
};
*/
class Rect
{
protected:
int l,b; // instance variable
public:
Rect() // Default
{
cout<<"\n Rect Created - Default Constructor of Rect is Called";
l=0;
b=0;
}
Rect(int len, int b) // Parameterized
{
cout<<"\n Rect Created using parameterized constructor";
l=len;
this->b=b;
}
void input()
{
cout<<"\n enter len and bre";
cin>>l>>b;
}
void output()
{
cout<<"Rect dimension is "<<l<<"x"<<b;
}
int area()
{
return l*b;
}
};
class Box:public Rect
{
int h;
public:
Box() // parent class constructor called first before child class
{
cout<<"\n Box Created";
h=0;
}
Box(int l, int b, int h) : Rect(l,b) // explicit call of constructor of parent class
{
cout<<"\n Now Box Using Parameterized Created";
this->h=h;
}
void input()
{
Rect::input();
cout<<"\n enter height ";
cin>>h;
}
void output()
{
cout<<"\nBox Dimension "<<l<<"x"<<b<<"x"<<h;
}
int operator==(Box &b)
{
return this->vol()==b.vol();
}
int vol()
{
//return area()*h;
return l*b*h;
}
};
void main()
{
clrscr();
Box b1(1,2,3);
Box b2(3,2,1);
Box b3;
//b1.input();
//b2.input();
b1.output();
b2.output();
if(b1==b2)
{
cout<<"\n Boxes are equal";
}
else
{
cout<<"\n Boxes are not equal";
}
getch();
}