SlideShare une entreprise Scribd logo
1  sur  20
Inheritance
09/04/131 VIT - SCSE
By
G.SasiKumar., M.E., (Ph.D).,
Assistant Professor
School of Computing Science and Engineering
VIT University
09/04/132 VIT - SCSE
It is the Process of creating a New class from an existing class.
The Existing class is called Base or Parent class.
The New class is called as Child or Derived Class
Inheritance
base
subclass1 subclass2
Notation:
09/04/133 VIT - SCSE
Advantages
It permits code reusability. So, save time and increase
the program reliability.
Improve Program Reliability
It Permits code sharing
09/04/134 VIT - SCSE
class <derived classname> : public
<base classname>
{
---
----
};
Syntax for Inheritance
Derived classBase Class
Methods
and
Properties
Base class methods
+
Additional methods
Inheritance is the property that allows the
reuse of an existing class to build a new class
Inheritance
Multilevel
Inheritance
Single Inheritance
Multiple Inheritance
Hierarchical
Inheritance
Single Inheritance
A class can be derived from a single base
class is called single inheritance
Base Class
sub Class
Multiple Inheritance
Class A Class B
Class C
Class A
Class B
Class C
Multilevel Inheritance
Hierarchical Inheritance
Base Class
sub Class 1sub Class 3 sub Class 2
Hybrid Inheritance
Hybrid is nothing but the combination of
Multilevel and multiple Inheritance
Lecturer
Marks Department
Student
Multi level
Inheritance
Multiple
Inheritance
Function Type
Access directly to
Private Protected Public
Class member Yes Yes Yes
Derived class
member
No Yes Yes
Friend Yes Yes Yes
Friend class
member
Yes Yes Yes
//Example for Single inheritance
#include<iostream.h>
class test
{
public:
int a,b;
void getdata();
void display();
};
class sample:public test
{
public:
int x,y;
void getinfo();
void dis();
void sum();
};
void test::getdata()
{
cout<<"Enter a & b:"<<endl;
cin>>a>>b;
}
void test::display()
{
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
}
void sample::getinfo()
{
cout<<"Enter x & Y:";
cin>>x>>y;
}
void sample::dis()
{
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
}
void sample::sum()
{
int s;
s=a+b+x+y;
cout<<"Sum="<<s;
}
void main()
{
sample s;
s.getdata();
s.display();
s.getinfo();
s.dis();
s.sum();
getch();
}
It is the process of creating new class from more than one base
classes.
Syntax :
class <derived class >:<access specifier>
base_class1,<access specifier> base_class2...
{
private :
// members;
protected :
// members;
public :
//memebers;
};
#include<iostream.h>
class base1
{
protected :
int v1;
public :
void disp_base1() {
cout<<“v1 "<<v1<<endl;
}};
class base2
{
protected :
int v2;
public :
void disp_base2()
{
cout<<“v2 is"<<v2<<endl;
}};
class deri:public base1,public
base2
{
private:
int v3;
public :
deri(int a,int b,int c)
{
v1=a;
v2=b;
v3=c;
}
void disp_me()
{
cout<<“v3 is"<<v3;
} };
void main()
{
deri d(10,20,30);
clrscr();
d.disp_base1();
d.disp_base2();
d.disp_me();
getch();
}
AMBIGUITY IN MULTIPLE
INHERITANCE (use same
member)
#include<iostream.h>
class base1
{
public:
int i;
void disp()
{
cout<<"base1 value"<<i<<"n";
}
};
class base2
{
public:
int i;
void disp()
{
cout<<"base2 value"<<i;
}
};
class deri:public base1,public base2
{
};
void main()
{
deri d;
//d.i=10; not allowed
//d.disp();
d.base1::i=10;
d.base2::i=20;
d.base1::disp();
d.base2::disp();
getch();
}
RESULT : AMBIGUITY ERROR OCCURRED
//Example for MultiLevel inheritance
#include<iostream.h>
#include<conio.h>
class publication
{
public:
int bookno;
void getdata()
{
cout<<"Enter Book Number:";
cin>>bookno;
}
void putdata()
{
cout<<bookno<<endl;
}
};
class author:public publication
{
public:
char aname[20];
void getdata()
{
publication::getdata();
cout<<"Enter author name";
cin>>aname;
}
};
class test:public author
{
public:
void display();
};
void test::display()
{
cout<<"Book Number:"<<bookno<<endl;
cout<<"Author Name:"<<aname<<endl;
}
void main()
{
test t;
t.getdata();
t.putdata();
t.author::getdata();
t.display();
getch();
}
//Example for Hierarchical inheritance
#include<iostream.h>
#include<conio.h>
class test
{
public:
int a,b;
void getdata()
{
cout<<"Enter a & b:"<<endl;
cin>>a>>b;
}
};
class simple:public test
{
public:
void display()
{
cout<<"a="<<a<<endl;
cout<<"b="<<b;
}
};
class sample:public test
{
public:
void sum()
{
int sum;
sum=a+b;
cout<<"sum="<<sum;
}
};
void main()
{
simple s1;
sample s2;
s1.getdata();
s1.display();
s2.getdata();
s2.sum();
getch();
}
HYBRID
INHERITANCE
#include<iostream.h>
class lecturer
{
private :
char lecturer_name[20];
public :
void getdata()
{
cout<<"Enter Lecturer name";
cin>>lecturer_name;
}
void putdata()
{
cout<<"lecturer name is
"<<lecturer_name;
}
};
class department : public lecturer
{
private :
char department_name[20];
public:
void getdata()
{
cout<<"Enter Department name";
cin>>department_name;
}
void putdata()
{
cout<<"Department name is "<<department_name;
}
};
class marks
{
public :
int mark1;
int mark2;
int mark3;
void getdata()
{
cout<<"Enter the marks for 3 subjects";
cin>>mark1>>mark2>>mark3;
}
void putdata()
{
cout<<"The marks for 3 subjects are
"<<mark1<<"t"<<mark2<<"t"<<mark3;
}
};
class student:public department,public marks
{
private:
int roll_no;
char student_name[20];
public:
void getdata()
{
department::getdata();
cout<<"Enter the student name";
cin>>student_name;
cout<<"Enter the student Enrollment no";
cin>>roll_no;
marks::getdata();
}
void putdata()
{
department::putdata();
cout<<"The name & rollno is
"<<student_name<<"t"<<roll_no;
marks::putdata();
}
};
void main()
{
student s;
clrscr();
s.getdata();
s.putdata();
getch();
}
09/04/1320 VIT - SCSE
1. The derived class need not have a constructor as long as
the base class has a no-argument constructor.
2. However, if the base class has constructors with
arguments (one or more), then it is mandatory for the
derived class to have a constructor and pass the
arguments to the base class constructor.
3. When an object of a derived class is created, the
constructor of the base class is executed first and later the
constructor of the derived class.
Constructors in Derived Classes

Contenu connexe

Similaire à 10 inheritance

Similaire à 10 inheritance (20)

OOPS IN C++
OOPS IN C++OOPS IN C++
OOPS IN C++
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
 
Inheritance
InheritanceInheritance
Inheritance
 
chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdf
 
inheritance in OOPM
inheritance in OOPMinheritance in OOPM
inheritance in OOPM
 
201801 CSE240 Lecture 14
201801 CSE240 Lecture 14201801 CSE240 Lecture 14
201801 CSE240 Lecture 14
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
OOP Assign No.03(AP).pdf
OOP Assign No.03(AP).pdfOOP Assign No.03(AP).pdf
OOP Assign No.03(AP).pdf
 
Inheritance
InheritanceInheritance
Inheritance
 
7 class objects
7 class objects7 class objects
7 class objects
 
Inheritance
InheritanceInheritance
Inheritance
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdf
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
 
Inheritance
InheritanceInheritance
Inheritance
 
inhertance c++
inhertance c++inhertance c++
inhertance c++
 
06 inheritance
06 inheritance06 inheritance
06 inheritance
 
[OOP - Lec 20,21] Inheritance
[OOP - Lec 20,21] Inheritance[OOP - Lec 20,21] Inheritance
[OOP - Lec 20,21] Inheritance
 
lecture-2021inheritance-160705095417.pdf
lecture-2021inheritance-160705095417.pdflecture-2021inheritance-160705095417.pdf
lecture-2021inheritance-160705095417.pdf
 

Plus de Docent Education

12 constructors invocation and data members initialization
12 constructors invocation and data members initialization12 constructors invocation and data members initialization
12 constructors invocation and data members initializationDocent Education
 
12 constructors invocation and data members initialization
12 constructors invocation and data members initialization12 constructors invocation and data members initialization
12 constructors invocation and data members initializationDocent Education
 
11 constructors in derived classes
11 constructors in derived classes11 constructors in derived classes
11 constructors in derived classesDocent Education
 
4 Type conversion functions
4 Type conversion functions4 Type conversion functions
4 Type conversion functionsDocent Education
 
1 Intro Object Oriented Programming
1  Intro Object Oriented Programming1  Intro Object Oriented Programming
1 Intro Object Oriented ProgrammingDocent Education
 

Plus de Docent Education (14)

17 files and streams
17 files and streams17 files and streams
17 files and streams
 
16 virtual function
16 virtual function16 virtual function
16 virtual function
 
14 operator overloading
14 operator overloading14 operator overloading
14 operator overloading
 
13 exception handling
13 exception handling13 exception handling
13 exception handling
 
12 constructors invocation and data members initialization
12 constructors invocation and data members initialization12 constructors invocation and data members initialization
12 constructors invocation and data members initialization
 
12 constructors invocation and data members initialization
12 constructors invocation and data members initialization12 constructors invocation and data members initialization
12 constructors invocation and data members initialization
 
11 constructors in derived classes
11 constructors in derived classes11 constructors in derived classes
11 constructors in derived classes
 
6 pointers functions
6 pointers functions6 pointers functions
6 pointers functions
 
5 array
5 array5 array
5 array
 
4 Type conversion functions
4 Type conversion functions4 Type conversion functions
4 Type conversion functions
 
1 Intro Object Oriented Programming
1  Intro Object Oriented Programming1  Intro Object Oriented Programming
1 Intro Object Oriented Programming
 
3 intro basic_elements
3 intro basic_elements3 intro basic_elements
3 intro basic_elements
 
2 Intro c++
2 Intro c++2 Intro c++
2 Intro c++
 
unit-1-intro
 unit-1-intro unit-1-intro
unit-1-intro
 

Dernier

What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 

Dernier (20)

What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 

10 inheritance

  • 1. Inheritance 09/04/131 VIT - SCSE By G.SasiKumar., M.E., (Ph.D)., Assistant Professor School of Computing Science and Engineering VIT University
  • 2. 09/04/132 VIT - SCSE It is the Process of creating a New class from an existing class. The Existing class is called Base or Parent class. The New class is called as Child or Derived Class Inheritance base subclass1 subclass2 Notation:
  • 3. 09/04/133 VIT - SCSE Advantages It permits code reusability. So, save time and increase the program reliability. Improve Program Reliability It Permits code sharing
  • 4. 09/04/134 VIT - SCSE class <derived classname> : public <base classname> { --- ---- }; Syntax for Inheritance
  • 5. Derived classBase Class Methods and Properties Base class methods + Additional methods Inheritance is the property that allows the reuse of an existing class to build a new class
  • 7. Single Inheritance A class can be derived from a single base class is called single inheritance Base Class sub Class Multiple Inheritance Class A Class B Class C
  • 8. Class A Class B Class C Multilevel Inheritance Hierarchical Inheritance Base Class sub Class 1sub Class 3 sub Class 2
  • 9. Hybrid Inheritance Hybrid is nothing but the combination of Multilevel and multiple Inheritance Lecturer Marks Department Student Multi level Inheritance Multiple Inheritance
  • 10. Function Type Access directly to Private Protected Public Class member Yes Yes Yes Derived class member No Yes Yes Friend Yes Yes Yes Friend class member Yes Yes Yes
  • 11. //Example for Single inheritance #include<iostream.h> class test { public: int a,b; void getdata(); void display(); }; class sample:public test { public: int x,y; void getinfo(); void dis(); void sum(); }; void test::getdata() { cout<<"Enter a & b:"<<endl; cin>>a>>b; } void test::display() { cout<<"a="<<a<<endl; cout<<"b="<<b<<endl; } void sample::getinfo() { cout<<"Enter x & Y:"; cin>>x>>y; } void sample::dis() { cout<<"x="<<x<<endl; cout<<"y="<<y<<endl; } void sample::sum() { int s; s=a+b+x+y; cout<<"Sum="<<s; } void main() { sample s; s.getdata(); s.display(); s.getinfo(); s.dis(); s.sum(); getch(); }
  • 12. It is the process of creating new class from more than one base classes. Syntax : class <derived class >:<access specifier> base_class1,<access specifier> base_class2... { private : // members; protected : // members; public : //memebers; };
  • 13. #include<iostream.h> class base1 { protected : int v1; public : void disp_base1() { cout<<“v1 "<<v1<<endl; }}; class base2 { protected : int v2; public : void disp_base2() { cout<<“v2 is"<<v2<<endl; }}; class deri:public base1,public base2 { private: int v3; public : deri(int a,int b,int c) { v1=a; v2=b; v3=c; } void disp_me() { cout<<“v3 is"<<v3; } };
  • 15. AMBIGUITY IN MULTIPLE INHERITANCE (use same member) #include<iostream.h> class base1 { public: int i; void disp() { cout<<"base1 value"<<i<<"n"; } }; class base2 { public: int i; void disp() { cout<<"base2 value"<<i; } }; class deri:public base1,public base2 { }; void main() { deri d; //d.i=10; not allowed //d.disp(); d.base1::i=10; d.base2::i=20; d.base1::disp(); d.base2::disp(); getch(); } RESULT : AMBIGUITY ERROR OCCURRED
  • 16. //Example for MultiLevel inheritance #include<iostream.h> #include<conio.h> class publication { public: int bookno; void getdata() { cout<<"Enter Book Number:"; cin>>bookno; } void putdata() { cout<<bookno<<endl; } }; class author:public publication { public: char aname[20]; void getdata() { publication::getdata(); cout<<"Enter author name"; cin>>aname; } }; class test:public author { public: void display(); }; void test::display() { cout<<"Book Number:"<<bookno<<endl; cout<<"Author Name:"<<aname<<endl; } void main() { test t; t.getdata(); t.putdata(); t.author::getdata(); t.display(); getch(); }
  • 17. //Example for Hierarchical inheritance #include<iostream.h> #include<conio.h> class test { public: int a,b; void getdata() { cout<<"Enter a & b:"<<endl; cin>>a>>b; } }; class simple:public test { public: void display() { cout<<"a="<<a<<endl; cout<<"b="<<b; } }; class sample:public test { public: void sum() { int sum; sum=a+b; cout<<"sum="<<sum; } }; void main() { simple s1; sample s2; s1.getdata(); s1.display(); s2.getdata(); s2.sum(); getch(); }
  • 18. HYBRID INHERITANCE #include<iostream.h> class lecturer { private : char lecturer_name[20]; public : void getdata() { cout<<"Enter Lecturer name"; cin>>lecturer_name; } void putdata() { cout<<"lecturer name is "<<lecturer_name; } }; class department : public lecturer { private : char department_name[20]; public: void getdata() { cout<<"Enter Department name"; cin>>department_name; } void putdata() { cout<<"Department name is "<<department_name; } }; class marks { public : int mark1; int mark2; int mark3; void getdata() { cout<<"Enter the marks for 3 subjects"; cin>>mark1>>mark2>>mark3; } void putdata() { cout<<"The marks for 3 subjects are "<<mark1<<"t"<<mark2<<"t"<<mark3; } }; class student:public department,public marks { private: int roll_no;
  • 19. char student_name[20]; public: void getdata() { department::getdata(); cout<<"Enter the student name"; cin>>student_name; cout<<"Enter the student Enrollment no"; cin>>roll_no; marks::getdata(); } void putdata() { department::putdata(); cout<<"The name & rollno is "<<student_name<<"t"<<roll_no; marks::putdata(); } }; void main() { student s; clrscr(); s.getdata(); s.putdata(); getch(); }
  • 20. 09/04/1320 VIT - SCSE 1. The derived class need not have a constructor as long as the base class has a no-argument constructor. 2. However, if the base class has constructors with arguments (one or more), then it is mandatory for the derived class to have a constructor and pass the arguments to the base class constructor. 3. When an object of a derived class is created, the constructor of the base class is executed first and later the constructor of the derived class. Constructors in Derived Classes