Inheritance
Inheritance is the capability of one class to acquire properties and characteristics from another class.
The class whose properties are inherited by other class is called the Parent or Base or Super class.
And, the class which inherits properties of other class is called Child or Derived or Sub class.
Inheritance makes the code reusable.
When we inherit an existing class, all its methods and fields become available in the new class, hence code is reused.
Types of inheritance
Single Inheritance
class animal
{
char type[20];// mammal or reptile
public:
...
};
class dog : public animal
{
char breed[25]; // Pug or Golden retriever
public:
...
};
Multiple Inheritance
class mammal
{
char type[20],respiration[20];
char gender;
int no_of_vertebrates;
public:
....
};
class WingedAnimal
{
char feather_color[25],beak_color[20];
public:
...
};
class bat:public mammal, public WingedAnimal
{
float wingspan;
char location[25];
public:
...
};
Multilevel Inheritance
class A
{int x;
public:
void read();
void print();
};
class B: public A
{
int y;
public:
void Bread();
void Bprint();
};
class C: public B
{
int z;
public:
void Cread();
void Cprint();
};
Hierarchical Inheritance
class A
{int x;
public:
void read();
void print();
};
class B: public A
{
int y;
public:
void Bread();
void Bprint();
};
class C: public A
{
int z;
public:
void Cread();
void Cprint();
};
Access specifiers with a class - private, public, protected
Private members of a class cannot be accessed by any other derived class or the object of the class.
Protected members of a class can be accessed only by the derived class and not the object of the class.
Public members of a class can be accessed both by the derived classes and the object of the class.
Visibility Modes
If the visibility mode is public, public members of the base class become public members of the derived class and protected members become protected members of the derived class.
eg
class B: public A
{ };
If the visibility mode is protected, public and protected members of the base class become protected members of the derived class.
eg
class B:protected A
{ };
If the visibility mode is private, public and protected members of the base class become private members of the derived class.
eg
class B:private A
{ };
Function Overriding/Shadowing
When a derived class function has the same name as that of its base class's member function , the derived class's member function hides/shadows the base class's inherited function. This situation is known as function overriding or shadowing.
Eg
class A
{
int x;
public:
void read();
void display();
};
class B:public A
{
int y;
public:
void read();
void display();
};
Whenever object of B is declared in main, the member functions that can be accessed by the object will be as follows:
read(), display() functions of class B
A::read(), A::display() functions of class A
Here the derived class B overrides the member functions of class A as there is same name for both the member functions.
Containership or Aggregration
When a class contains objects of other class types as its member, it is referred to as Containersip or Aggregation.
class A
{
int x;
public:
A();
void read();
void disp();
};
class B
{
int y;
A a1;// object of class A
public:
void check();
};
Parameterized Constructors used in Inheritance
#include <iostream.h>
#include <conio.h>
class A
{
int x,y;
public:
A(int a, int b)
{
x=a;
y=b;
}
};
class B
{
int z;
A a1;
public:
B(int c,int a,int b):A(a,b)
{
z=c;
}
};
void main()
{
B b1;
}
Virtual Base Class
In a hierarchical inheritance, more than 1 derived class inherit from a single base class. In this situation a copy of base class will be maintained in each of the derived class. When one more derived class derives from these two classes, there will be ambiguity as to which copy of base class will be used. In order to overcome this ambiguity, virtual keyword is prefixed to visibility mode for each of the derived class, so that one copy of base will be present when the object of the final derived class is created.
class A
{
public:
int a;
...};
class B: virtual public A
{
public:
int b;
........
};
class C: virtual public A
{
public:
int c;
.........
};
class D:public B, public C
{
public:
int total;
....
};
Size of the object will be total number of size of data types of all base classes and its own class (in bytes).
char - 1, int - 2, float - 4, long- 4, double - 8
Size of the object will be total number of size of data types of all base classes and its own class (in bytes).
char - 1, int - 2, float - 4, long- 4, double - 8
Text book Unsolved problems - P 291
1) a)MNC
b)129 bytes
c)Enter(), Output(), Association(), Add(), Show(), EnterDate(), DisplayData()
d) Country[25]
2) a) CEO
b) 20 bytes
c) display(),INDATA(), OUTDATE(), input(), output()
d) yes
1) a)MNC
b)129 bytes
c)Enter(), Output(), Association(), Add(), Show(), EnterDate(), DisplayData()
d) Country[25]
2) a) CEO
b) 20 bytes
c) display(),INDATA(), OUTDATE(), input(), output()
d) yes
2017 - Board - QP
Answer the questions (i) to (iv) based on the following :
class First
{
int X1;
protected:
float X2;
public:
First();
void Enter1(); void Display1();
};
class Second : private First
{
int Y1;
protected:
float Y2;
public:
Second();
void Enter2();
void Display();
};
class Third : public Second
{
int Z1;
public:
Third();
void Enter3();
void Display();
};
void main()
{
Third T; //Statement 1
___________________; //Statement 2
}
(i) Which type of Inheritance out of the following is illustrated in the above example ?
Single Level Inheritance, Multilevel Inheritance, Multiple Inheritance
Multilevel
(ii) Write the names of all the member functions, which are directly accessible by the object T of class Third as declared in main() function.
Member functions : Enter2(), Second::Display(), Enter3(), Third::Display()
(iii) Write Statement 2 to call function Display() of class Second from the object T of class Third.
T.Second::Display();
(iv) What will be the order of execution of the constructors, when the object T of class Third is declared inside main() ?
First()-> Second()->Third()
No comments:
Post a Comment