0% found this document useful (0 votes)
2 views8 pages

Oops Using C++ Notes Unit - 3

The document explains the concept of inheritance in object-oriented programming, detailing how a derived class can inherit properties from a base class. It outlines different types of inheritance, including single, multilevel, multiple, hybrid, and hierarchical inheritance, along with code examples for each type. Additionally, it discusses visibility modes (private, protected, public) and the accessibility of base class members in derived classes.

Uploaded by

ka3867885
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views8 pages

Oops Using C++ Notes Unit - 3

The document explains the concept of inheritance in object-oriented programming, detailing how a derived class can inherit properties from a base class. It outlines different types of inheritance, including single, multilevel, multiple, hybrid, and hierarchical inheritance, along with code examples for each type. Additionally, it discusses visibility modes (private, protected, public) and the accessibility of base class members in derived classes.

Uploaded by

ka3867885
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

UNIT-III

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 //


{ privatederivation
Members of ABC;
};
Class ABC:public XYZ
{
Members of ABC;
};
//public derivation
Class ABC:protected XYZ{
//protected derivation members 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 membes of base class are inaccessible to
the objects of the derived class
When a base class is protected inherited bya derived class, public members of the base class can only be accessed
by the member functions of the derived class.private membes 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
Typesof Inheritance:
1. Single Inheritance
2. Multilevel Inheritance
3. Mutiple Inheritance
4. Hybrid inheritance
5. Hierarchical Inheritance.

1.SINGLE INHERITANCE:one derived class inherits from only one baseclass.It is the most simplest form of
Inheritance.
//Base class
A
//Derivedclass

#include<iostream> B
Using namespacestd;
class A
{
public:
int a,b;
voidget()
{
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 1 2
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 subclass for the other.

#include<iostream.h>
class A
{
public:
int a,b;
voidget()
{
cout<<"Enter any two Integer values"<<endl;
cin>>a>>b;
}
};
Class B:public A
{
public:
int c;
void add()
{
c=a+b;
}
};
Class C:publicB
{
public:
void show()
{
cout<<a<<"+"<<b<<"="<<c;
}
};
int main()
{
C c;
c.get();
c.add();
c.show();
}
Output:
Enter any two Integer values 1214
12+14=26
3.MultipleInheritance:In this type of inheritance a single derived class may inherit from two or more than two
base classes.
A B

C
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();
}
OUTPUT:
Enter an Integer value12Enteran

Integervalue13
12+13=25
4.HybridInheritance:Hybrid inheritance is combination of two or more inheritances such as
single,multiple,multilevel or Hierarchical inheritances.

B C

D
#include<iostream.h>
class arithmetic
{
protected:
int num1,num2;
public:
Void get data()
{
cout<<"For Addition:"; cout<<"\nEnter the first number:"; cin>>num1;
cout<<"\nEnter these cond 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<<"\n Enter the first number:"; cin>>n1;
cout<<"\nEnter these cond number:";

cin>>n2; diff=n1-n2;
}
};
Class result:public plus,public minus
{
public:
void display()
{
cout<<"\nSumof"<<num1<<"and"<<num2<<"="<<sum;
cout<<"\nDifference of "<<n1<<" and "<<n2<<"= "<<diff;
}
};
int main()
{
resultz;
z.getdata();
z.add();
z.sub();
z.display();
}
OUTPUT:
ForAddition:
Enter the first number: 1 Enter these cond number:2
For Subtraction:
Enter the first number: 3 Enter the second number:4
Sum of1 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.

B C D

#include<iostream.h>
class A //Base Class
{
public:
int a,b;
void get number()
{

cout<<"\n\nEnter Number:\t"; cin>>a;


}
};
Class B:public A //DerivedClass1
{
public:
void square()
{
Get number(); //Call Base class property
cout<<"\n\n\tSquareofthenumber:\t"<<(a*a);
}
};
Class C:public A //DerivedClass2
{
public:
void cube()
{
Get number(); //Call Base class property
cout<<"\n\n\tCubeofthenumber:::\t"<<(a*a*a);
}
};
int main()
{
B b1; //b1 is object of Derived class 1
b1.square(); //callmemberfunctionofclassB
C c1; //c1 is object of Derived class 2
c1.cube(); //call member function of class C
}
OUTPUT:
Enter Number:2
Square of the number:4
Enter Number : 3
Cubeofthe number::: 27

You might also like