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
 
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
 

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

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...PsychoTech Services
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
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
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 

Dernier (20)

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
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
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
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"
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 

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