OOP LAB 6 (Inheritance)
OOP LAB 6 (Inheritance)
OOP LAB 6 (Inheritance)
INHERITANCE
Concepts of 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. This also provides an opportunity to reuse
the code functionality and fast implementation time.
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.
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:
int main(void)
{
Rectangle Rect;
Rect.setWidth(5);
Rect.setHeight(7);
return 0;
}
When the above code is compiled and executed, it produces the following result:
Total area: ?
Types of Inheritance
Single Inheritance
In this type of inheritance one derived class inherits from only one base class. It is the
simplest form of Inheritance.
When dealing with inherited classes, things get a bit more complex.
First, there is a third access specifier that we have yet to talk about because it’s only
useful in an inheritance context. The protected access specifier restricts access to
member functions of the same class, or those of derived classes.
class Base
{
public:
int m_nPublic; // can be accessed by anybody
private:
int m_nPrivate; // can only be accessed by Base member functions (but not derived
classes)
protected:
int m_nProtected; // can be accessed by Base member functions, or derived classes.
};
m_nProtected = 3; // allowed: can access protected base members from derived class
};
int main()
{
Base cBase;
cBase.m_nPublic = 1; // allowed: can access public members from outside class cBase.m_nPrivate
= 2; // not allowed: can not access private members from outside class
cBase.m_nProtected = 3; // not allowed: can not access protected members from outside
class
}
RUN the above code and resolve the error………..
Multiple Inheritance
In this type of inheritance a single derived class may inherit from two or more than two base
classes.
#include<iostream.h>
#include<conio.h>
class student
protected:
int rno,m1,m2;
public:
void get()
cin>>rno;
cin>>m1>>m2;
};
class sports
protected:
void getsm()
};
int tot,avg;
public:
void display()
tot=(m1+m2+sm); avg=tot/3;
}
};
void main()
OUTPUT??
Task 1:
Create a class Employee with the following attributes and member functions:
Get function to take input of human resource allowance, basic salary and profitable fund.
Sample Input Sample Output
Task 2:
To write a c++ program to get student details, total marks & average marks using Multilevel
Inheritance.
Declare the base class student.
Define three data members rollno, marks1 & marks2.
Declare and define the function get() to get input for data members rollno, marks1 & marks2.
Declare the other class arts.
Define one data member arts marks.
Declare and define the function getam() to input the arts mark.
Create the class result derived from student and arts.
Declare and define the function display() to find out the total and average.
Declare the derived class object & call the functions get(),getam() and display().
Stop the program.
Task 3:
Write a class LocalPhone that contains an attribute phone to store a local telephone number. The
class contains member functions to input and display phone number. Write a child class
NatPhone for national phone numbers that inherits LocPhone class. It additionally contains an
attribute to store city code. It also contains member functions to input and show the city code.
Write another class IntPhone for international phone numbers that inherit NatPhone class. It
additionally contains an attribute to store country code. It also contains member functions to
input and show the country code. Test these classes from main() by creating objects of derived
classes and testing functions in a way that clear concept of multi-level Inheritance.
Task 4:
Make A simple Billing System, with help of Multilevel Inheritance.
PRACTICAL-V
INHERITANCE(Continue---)
Hierarchical Inheritance
In this type of inheritance, multiple derived classes inherits from a single base class.
Virtual Base Class
An ambiguity can arise when several paths exist to a class from the same base class. This means that
a child class could have duplicate sets of members inherited from a single base class.
C++ solves this issue by introducing a virtual base class. When a class is made virtual, necessary care
is taken so that the duplication is avoided regardless of the number of paths that exist to the child
class.
#include<iostream.h>
#include<conio.h> class
base
{
public:
virtual void show()
{
cout<<"\n Base class show:";
}
void display()
{
cout<<"\n Base class display:" ;
}
};
class drive:public base
{
public:
void display()
{
cout<<"\n Drive class display:";
}
void show()
{
cout<<"\n Drive class show:";
}
};
void main()
{
clrscr();
base obj1;
base *p;
cout<<"\n\t P points to base:\n" ;
p=&obj1;
p->display();
p->show();
cout<<"\n\n\t P points to drive:\n";
drive obj2;
p=&obj2;
p->display();
p->show();
getch();
}
Output:
Abstract Classes
An abstract class is one that is not used to create objects. An abstract class is designed only to act
as a base class.
1. Abstract class cannot be instantiated, but pointers and refrences of Abstract class
type can be created.
2. Abstract class can have normal functions and variables along with a pure virtual
function.
3. Abstract classes are mainly used for Upcasting, so that its derived classes can use
its interface.
4. Classes inheriting an Abstract Class must implement all pure virtual functions, or
else they will become Abstract too.
Example of Abstract Class
class Base //Abstract base class
{
public:
virtual void show() = 0; //Pure Virtual Function
};
int main()
{
Base obj; //Compile Time Error
Base *b;
Derived d;
b = &d;
b->show();
}
A derived-class constructor must call a base-class constructor before it does anything else. If the base
class constructor is not called explicitly in a derived-class constructor, the system will try to invoke
the base-class's no-arg constructor. But, remember, a base class will have a no-arg constructor only if
you provide one, or, by default, if you have defined no constructors at all for the base class. In what
follows, we will first use the User class example to illustrate the more common case, which is that of
a derived-class constructor making an explicit call to a base-class constructor.
void main()
{
gamma g(5, 7.65, 30, 100);
cout << "n";
g.show_x();
g.show_y();
g.show_mn();
}
Output:
Task 1:
1) Define a class called student that has the following data members:
- int student number
- string student name
- double student average
The following member functions:
- Constructor that initialize the data members with default values.
- set and get functions for each data member
- Print function to print the values of data members.
Define a class called graduatestudent that inherits data members and functions
from the class student, and then declare the following data members :
- int level
- int year
Member functions:
- constructor -set and get functions for each data member
- Print function.
Define a class called master that inherits data members and functions from graduatestudent class,
and then declare the following data member:
- int newid.
Member function:
- constructor
- set and get function for the data member
- Print function.
Write a driver program that:
- Declare object of type student with suitable values then print it
- Declare object of type master with your information then print it.