C++ INHERITANCE Upated
C++ INHERITANCE Upated
C++ INHERITANCE Upated
//main function
int main()
{
Child obj1;
return 0;
ACSC 328: OOP(C++) C++ INHERITANCE
Modes of Inheritance
Public: When the member is declared as public, it is accessible to all the functions
of the program.
•There are no restrictions on accessing public members.
•The public members of a class can be accessed from anywhere in the program
using the direct member access operator (.) with the object of that class.
Private: Access is limited to within the class definition.
•This is the default access modifier type if none is formally specified.
•They are not allowed to be accessed directly by any object or function outside the
class.
Protected: When the member is declared as protected, it is accessible within its
own class as well as the class immediately derived from it.
Access is limited to within the class definition and any class that inherits from the
class.
C++ INHERITANCE
Single inheritance
Syntax:
// base class
class Vehicle {
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};
Output:
This is a Vehicle
// sub class derived from a single base classes
class Car: public Vehicle{
};
// main function
int main()
{
// creating object of sub class will
// invoke the constructor of base classes
Car obj;
return 0;
}
class B : private A
{
public:
void display()
{
int result = mul();
cout <<"Multiplication of a and b is : "<<result<< endl;
}
};
ACSC 328: OOP(C++) C++ INHERITANCE
Multiple Inheritance
Multiple Inheritance is a feature of C++ where a class can inherit from more than one classes. i.e
one sub class is inherited from more than one base classes.
};
10
C++ INHERITANCE
class pool int main()
{ {
protected: wall1 W1;
int l,w,h; wall2 W2;
public: int area=W1.area1() +W2.area2();
pool() cout<<"SURFACE AREA="<<area;
{ return 0;
l=10; }
w=5;
h=7;
}
};
class wall1: public pool
{
public:
int area1()
{
return (2*(l*h))+(2*(h*w));
}
};
class wall2: public pool
{
public:
int area2()
{
return l*w;
}
};