Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

Unit 3 Inheritance

oop

Uploaded by

Anupama Mishra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Unit 3 Inheritance

oop

Uploaded by

Anupama Mishra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

UNIT 3

INHERITANCE:
The mechanism of deriving a new class from an old one is called inheritance or derivation. The old class is
referred to as the base class and the new one is called the derived class or sub class. The derived class inherits
some or all of the traits from the base class. A class can also inherit properties from more than one class or
from more than one level. Reusability is an important feature of OOP. A derived class can be defined by
specifying its relationship with the base class in addition to its own details.

class derived-class-name : visibility-mode base-class-name


{
………
………
}

The colon indicates that the derived class name is derived from the base-class-name. the visibility mode is
optional and if present, may be either private or protected or public. The default is private. Visibility mode
specifies whether the features of the base class are privately derived or publicly derived.

class ABC : private XYZ


{
members of ABC;
};
//private derivation

class ABC:public XYZ


{
members of ABC;
};
//public derivation
class ABC:protected XYZ {
// protected derivationmembers of ABC;
};
class ABC:XYZ //private by default
{
members of ABC;
};

When a base class is privately inherited by a derived class, public members of the base class can only be
accessed by the member functions of the derived class. Private members of base class are inaccessible to the
objects of the derived class
When a base class is protected inherited by a derived class, public members of the base class can only be
accessed by the member functions of the derived class. Private members of base class are inaccessible to the
objects of the derived class. If private members of base class are to be inherited to derived class then declare
them as protected. When the base class is publicly inherited, public members of the base class is publicly
inherited, public members of the base class become public members of the derived class and therefore they are
accessible to the objects of the derived class. In both the cases, the private members are not inherited and
therefore, the private members of a base class will never become the members of its derived class.
In inheritance, some of the base class data elements and member functions are inherited into the derived class.
We can add our own data and member functions and thus extend the functionality of the base class.
Inheritance, when used to modify and extend the capability of the existing classes, becomes a very powerful
tool for incremental program development
Types of Inheritance:
1.Single Inheritance
2.Multi level Inheritance
3.Mutiple Inheritance
4.Hybrid inheritance
5. Hierarchical Inheritance

1. SINGLE INHERITANCE: One derived class inherits from only one base class. It is the most simplest
form of Inheritance.

#include<iostream>
using namespace std;
class A
{
public:
int a,b;
void get()
{
cout<<"Enter any two Integer values"<<endl;
cin>>a>>b;
}
};
class B:public A
{
int c;
public:
void add()
{
c=a+b;
cout<<a<<"+"<<b<<"="<<c;
}
};
int main()
{
B b;
b.get();
b.add();
}
Output:
Enter any two Integer values
12
1+2=3

2.MULTILEVEL INHERITANCE: In this type of inheritance the derived class inherits from a
class, which in turn inherits from some other class. The Super class for one, is sub class for the other.

#include<iostream.h>
class A
{
public:
int a,b;
void get()
{
cout<<"Enter any two Integer values"<<endl;
cin>>a>>b;
}
};
class B:public A
{
public:
int c;
void add()
{
c=a+b;
}
};
class C:public B
{
public:
void show()
{
cout<<a<<"+"<<b<<"="<<c;
}
};
int main()
{
C c;
c.get();
c.add();
c.show();
}

Output:
Enter any two Integer values
12 14
12+14=26
3.Multiple Inheritance:In this type of inheritance a single derived class may inherit from two or more than
two base classes.

Syntax:
class D : visibility A, visibility B,….
{
………………
}
#include<iostream.h>
class A
{
public:
int a;
void getA()
{
cout<<"Enter an Integer value"<<endl;
cin>>a;
}
};
class B
{
public:
int b;
void getB()
{
cout<<"Enter an Integer value"<<endl;
cin>>b;
}
};
class C:public A,public B
{
public:
int c;
void add()
{
c=a+b;
cout<<a<<"+"<<b<<"="<<c<<endl;
}
};
int main()
{
C obj;
obj.getA();
obj.getB();
obj.add();
}
Enter an Integer
value 12 Enter an
Integer value 13
12+13=25

4.Hybrid Inheritance: Hybrid inheritance is combination of two or more inheritances such


as single,multiple,multilevel or Hierarchical inheritances.
A
BC
D
#include<iostream.h>
class arithmetic
{
protected:
int num1, num2;
public:
void getdata()
{
cout<<"For Addition:";
cout<<"\nEnter the first number: ";
cin>>num1;
cout<<"\nEnter the second number: ";
cin>>num2;
}
};
class plus:public arithmetic
{
protected:
int sum;
public:
void add()
{
sum=num1+num2;
}
};
class minus
{
protected:
int n1,n2,diff;
public:
void sub()
{
cout<<"\nFor Subtraction:";
cout<<"\nEnter the first number: ";
cin>>n1;
cout<<"\nEnter the second number: ";
cin>>n2;
diff=n1-n2;
}
};
class result:public plus, public minus
{
public:
void display()
{
cout<<"\nSum of "<<num1<<" and "<<num2<<"= "<<sum;
cout<<"\nDifference of "<<n1<<" and "<<n2<<"= "<<diff;
}
};
int main()
{
result z;
z.getdata();
z.add();
z.sub();
z.display();
}
For Addition:
Enter the first number: 1
Enter the second number: 2
For Subtraction:
Enter the first number: 3
Enter the second number: 4
Sum of 1 and 2= 3
Difference of 3 and 4= -1

5.Hierarchical Inheritance:- Inheriting is a method of inheritance where one or more derived


classes is derived from common base class.
A
BCD
#include<iostream.h>
class A //Base Class
{
public:
int a,b;
void getnumber()
{
cout<<"\n\nEnter Number :\t";
cin>>a;
}
};
class B : public A //Derived Class 1
{
public:
void square()
{
getnumber(); //Call Base class property
cout<<"\n\n\tSquare of the number :\t"<<(a*a);
}
};
class C :public A //Derived Class 2
{
public:
void cube()
{
getnumber(); //Call Base class property
cout<<"\n\n\tCube of the number :::\t"<<(a*a*a);
}
};
int main()
{
B b1; //b1 is object of Derived class 1
b1.square(); //call member function of class B
C c1; //c1 is object of Derived class 2
c1.cube(); //call member function of class C
}
Enter Number : 2
Square of the number : 4
Enter Number : 3
Cube of the number ::: 27

Pointers to Objects:Pointers to objects are useful for creating objects at run time. To access members
arrow operator ( ) and de referencing operator or indirection (*) are used.

Declaration of pointer:

className*ptr
ex:
item *obj;
Here obj is a pointer to object of type item.
class item
{
int code;
float price;
public:
void getdata(int a,float b)
{
code=a;
price=b;
}
void show()
{
cout<<”code:”<<code<<”\n”<<”Price:”<<price<<endl;
}
};

Declaration of object and pointer to class item:


item obj;
item *ptr=&obj;
The member can be accessed as follow.
a) Accessing members using dot
operator obj.getdata(101,77.7);
obj.show();
b)using pointer
ptr->getdata(101,77.7);
ptr->show();
c)Using de referencing operator and dot operator
(*ptr).getdata(101,77.7);
(*ptr).show();

Creating array of objects using pointer:

item *ptr=new item[10];

Above declaration creates memory space for an array of 10 objects of type item.

#include<iostream.h>
class item
{
int code;
float price;
public:
void getdata(int a,float b)
{
code=a;
price=b;
}
void show()
{
cout<<code<<"\t"<<price<<endl;
}
};
int main()
{
int n;
int cd;
float pri;
cout<<"Enter number of objects to be created:";
cin>>n;
item *ptr=new item[n];
item *p;
p=ptr;
for(int i=0;i<n;i++)
{
cout<<"Enter data for object"<<i+1;
cout<<"\nEnter Code:";cin>>cd;
cout<<"Enter price:";cin>>pri;
p->getdata(cd,pri);
p++;
}
p=ptr;
cout<<"Data in various objects are "<<endl;
cout<<"Sno\tCode\tPrice\n";
for(i=0;i<n;i++)
{
cout<<i+1<<"\t";
ptr->show();
ptr++;
}
return 0;
}

Pointers to Derived Classes: Pointers can be declared to derived class. It can be used to access
members of base class and derived class. A base class pointer can also be used to point to object of derived
class but it can access only members that are inherited from base class.
#include<iostream.h>
class base
{
public:
int a;
void get_a(int x)
{
a=x;
}
void display_a()
{
cout<<"In base"<<"\n"<<"a="<<a<<endl;
}
};
class derived:public base
{
int b;
public:
void get_ab(int x,int y)
{
a=x;
b=y;
}
void display_ab()
{
cout<<"In Derived "<<"\n"<<"a="<<a<<"\nb="<<b<<endl;
}
};
int main()
{
base b;
base *bptr;
bptr=&b;//points to the object of base class
bptr->get_a(100);
bptr->display_a();
derived d;
derived *dptr;
dptr=&d;//points to the object of derived class
dptr->get_a(400);
dptr->display_a();
dptr->get_ab(300,200);
dptr->display_ab();
bptr=&d;//points to the object of derived class
bptr->get_a(400);
bptr->display_a();
return 0;
}
Output:
In base
a=100
In base
a=400
In Derived
a=300
b=200
In base
a=400

You might also like