Friday, 2 February 2018

Object Oriented Programming: No. 05


 No: 5


1. Experiment vision: To write a program on the concept of Hierarchical and Hybrid
inheritance.
2. OBJECTIVE: Write a program to implement Hierarchical and Hybrid inheritance.
3.1 THEORY:
  • Hierarchical inheritance
When many subclasses inherit properties from a single base class, it is called hierarchical inheritance.
3.2 PROGRAM:
1) Following is an example which makes use of Hierarchical inheritance.
#include <iostream>
using namespace std;
class Side
{
protected:
int l;
public:
void set_values (int x)
{
l=x;
}
};
class Square: public Side
{
public:
int sq()
{
return (l *l);
}
};
class Cube:public Side
{
public:
int cub()
{
return (l *l*l);
}
};
int main ()
{
Square s;
s.set_values(10);
cout<< "The square value is::" <<s.sq() <<endl;
Cube c;
c.set_values (20);
cout<< "The cube value is::" <<c.cub() <<endl;
return 0;
}



3.3 OUTPUT:
4.1 THEORY:
  • Hybrid inheritance:
In this type of inheritance, we mix two or more type of inheritance, then such type of inheritance is known as Hybrid inheritance.


4.2 PROGRAM:
2) Following is an example which makes use of Hybrid inheritance.
#include <iostream>
using namespace std;
class student
{
protected:
int id;
char name[20];
public:
void getdata()
{
cout<<"Enter the name and id\n";
cin>>name>>id;
}
void putdata()
{
cout<<"\nname="<<name<<"\nid="<<id;
}
};
class marks:public student
{
protected:
int sub1,sub2;
public:
void getinfo()
{
cout<<"Enter marks for sub1 and sub2\n";
cin>>sub1>>sub2;
}
void putinfo()
{
cout<<"\nsub1="<<sub1<<"\nsub2="<<sub2;
}
};
class bonus
{
protected:
int b;
public:
void get_b(int x)
{
b=x;
}
void show_b()
{
cout<<"\nbonus="<<b;
}
};
class result:public marks,public bonus
{
int total;
public:
void display()
{
putdata();
putinfo();
show_b();
total=sub1+sub2+b;
cout<<"\ntotal marks="<<total;
}
};
int main()
{
result r;
r.getdata();
r.getinfo();
r.get_b(5);
r.display();
return 0;
}


4.3 OUTPUT:
5. conclusion:
Thus a C++ program by using Hierarchical and Hybrid inheritance has been executed successfully.
6. PRECAUTIONS:
1. Avoid wrong keywords.

2. Check whether all brackets are closed properly or not.

3. Take desired output.

4. In case of abnormal results, Contact the teacher/instructor, repeat the
experiment, and check the algorithm and program.

7. REMARK:
Inheritance can be used to modify a class when it is not satisfying the requirement of a particular problem. Additional members are added through inheritance to extend the capacity of class. Hybrid inheritance will be useful when we need to apply two or more type of inheritance to design a program.
8. Discussion Questions:
1) What do you mean by Hierarchical and Hybrid inheritance?
____________________________________________________________________________________________________________________________________________________________
2) Explain Hierarchical inheritance with example.
____________________________________________________________________________________________________________________________________________________________
3) Explain Hybrid inheritance with example.
____________________________________________________________________________________________________________________________________________________________
4) What are the advantages of Hybrid inheritance?
____________________________________________________________________________________________________________________________________________________________
5) Explain different visibility modes in inheritance.
____________________________________________________________________________________________________________________________________________________________

No comments:

Post a Comment

About Me

Hi, I am Prof. Amol Zade, working in the field of teaching and research from last more than 10 years. My area of interests are Artificial Intelligence, Wireless Networking, Algorithms. I have 16 International paper publications along with 4 International Conference; also 3 Books published. published on Object Oriented Programming with open source approach.