SlideShare una empresa de Scribd logo
1 de 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

Más contenido relacionado

Similar a 10 inheritance

Similar a 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
 
lecture-2021inheritance-160705095417.pdf
lecture-2021inheritance-160705095417.pdflecture-2021inheritance-160705095417.pdf
lecture-2021inheritance-160705095417.pdf
 
[OOP - Lec 20,21] Inheritance
[OOP - Lec 20,21] Inheritance[OOP - Lec 20,21] Inheritance
[OOP - Lec 20,21] Inheritance
 

Más 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
 

Más 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
 

Último

microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfSanaAli374401
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...KokoStevan
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 

Último (20)

microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 

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