Inheritance:
It is the process of joining two or more classes and
we can access the values from both classes.
Types of Inheritance:
Benefits of inheritance:
- Software reusability
- Information hiding
- Code sharing
Syntax:
Example:
Output:
Inheritance:
It is the process of joining two or more classes and
we can access the values from both classes.
Types of Inheritance:
Benefits of inheritance:
Syntax:
class a
{
//Base
class
}
class b: public a
{
//Derived
class
}
Object creation:
Derived_class_name object_name;
object_name.function(parameter list); //If use
constructor then NO need this
Example:
#include<iostream.h>
#include<conio.h>
class item // Base class
{
public:
char item_name[10];
int qty;
float price;
double amt;
item() //Constructor
{
cout<<"\n
Enter item name : ";
cin>>item_name;
cout<<"\n
Enter number of quantity : ";
cin>>qty;
cout<<"\n
Enter item price : ";
cin>>price;
amt=qty*price;
}
};
class display : public item // Derived class
{
public:
display()
{
cout<<"\n \n
Output of Item details with total amount...........
"<<endl<<endl;
cout<<"\n
Item name : "<<item_name<<endl;
cout<<"\n
Quantity : "<<qty<<endl;
cout<<"\n
Item price : "<<price<<endl;
cout<<"\n Total
amout : "<<amt<<endl;
}
};
void main()
{
clrscr();
display d;
getch();
}
Output:
No comments:
Post a Comment