 Inheritance is a mechanism of reusing and extending existing c
l
a
s
s
e
s
without modifying them, thus producing hierarchical relationships
betweenthem.Inheritanceis almostlike embedding anobjectinto aclass.
Type of Inheritance:
✓ Single inheritance
✓ Multi-level inheritance
✓ Multiple inheritance
✓ Hierarchical Inheritance
✓ Hybrid Inheritance
2. Explain different visibility modes used in inheritance.
Name-Anant Ambadas Patil
Roll no-20IF232
Unit- III : Extending classes using
inheritance
1. Define Inheritance and enlist its types.
 Private visibility mode
When a base class is inherited with private visibility mode the public and
protected members of the base class become ‘private’ members of the derived
class
protected visibility mode
When a base class is inherited with protected visibility mode the protected and
public members of the base class become ‘protected members ‘ of the derived
class
public visibility mode
When a base class is inherited with public visibility mode , the protected
members of the base class will be inherited as protected members of the
derived class and the public members of the base class will be inherited as
public members of the derived class.
In this inheritance, a derived class is created from a single baseclass.
In the given example, Class A is the parent class and Class B is the child class
since Class B inherits the features and behavior of the parent class A.
3. State different types of inheritance with diagram.
 Single inheritance
Multi-level inheritance
In this inheritance, a derived class is created from another derived class. In the given
example, class c inherits the properties and behavior of class B and class B
inherits the properties and behavior of class B. So, here A is the parent class of B
and class B is the parent class of C. So, here class C implicitly inherits the properties
and behavior of class A along with Class B i.e there is a multilevel of inheritance.
In this inheritance, a derived class is created from more than one base class. This
inheritance is not supported by .NET Languages like C#, F# etc. and Java Language.
In the given example, class c inherits the properties and behavior of class B and class
A at same level. So, here A and Class B both are the parent classes for Class C.
In this inheritance, more than one derived classes are created from a single base class
and futher child classes act as parent classes for more than one child classes.
In the given example, class A has two childs class B and class D. Further, class B
and class C both are having two childs - class D and E; class F and G respectively.
Multiple inheritance
Hierarchical Inheritance
This is combination of more than one inheritance. Hence, it may be a combination
of Multilevel and Multiple inheritance or Hierarchical and Multilevel inheritance or
Hierarchical and Multipath inheritance or Hierarchical, Multilevel and Multiple
inheritance.
Since .NET Languages like C#, F# etc. does not support multiple and multipath
inheritance. Hence hybrid inheritance with a combination of multiple or multipath
inheritances is not supported by .NET Languages.
Hybrid inheritance
4. Write a program to implement single inheritance from following figure,
accept and display data for 2 objects.
#include<iostream>
using namespace std;
class Student
{
protected:
int rollno;
char name[20];
public:
void get_stud_info()
{
cout<<"n Enter Student RollNo:";
cin>>rollno;
cout<<"n Enter Student Name:";
cin>>name;
}
void disp_stud_info()
{
cout<<"nRoll No:"<<rollno;
cout<<"nName:"<<name;
}
};
class Marks:public Student
{
protected:
int m1,m2,m3,total;
float percentage;
public:
void get_test_marks()
{
cout<<"n Enter class Test-1 Marks:";
cin>>m1;
cout<<"n Enter Class Test-2 Marks:";
cin>>m2;
cout<<"n Enter Class Test-3 Marks:";
cin>>m3;
total=m1+m2+m3;
cout<<"nTotal Marks : "<<total;
percentage=total/3;
cout<<"nPercentage : "<<percentage;
}
void disp_test_marks()
{
cout<<"nClass Test-1 Marks:"<<m1;
cout<<"nClass Test-2 Marks:"<<m2;
cout<<"nClass Test-3 Marks:"<<m3;
cout<<"nTotal Marks :"<<total;
cout<<"nPercentage :"<<percentage;
}
};
int main()
{
Marks m1;
m1.get_stud_info();
m1.get_test_marks();
cout<<"n**********************STUDENT INFORMATION**********************";
m1.disp_stud_info();
m1.disp_test_marks();
return 0;
}
5. Write a program to demonstrate constructor in derived class.
#include <iostream.h>
class alpha
(
private:
int x;
public:
alpha(int i)
{
x = i;
cout << "n alpha initialized n";
}
void show_x()
{
cout << "n x = "<<x;
}
);
class beta
(
private:
float y;
public:
beta(float j)
{
y = j;
cout << "n beta initialized n";
}
void show_y()
{
cout << "n y = "<<y;
}
);
class gamma : public beta, public alpha
(
private:
int n,m;
public:
gamma(int a, float b, int c, int d):: alpha(a), beta(b)
{
m = c;
n = d;
cout << "n gamma initialized n";
}
void show_mn()
{
cout << "n m = "<<m;
cout << "n n = "<<n;
}
);
void main()
{
gamma g(5, 7.65, 30, 100);
cout << "n";
g.show_x();
g.show_y();
g.show_mn();
}
6. Differentiate between multiple inheritance and multilevel inheritance.
S.NO Single inheritance Multiple inheritance
1. Single inheritance is one in which
the derived class inherits the
single base class.
Whereas multiple inheritance is one in
which the derived class acquires two or
more base classes.
2. In single inheritance, the derived
class uses the features of the
single base class.
While in multiple inheritance, the derived
class uses the joint features of the inherited
base classes.
3. Single inheritance requires small
run time as compared to multiple
inheritance due to less overhead.
While multiple inheritance requires more run
time time as compared to single inheritance
due to more overhead.
4. Single inheritance is a lot of close
to specialization.
In contrast, multiple inheritance is a lot of
close to generalization.
5. Single inheritance is implemented
as Class DerivedClass_name :
access_specifier Base_Class{};.
While multiple inheritance is implemented
as Class DerivedClass_name :
access_specifier Base_Class1,
access_specifier Base_Class2, ….{}; .
6. Single inheritance is simple in
comparison to the multiple
inheritance.
While multiple inheritance is complex in
comparison to the single inheritance.
7. Single inheritance can be
implemented in any programming
language.
C++ supports multiple inheritance but
multiple inheritance can’t be implemented in
any programming language(C#, Java
doesn’t support multiple inheritance).
6. Write a program that illustrates multilevel inheritance.
#include<iostream>
using namespace std;
class person
{
protected:
char name[20], gender[20];
int age;
public:
void get_person_info()
{
cout<<"n Enter Name:";
cin>>name;
cout<<"n Enter Gender:";
cin>>gender;
cout<<"n Enter Age:";
cin>>age;
}
void disp_person_info()
{
cout<<"nName :"<<name;
cout<<"nGender:"<<gender;
cout<<"nAge :"<<age;
}
};
class employee:public person
{
protected:
int emp_id;
char company[20];
float salary;
public:
void get_data()
{
cout<<"n Enter Employee ID:";
cin>>emp_id;
cout<<"n Enter Company Name:";
cin>>company;
cout<<"nEnter Salary";
cin>>salary;
}
void disp_data()
{
cout<<"nEmployee ID:"<<emp_id;
cout<<"nCompany :"<<company;
cout<<"nSalary :"<<salary;
}
};
class programmer:public employee
{
protected:
int no_of_prog_lang_known;
public:
void get_info()
{
cout<<"nProgramming Language known:";
cin>>no_of_prog_lang_known;
}
void disp_info()
{
cout<<"nNo. of Programming Language Known :"<<no_of_prog_lang_known;
}
};
int main()
{
programmer p;
p.get_person_info();
p.get_data();
p.get_info();
p.disp_person_info();
p.disp_data();
p.disp_info();
return 0;
}
8. Describe multiple inheritances with suitable example.
 Multiple inheritance occurs when a class inherits from more than one b
a
s
e
class. So the class can inherit features from multiple base classes using
multiple inheritance. This is an important feature of object oriented
programming languages such as C++.
A diagram that demonstrates multiple inheritance is given below −
9. Explain hybrid inheritance with example.
 Hybrid Inheritance is implemented by combining more than one type of
inheritance. For example: Combining Hierarchical inheritance and Multiple
Inheritance.
Below image shows the combination of hierarchical and multiple
inheritance:

OOP Assign No.03(AP).pdf

  • 1.
     Inheritance isa mechanism of reusing and extending existing c l a s s e s without modifying them, thus producing hierarchical relationships betweenthem.Inheritanceis almostlike embedding anobjectinto aclass. Type of Inheritance: ✓ Single inheritance ✓ Multi-level inheritance ✓ Multiple inheritance ✓ Hierarchical Inheritance ✓ Hybrid Inheritance 2. Explain different visibility modes used in inheritance. Name-Anant Ambadas Patil Roll no-20IF232 Unit- III : Extending classes using inheritance 1. Define Inheritance and enlist its types.  Private visibility mode When a base class is inherited with private visibility mode the public and protected members of the base class become ‘private’ members of the derived class protected visibility mode When a base class is inherited with protected visibility mode the protected and
  • 2.
    public members ofthe base class become ‘protected members ‘ of the derived class public visibility mode When a base class is inherited with public visibility mode , the protected members of the base class will be inherited as protected members of the derived class and the public members of the base class will be inherited as public members of the derived class.
  • 3.
    In this inheritance,a derived class is created from a single baseclass. In the given example, Class A is the parent class and Class B is the child class since Class B inherits the features and behavior of the parent class A. 3. State different types of inheritance with diagram.  Single inheritance Multi-level inheritance In this inheritance, a derived class is created from another derived class. In the given example, class c inherits the properties and behavior of class B and class B inherits the properties and behavior of class B. So, here A is the parent class of B and class B is the parent class of C. So, here class C implicitly inherits the properties and behavior of class A along with Class B i.e there is a multilevel of inheritance.
  • 4.
    In this inheritance,a derived class is created from more than one base class. This inheritance is not supported by .NET Languages like C#, F# etc. and Java Language. In the given example, class c inherits the properties and behavior of class B and class A at same level. So, here A and Class B both are the parent classes for Class C. In this inheritance, more than one derived classes are created from a single base class and futher child classes act as parent classes for more than one child classes. In the given example, class A has two childs class B and class D. Further, class B and class C both are having two childs - class D and E; class F and G respectively. Multiple inheritance Hierarchical Inheritance
  • 5.
    This is combinationof more than one inheritance. Hence, it may be a combination of Multilevel and Multiple inheritance or Hierarchical and Multilevel inheritance or Hierarchical and Multipath inheritance or Hierarchical, Multilevel and Multiple inheritance. Since .NET Languages like C#, F# etc. does not support multiple and multipath inheritance. Hence hybrid inheritance with a combination of multiple or multipath inheritances is not supported by .NET Languages. Hybrid inheritance 4. Write a program to implement single inheritance from following figure, accept and display data for 2 objects. #include<iostream> using namespace std; class Student { protected: int rollno; char name[20]; public:
  • 6.
    void get_stud_info() { cout<<"n EnterStudent RollNo:"; cin>>rollno; cout<<"n Enter Student Name:"; cin>>name; } void disp_stud_info() { cout<<"nRoll No:"<<rollno; cout<<"nName:"<<name; } }; class Marks:public Student { protected: int m1,m2,m3,total; float percentage; public: void get_test_marks() { cout<<"n Enter class Test-1 Marks:"; cin>>m1; cout<<"n Enter Class Test-2 Marks:"; cin>>m2; cout<<"n Enter Class Test-3 Marks:"; cin>>m3;
  • 7.
    total=m1+m2+m3; cout<<"nTotal Marks :"<<total; percentage=total/3; cout<<"nPercentage : "<<percentage; } void disp_test_marks() { cout<<"nClass Test-1 Marks:"<<m1; cout<<"nClass Test-2 Marks:"<<m2; cout<<"nClass Test-3 Marks:"<<m3; cout<<"nTotal Marks :"<<total; cout<<"nPercentage :"<<percentage; } }; int main() { Marks m1; m1.get_stud_info(); m1.get_test_marks(); cout<<"n**********************STUDENT INFORMATION**********************"; m1.disp_stud_info(); m1.disp_test_marks(); return 0; }
  • 8.
    5. Write aprogram to demonstrate constructor in derived class. #include <iostream.h> class alpha ( private: int x; public: alpha(int i) { x = i; cout << "n alpha initialized n"; } void show_x() { cout << "n x = "<<x; } ); class beta ( private: float y; public: beta(float j) { y = j;
  • 9.
    cout << "nbeta initialized n"; } void show_y() { cout << "n y = "<<y; } ); class gamma : public beta, public alpha ( private: int n,m; public: gamma(int a, float b, int c, int d):: alpha(a), beta(b) { m = c; n = d; cout << "n gamma initialized n"; } void show_mn() { cout << "n m = "<<m; cout << "n n = "<<n; } );
  • 10.
    void main() { gamma g(5,7.65, 30, 100); cout << "n"; g.show_x(); g.show_y(); g.show_mn(); }
  • 11.
    6. Differentiate betweenmultiple inheritance and multilevel inheritance. S.NO Single inheritance Multiple inheritance 1. Single inheritance is one in which the derived class inherits the single base class. Whereas multiple inheritance is one in which the derived class acquires two or more base classes. 2. In single inheritance, the derived class uses the features of the single base class. While in multiple inheritance, the derived class uses the joint features of the inherited base classes. 3. Single inheritance requires small run time as compared to multiple inheritance due to less overhead. While multiple inheritance requires more run time time as compared to single inheritance due to more overhead. 4. Single inheritance is a lot of close to specialization. In contrast, multiple inheritance is a lot of close to generalization. 5. Single inheritance is implemented as Class DerivedClass_name : access_specifier Base_Class{};. While multiple inheritance is implemented as Class DerivedClass_name : access_specifier Base_Class1, access_specifier Base_Class2, ….{}; . 6. Single inheritance is simple in comparison to the multiple inheritance. While multiple inheritance is complex in comparison to the single inheritance. 7. Single inheritance can be implemented in any programming language. C++ supports multiple inheritance but multiple inheritance can’t be implemented in any programming language(C#, Java doesn’t support multiple inheritance). 6. Write a program that illustrates multilevel inheritance. #include<iostream> using namespace std; class person { protected: char name[20], gender[20]; int age; public:
  • 12.
    void get_person_info() { cout<<"n EnterName:"; cin>>name; cout<<"n Enter Gender:"; cin>>gender; cout<<"n Enter Age:"; cin>>age; } void disp_person_info() { cout<<"nName :"<<name; cout<<"nGender:"<<gender; cout<<"nAge :"<<age; } }; class employee:public person { protected: int emp_id; char company[20]; float salary; public: void get_data() { cout<<"n Enter Employee ID:"; cin>>emp_id;
  • 13.
    cout<<"n Enter CompanyName:"; cin>>company; cout<<"nEnter Salary"; cin>>salary; } void disp_data() { cout<<"nEmployee ID:"<<emp_id; cout<<"nCompany :"<<company; cout<<"nSalary :"<<salary; } }; class programmer:public employee { protected: int no_of_prog_lang_known; public: void get_info() { cout<<"nProgramming Language known:"; cin>>no_of_prog_lang_known; } void disp_info() { cout<<"nNo. of Programming Language Known :"<<no_of_prog_lang_known; } };
  • 14.
    int main() { programmer p; p.get_person_info(); p.get_data(); p.get_info(); p.disp_person_info(); p.disp_data(); p.disp_info(); return0; } 8. Describe multiple inheritances with suitable example.  Multiple inheritance occurs when a class inherits from more than one b a s e class. So the class can inherit features from multiple base classes using multiple inheritance. This is an important feature of object oriented programming languages such as C++. A diagram that demonstrates multiple inheritance is given below −
  • 15.
    9. Explain hybridinheritance with example.  Hybrid Inheritance is implemented by combining more than one type of inheritance. For example: Combining Hierarchical inheritance and Multiple Inheritance. Below image shows the combination of hierarchical and multiple inheritance: