JSPM Narhe Technical Campus, Narhe Subject: Object Oriented Programming
JSPM Narhe Technical Campus, Narhe Subject: Object Oriented Programming
Objective :
The objective of this assignment is to learn the concepts of inheritance.
Theory:
Inheritance:
One of the most important concepts in object-oriented programming is that of
inheritance.
Inheritance allows us todefine 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.
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.
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
Class body;
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
Access Control and Inheritance:
A derived class can access all the non-private members of its base class. Thus base-
class members that should not be accessible to the member functions of derived
classes should be declared private in the base class.
We can summarize the different access types according to - who can access them, in the
following way:
Access public Protected private
Same class Yes yes
A derived class inherits all base class methods with the followingexceptions:
v
/ Constructors, destructors and copy constructors of the base class. Overloaded
operators of the base class.
v/ The friend functions of the base class.
Type of Inheritance:
When deriving a class from a base class, the base class may be inherited through
public, protected or private inheritance.
A derived class with only one base class is called single inheritance. And one with
several base classes is called multiple inheritance.
On the other hand the traits of one class may be inherited by more than one class.
This process is known as hierarchical inheritance.
The mechanism of deriving a class from another derived class is known as Multilevel
inheritance.
Hierarchical inheritance
Hybrid inheritance
Multilevel
Single Inheritance
Multiple Inheritance
Inheritance
Input:
Data in the form of name and age of a person.
Out ut:
Program displays information of a person.
References:
Text Books:
1. E Balagurusamy, "Object Oriented Programming Using C++ and JAVA", Tata
McGraw-
Hill
2. Herbert Schildt , " The Complete Reference TO C++" Tata McGraw-Hill,3RD Edition.
Reference books:
1. Bjarne Stroustrup, "C++ Programming Language", Pearson Education
2.Robert Lafore, "Object-Oriented Programming in C++ 't,Pearson Education
India , (4th Edition)
3. YeshwantKanetkar "Let us C++", BPB Publications
Conclusion:
#include <iostream>
#include <string>
using namespace std;
class doctor{
protected:
int age_d;
string name_d;
public:
void get_d()
{
std::cout<< "enter name and age of doctor\n";
cin>>name_d>>age_d;
}
};
class engg
{protected:
int age_e;
string name_e;
public:
void get_e()
{
std::cout<< "enter name and age of engineer\n";
cin>>name_e>>age_e;
}
};
class database:publicdoctor,publicengg
{
public:
void display(){
std::cout<<"engineer\n";
std::cout<<"name\n"<<"\t"<<"age\n";
std::cout<<name_e<<"\t"<<age_e<<endl;
std::cout<<"doctor:-\n";
std::cout<<name_d<<"\t"<<age_d<<endl;
}
};
int main()
{
database j;
j.get_e();
j.get_d();
j.display();
return 0;
}
OUTPUT: