Friday, 2 February 2018

Object Oriented Programming: Number 04


No: 04

1. Experiment vision: To write a program on the concept of Multiple and Multilevel inheritance.
2. OBJECTIVE: Program to implement multiple and multilevel inheritance in C++.
3.1 THEORY:
  • Inheritance
  • One of the most important concepts in object-oriented programming is that of inheritance. Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class.
  • The idea of inheritance implements the is a relationship. For example, mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as well and so on.
  • Base & Derived Classes
  • A class can be derived from more than one classes, which means it can inherit data and functions from multiple base classes. To define a derived class, we use a class derivation list to specify the base class(es). A class derivation list names one or more base classes and has the form:
class derived-class: access-specifier base-class;
  • Where access-specifier is one of public, protected, or private, and base-class is the name of a previously defined class. If the access-specifier is not used, then it is private by default.
  • Multilevel Inheritance
  • The mechanism of deriving a class from another derived class is known as multilevel inheritance.


3.2PROGRAM:


1) Following is an example which makes use of multilevel inheritance:
#include <iostream>
using namespace std;
class student
{
protected:
int rollno;
char name[20];
public:
void getinfo()
{ cout<<"\nEnter info about student";
cin>>rollno>>name;
}
void putinfo()
{ cout<<"\nrollno="<<rollno<<"\nname="<<name;
}
};
class test:public student
{
protected:
int sub1,sub2;
public:
void getmarks(int x,int y)
{
sub1=x;
sub2=y;
}
void putmarks()
{
cout<<"\nsub1="<<sub1<<"\nsub2="<<sub2;
}
};
class result:public test
{
int total;
public:
void display()
{
putinfo();
putmarks();
total=sub1+sub2;
cout<<"\n Total="<<total;
} };
int main()
{ result r;
r.getinfo();
r.getmarks(27,28);
r.display();
return 0;
}
3.3 OUTPUT:


4.1 THEORY:
  • Multiple inheritances
  • A class can inherit the attributes of two or more classes. This is known as multiple inheritances. Multiple inheritances allow us to join the features of a number of existing classes as a starting point for defining new classes. It is like a child inheriting the physical features of one parent and the brainpower of another.


4.2 PROGRAM:
2) Following is an example which makes use of multiple inheritance.


#include <iostream>
using namespace std;
class B
{
protected:
float b;
public:
void getb()
{
cout<<"\n Enter basic salary=";
cin>>b;
}
};
class DA
{
protected:
float da;
public:
void getda()
{
cout<<"\nEnter DA=";
cin>>da;
}
};
class HRA
{
protected:
float hra;
public:
void gethra()
{
cout<<"\nEnter HRA=";
cin>>hra;
}
};
class Gross:public B,public DA,public HRA
{
private:
float gs;
public:
void getgs()
{
gs=b+da+hra;
cout<<"\nGross salary="<<gs;
}
};
int main()
{
Gross g;
g.getb();
g.getda();
g.gethra();
g.getgs();
return 0;
}

4.3. OUTPUT:
5. conclusion:
Thus a C++ program by using multiple and multilevel 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 provides reusability of existing class. It saves the programmer time to design same code again and again. So indirectly saves money.
9. Discussion Questions:
1) What Is Inheritance& It’s Types?
____________________________________________________________________________________________________________________________________________________________


2) Differentiate Between Multiple & Multilevel Inheritance.
____________________________________________________________________________________________________________________________________________________________
3) What is single level inheritance? Explain with example.
____________________________________________________________________________________________________________________________________________________________
4) What are the advantages of inheritance?
____________________________________________________________________________________________________________________________________________________________
5) Explain the ambiguity in multiple 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.