Professional Documents
Culture Documents
Inheritance
by
Shivani Nanda,
Silicon Institute of Technology,
Patia Hills, Bhubaneswar-751024
Shivani Nanda 1
Inheritance
Inheritance
Silicon Institute of Technology
Shivani Nanda 2
Inheritance
Access Specifier
Silicon Institute of Technology
Shivani Nanda 3
Inheritance
public inheritance
Silicon Institute of Technology
class A{
private : int x;
public : int y;
protected: int z;
};
class B: public A
{ //body
// here x is inaccessible, y is public, z is protected
};
Shivani Nanda 4
Inheritance
Single Inheritance : Public Mode
Silicon Institute of Technology
class A{
private : int x;
public : int y;
protected: int z;
};
class B: private A
{ //body
// here y and z are private
};
Shivani Nanda 6
Inheritance
Single Inheritance : Private Mode
Silicon Institute of Technology
private: //.
protected: //
};
Shivani Nanda 8
Inheritance
Silicon Institute of Technology
Shivani Nanda 9
Inheritance
protected inheritance
Silicon Institute of Technology
class A{
private : int x;
public : int y;
protected: int z;
};
class B: protetced A{
//body
// here x is inaccessible,
y and z are protected
};
Shivani Nanda 10
Inheritance
Single Inheritance : Protected Mode
Silicon Institute of Technology
Shivani Nanda 12
Inheritance
class person
Silicon Institute of Technology
Shivani Nanda 14
Inheritance
Silicon Institute of Technology
void show_licenceno()
{cout<<" Licence Number: "<<licenceno<<endl; } };
int main()
{ owner o1;
o1.getval(111,33,"Suzanne wesley","R.K.Puram");
o1.setval("MS Vision","Connaught Place");
o1.getlicenceno(667677);
o1.show_person();
o1.show_company();
o1.show_licenceno();
return 0;Nanda
Shivani } 15
Inheritance
Ambiguity in Multiple Inheritance
Silicon Institute of Technology
Shivani Nanda 16
Inheritance
Ambiguity in Multiple Inheritance : Example
Silicon Institute of Technology
class bank{
protected: int code;char bname[15],baddress[25];
public: void getdetail(int c,char bn[],char badd[])
{ code = c;
strcpy(bname,bn);
strcpy(baddress,badd);} };
class person{ protected:int code, age;
char name[20],address[30];
public: void getval(int sn, int ag, char st[],char add[])
{ code = sn;
age = ag;
strcpy(name,st);
strcpy(address,add);} };
Shivani Nanda 17
Inheritance
class owner : public bank, public person
Silicon Institute of Technology
{int regno;
public: void getregno(int regnum)
{regno = regnum;}
void show_regno(); };
void owner::show_regno()
{ cout<<" Serial Number : "<<person::code<<endl;
cout<<" Age : "<<age<<endl;
cout<<" Name : "<<name<<endl;
cout<<" Bank Code : "<<bank::code<<endl;
cout<<" Bank Name : "<<bname<<endl;
cout<<" Bank Address : "<<baddress<<endl;
cout<<" Registeration Number : "<<regno<<endl; }
Shivani Nanda 18
Inheritance
Silicon Institute of Technology
int main()
{
owner o1;
o1.getdetail(1345,"Bank of Baroda","Anand Vihar");
o1.getval(112,27,"Himanshu Ahuja","Anand Vihar");
o1.getregno(334224);
cout<<endl<<endl;
o1.show_regno();
cout<<endl<<endl;
return 0;
}
Shivani Nanda 19
Inheritance
Multilevel Inheritance
Silicon Institute of Technology
faculty
Shivani Nanda 20
Inheritance
Multilevel Inheritance : Transitive Nature
Silicon Institute of Technology
Shivani Nanda 21
Inheritance
Multilevel Inheritance : Example
Silicon Institute of Technology
Shivani Nanda 22
Inheritance
Silicon Institute of Technology
Shivani Nanda 23
Inheritance
Silicon Institute of Technology
int main()
{
faculty f1;
f1.getval(112,27,"Shantilal Garodia","Nehru Place");
f1.getsal(30000);
f1.getfaculty("Pschology");
cout<<endl<<endl;
f1.showfaculty();
cout<<endl<<endl;
return 0;
}
Shivani Nanda 24
Inheritance
Hierarchical Inheritance
Silicon Institute of Technology
graduate undergraduate
Shivani Nanda 25
Inheritance
Hierarchical Inheritance
Silicon Institute of Technology
Shivani Nanda 26
Inheritance
Hybrid Inheritance
Silicon Institute of Technology
Shivani Nanda 27
Inheritance
Hybrid Inheritance : Example
Silicon Institute of Technology
Shivani Nanda 28
Inheritance
Silicon Institute of Technology
Shivani Nanda 29
Inheritance
Multipath Inheritance : A form of Hybrid Inheritance
Silicon Institute of Technology
Shivani Nanda 30
Inheritance
Multipath Inheritance : Example
Silicon Institute of Technology
Shivani Nanda 31
Inheritance
Multipath Inheritance : Ambiguity
Silicon Institute of Technology
employee student
Shivani Nanda 32
Inheritance
Ambiguity resolution in multipath inheritance
Silicon Institute of Technology
Shivani Nanda 34
Inheritance
Virtual Base Classes
Silicon Institute of Technology
Shivani Nanda 35
Inheritance
Silicon Institute of Technology
Shivani Nanda 37
Inheritance
Silicon Institute of Technology
Shivani Nanda 39
Inheritance
Order of Calling of Constructor and Destructor
Silicon Institute of Technology
Shivani Nanda 40
Inheritance
Constructors and Destructors in Multiple Inheritance
Silicon Institute of Technology
Shivani Nanda 41
Inheritance
Constructors and Destructors of Virtual Base Classes
Silicon Institute of Technology
Shivani Nanda 42
Inheritance
Parameterized Constructor of the Base class
Silicon Institute of Technology
Shivani Nanda 43
Inheritance
Silicon Institute of Technology
Shivani Nanda 44
Inheritance
Silicon Institute of Technology
Shivani Nanda 45