Sie sind auf Seite 1von 33

INHERITANCE

■ The mechanism of deriving a new class from an already existing class ,


such that the new class inherits all members from the old class is called
inheritance.
■ The class whose members are inherited is called base class or super
class.
■ The class that inherits the members of the base class is called derived
class or child class or sub class.
■ Need of inheritance
– Code reusability
– Transitive nature
Types of inheritance
Single inheritance Multiple inheritance
Hierarchical inheritance
Base class 1 Base class 2
Base class Base class

Derived class Derived class


Derived class Derived class 2
1

Multilevel inheritance Hybrid


Hybrid inheritance
inheritance
Base class Base class Base class
Base class
Derived class
Derived class Derived class Derived class 1
1 1 2
Derived class Derived class
Derived class Derived class 2 2
2 3
Definition of derived class
■ Syntax
■ class derived class name : access specifier base class name
■ Eg: class B : public A ------ This means class B is derived from class A
■ If no access specifier specified , by default it is taken as private.
■ Derived class inherits all the members of the base class , but can access only the non
private members of the base class.
■ Size of a class/object = size of public data members + size of protected data
members + size of private data members
■ Size of derived class = Sum of size of all data members of base class + Sum of size of
all data members of derived class
■ Only public and protected members of a base class is directly
accessible inside the derived class.
■ Members of derived class cannot be accessed inside the base
class.
Base class members Accessible by objects Accessible inside derived class

Public Yes Yes

Protected No Yes

Private No No
Visibility Mode
Public Derivation Public Members – Public
Protected Members – Protected
Private Members – Private

Protected Derivation Public Members – Protected


Protected Members – Protected
Private Members - Private

Private Derivation Public Members – Private


Protected Members – Private
Private Members - Private
4. Answer the questions given below after going through the
class Unittest:private Examination
following program code:-
{
class Examination
char testcode[5];
{
protected:
char Etitle[20];
float totalmarks;
protected:
public:
char Subname[20];
Unittest();
int class;
void readUnittestdetails();
public:
void showUnittestdetails();
Examination();
};
void getexamdetails();
class Halfyearly: protected Unittest
void showexamdetails();
{
};
char topics[20];
float weightage;
public:
i)Name the members accessible by object of class Halfyearly void readHYdetails();
void showHYdetails();
ii) State the size of class Unittest and Halfyearly. }E1;

iii) What type of inheritance is demonstrated in this program


code.
class CUSTOMER ~SALESMAN() {
{ cout<<”\n Destroyed SALESMAN”; }
int Cust_no; };
char Cust_Name[20]; class SHOP : public SALESMAN, private CUSTOMER
protected: {
void Register(); char Voucher_No[10];
public: char Sales_Date[8];
CUSTOMER( ) public :
{ cout<<”\n First costomer is in”; } SHOP( ) {cout<<“\n constructing SHOP”;}
void Status( ); void Sales_Entry( );
~CUSTOMER() { cout<<”\n CUSTOMER just left”; }}; void Sales_Detail( );
class SALESMAN ~SHOP(){ cout<< “\n Just destroyed”;}
{ int Salesman_no; };
char Salesman_Name[20];
protected:
double Salary; 1. State the type of inheritance shown in the qn.
float commission; 2. What is the size of class customer , salesman and shop.
public: 3. State the members accessible by objects of class shop.
SALESMAN( ) 4. State the members accessible by objects of class salesman.
{ cout<<”\n Now its Salesman”;} 5. State the members accessible by objects of class customer.
void Enter( );
void Show( );
Rules for visibility modes
■ If a member has to be inherited as well as available to each and every non
member function then it should be declared in public visibility mode.
■ If a member function need to be inherited but not intended to be public then it
should be declared in protected visibility mode.
■ If a member function need not be inherited , then place it under private
derivation.
Difference between private and protected – Private members are not inherited but
protected members are inherited.
Similarity between private and protected – Both are accessible only by member
functions of the same class.
Single Inheritance - Private Visibility

class X class Y: private X class Y


{ { {
int a; int p; int a;
protected: protected: int p;
int b; int q; int b;
void Input(); void Accept(); void Input();
public: public: int c;
int c; int r; void Print();
void Print(); void Show(); protected:
}; }; int q;
void Accept();
public:
int r;
void Show();
};
Single Inheritance - Protected Visibility

class Y
class X class Y :protected X {
{ { int a;
int a; int p; int p;
protected: protected: protected:
int b; int q; int q;
void Input(); void Accept(); int b;
public: public: void Input();
int c; int r; int c;
void Print(); void Show(); void Print();
}; }; void Accept();
public:
int r;
void Show();
};
Single Inheritance - Public Visibility

class X class Y : public X class Y


{ { {
int a; int p; int a;
protected: protected: int p;
int b; int q; protected:
void Input(); void Accept(); int q;
public: public: int b;
int c; int r; void Input();
void Print(); void Show(); void Accept();
}; }; public:
int r;
int c;
void Print();
void Show();
};
Multilevel Inheritance

class X class Y: public X class Z: public Y


{ { {

int a; int p; int I,


protected: protected:
protected:
int q; int j;
int b;
void Accept(); void getdata();
void Input();
public: public:
public:
int r; int k;
int c;
void Show(); void putdata();
void Print(); }; };
};
Multiple Inheritance

class X
class Y class Z: public X, public Y
{ { {
int a; int p; int I,
protected: protected: protected:
int b; int q; int j;
void Input(); void Accept(); void getdata();

public: public: public:

int c; int r; int k;


void Show(); void putdata();
void Print();
}; };
};
Hierarchical Inheritance

Class X Class Y :private X Class Z: public X


{ { {

int a; int p; int I,


protected: protected:
protected:
int q; int j;
int b;
void Accept(); void getdata();
void Input();
public: public:
public:
int r; int k;
int c;
void Show(); void putdata();
void Print(); }; };
};
class teacher class schedule: public course , private teacher
{ {
int tcode; int DD , MM , YYYY;

2018 Outside
protected: public:
char name[20]; schedule();
public: void start();
teacher();
void enter(); };
void view();
Delhi
void show(); void main()
}; {
class course schedule s;
{ }
int id;
protected: 1. Which type of inheritance is illustrated in the example given.
char title[30]; 2. Write the names of all members which are directly accessible by the
public: member function view() of class schedule.
course(); 3. Write the names of all members ,which are directly accessible by Object
void Initiate(); S of class Schedule declared in the main() function.
void Display(); 4. What will be the order of execution of constructors when object S of class
}; schedule is declared inside main() function.
2017 Outside Delhi Answer the questions given below:- class Third : public Second
{
class First class Second : private First int z1;
{ { public:
int x1; int y1; Third();
protected: protected: void enter3();
float x2; float y2; void display();
public: public: };
first(); second();
void enter2(); void main()
void enter1(); {
void display1(); void display();
}; Third T; // Statement 1
}; _________ // Statement 2
1. Which type of inheritance is shown ? }
2. Write the names of all member functions accessible by object T .
3. Write statement2 to call function display() of class Second from object
T.
4. What will be the order of execution of constructors.
class item
{ class salepoint : public item , private trader
int id; {
2016 Outside Delhi
char iname[20]; char name[20],location[20];
protected: public:
float qty; salepoint();
public: void enterall();
item(); void viewall();
void enter(); };
void view();
};

class trader 1. Which type of inheritance is illustrated in the example.


{ 2. Write the names of all data members which are directly
int dcode; accessible from member functions of class salepoint.
protected: 3. Write the names of all member functions which are directly
char manager[20];
accessible by an object of class salespoint.
public:
trader(); 4. What will be the order of execution of constructors when an
void enter(); object of class salepoint is declared.
void view();
};
2015 OUTSIDE DELHI
Consider the following class state :
class State
2014 OUTSIDE DELHI
{
protected:
int tp; //no. of tourist places
public:
State()
{ Write a code in C++ to publically derive another class ‘District’
tp = 0; with
} the following additional members derived in the Public visibility
void inctp() mode.
{ Data Members
tp++; distname – char (50)
} population – long
int gettp() Member functions :
{ ● dinput() – To enter distname and population.
return tp; ● doutput() – To display distname and population on screen.
} };
2013 OUTSIDE DELHI
2012 OUTSIDE DELHI
2011 OUTSIDE DELHI
2010 OUTSIDE DELHI
Answer the questions based on the following:

class Regular
{
2009 OUTSIDE DELHI
char SchoolCode[10];
public:
void InRegular( );
void OutRegular( );
};

class Distance
{
char StudyCentreCode[5];
public:
void InDistance ( );
void OutDistance( );
};

class Course: public Regular, private Distance


{
char Code[5];
float Fees ;
int Duration;
public:
void InCourse( );
void OutCourse( );
};

Which type of Inheritance is shown in the above example?

Write names of all the member functions accessible from OutCourse function of
class Course.

Write name of all the members accessible through an object of class Course.

Is the function InRegular( ) accessible inside the function InDistance( )? Justify your
answer.
class Toys
{ char TCode[5];
protected:
2008 OUTSIDE DELHI
float Price;
void Assign(float);
public:
Toys();
void TEntry();
i) Which type of inheritance is shown in the above
void TDisplay(); };
class SoftToys: public Toys example?
{ char STName[20]; ii) How many bytes will be required by an object of the
float Weight;
class SoftToys?
public:
SoftToys (); iii) Write name of all the data member(s) accessible from
void STEntry(); member functions of class SoftToys.
void STDisplay();};
class ElectronicToys:public Toys iv) Write name of all the member functions, which are
{ char ETName[20]; accessible from an object of the class ElectronicToys.
int No_of_Batteries;
public:
ElectronicToys();
void ETEntry();
void ETDisplay();};
class Teacher
{ char TNo[5],TName[20],Dept[10];
int Workload;
2007 OUTSIDE DELHI
protected:
i) Which type of inheritance is depicted by the above
float Salary;
example?
void AssignSal(float);
public:
ii) Identify the member function(s) that cannot be called
Teacher();
directly from the objects of class School from the following:
void TEntry();
TEntry() , SDisplay(), SchEntry()
void TDisplay(); };
class Student
iii) Write name of all the member(s) accessible from
{ char Admno[10],SName[20],Stream[10];
member functions of class School.
protected:
int Attendance,TotMarks;
iv) If class School was derived privately form Class Teacher
public:
and privately form class Student, then, name the member
Student();
function(s) that could be accessed through Objects of class
void SEntry();
School.
void SDisplay();};
class School:public Student,public Teacher
{ char SCode[10],SchName[20];
public:
School();
void SchEntry();
void SchDisplay();};
Inheritance and Constructors & Destructors

class base class derived : public base


■ Whenever an object of a derived { {
class is created , first the base class int x,y; int z;
constructor is invoked followed by public: public:
derived class constructor. base(int a, int b) derived(int p, int q ,int r):base (p,q)
{ {
■ Constructors & Destructors of base x=a; z=r;
class are not inherited to the derived y=b; }
class. } };
};
■ When a base class has a
class derived : public base
parameterized constructor , then it is
{
the responsibility of the derived class public:
to provide the arguments required by derived(int p, int q):base (p,q)
the base class. {
}
};
As long as base class constructor takes no arguments , the derived class need not have
a constructor function. But if the base class has a parameterized constructor then it is
mandatory for the derived class to have a parameterized constructor.

class X class Y class Z : public X , public Y


{ { {
int a,b; int c,d; int e,f;
public: public: public:
X(int p,int q) Y(int r,int s) Z(int a1,int b1,int c1,int d1,int e1,int f1) : X(a1,b1) , Y(c1,d1)
{ { {
a=p; c=r; e=e1;
b=q; d=s; f=f1;
} } }
}; }; };
■ Private members of the base class can be indirectly accessed by the
inherited functions (public , protected) of the base class.
class base class derived: public
{ base
int eno,salary; {
public: int allowance; As both base class and
void get() public: derived class have
{ void get() functions with the same
cin>>eno>>salary; { name – get() , the scope
} base::get(); resolution operator is used
void disp() cin>>allowance;
{ } to show which function is
cout<<eno<<salary; void display() called.
}; {
disp();
cout<<allowance;
} };
■ The size of an empty class is 1 byte. It depends on the range specified for
char datatype by the compiler.

class X class X class X class X


{ { { {
}; }; int a; int a;
class Y : public X class Y : public X }; };
{ { class Y : public X class Y : public X
}; int a; { {
cout<<sizeof(X)< }; float b; };
<sizeof(Y); cout<<sizeof(X)< }; cout<<sizeof(X)<
1 byte 1 byte <sizeof(Y); cout<<sizeof(X)< <sizeof(Y);
1 byte <sizeof(Y); 2 byte 2 byte
class X
3 byte(1+2) 2 byte 4 byte
{
};
cout<<sizeof(X);
1 byte
int x=10; - global variable class B : public A
int z=5; {
class A int y; x of class A gets inherited to
{ void get() class B but since it is a
int x; - local variable { private member of class A , it
public: cin>>y; cannot be directly accessed
void in(); } in class B.
void disp(); void cal() As x is already present in
}; { class B , the global variable x
x=x+10; ---Error is hidden and is not
y=y*5; activated.
z=z+10;
} };
class A class A
{ {
int x; int x;
public: public:
int y; int y;
}; };
class B:public A class B:private A
{ {
int c; int c;
A::y; -- Error public:
public: int d;
int d; A::y; -- This is allowed
}; };

Das könnte Ihnen auch gefallen