Assignment No.03
Assignment No.03
Assignment No.03
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
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.
3. State different types of inheritance with diagram.
Single inheritance
In this inheritance, a derived class is created from a single base class.
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.
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.
Multiple 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.
Hierarchical Inheritance
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.
Hybrid inheritance
class Student
protected:
int rollno;
char name[20];
public:
void get_stud_info()
cin>>rollno;
cin>>name;
void disp_stud_info()
cout<<"\nRoll No:"<<rollno;
cout<<"\nName:"<<name;
};
protected:
int m1,m2,m3,total;
float percentage;
public:
void get_test_marks()
cin>>m1;
cin>>m2;
cin>>m3;
total=m1+m2+m3;
percentage=total/3;
cout<<"\nPercentage : "<<percentage;
void disp_test_marks()
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 Whereas multiple inheritance is one in
the derived class inherits the which the derived class acquires two or
single base class. more base classes.
2. In single inheritance, the derived While in multiple inheritance, the derived
class uses the features of the class uses the joint features of the inherited
single base class. base classes.
3. Single inheritance requires small While multiple inheritance requires more run
run time as compared to multiple time time as compared to single inheritance
inheritance due to less overhead. due to more overhead.
4. Single inheritance is a lot of close In contrast, multiple inheritance is a lot of
to specialization. close to generalization.
5. Single inheritance is implemented While multiple inheritance is implemented
as Class DerivedClass_name : as Class DerivedClass_name :
access_specifier Base_Class{};. access_specifier Base_Class1,
access_specifier Base_Class2, ….{}; .
6. Single inheritance is simple in While multiple inheritance is complex in
comparison to the multiple comparison to the single inheritance.
inheritance.
7. Single inheritance can be C++ supports multiple inheritance but
implemented in any programming multiple inheritance can’t be implemented in
language. any programming language(C#, Java
doesn’t support multiple inheritance).
class person
protected:
int age;
public:
void get_person_info()
cin>>name;
cin>>gender;
cin>>age;
void disp_person_info()
cout<<"\nName :"<<name;
cout<<"\nGender:"<<gender;
cout<<"\nAge :"<<age;
};
protected:
int emp_id;
char company[20];
float salary;
public:
void get_data()
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;
};
protected:
int no_of_prog_lang_known;
public:
void get_info()
cin>>no_of_prog_lang_known;
void disp_info()
};
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;