Home »
» Single inheritance in C++
Single inheritance:
- If one base class and one derived class in a program called “Single inheritance”.
Syntax:
class a
{
//Base class
}
class b: public a
{
//Derived class
}
Object creation:
Derived_class_name object_name;
object_name.function(parameter list);
Example:
#include<iostream.h>
#include<conio.h>
class emp_data
{
public:
int empno,sal;
char ename[10];
void get_data()
{
cout<<"\n
Enter employee id: ";
cin>>empno;
cout<<"\n
Enter employee name: ";
cin>>ename;
cout<<"\n
Enter employee salary: ";
cin>>sal;
}
};
class emp_sal:public emp_data
{
public:
void salary_increment()
{
if (sal<=5000)
{
sal=sal+1000;
cout<<"\n
Employee salary has been increased by 1000";
}
else if (sal>=5000
&& sal<=10000)
{
sal=sal+2000;
cout<<"\n
Employee salary has been increased by 2000";
}
else
{
sal=sal+3000;
cout<<"\n
Employee salary has been increased by 3000";
}
}
void display()
{
cout<<"\n \n
Employee Details.......";
cout<<"\n
Employee ID : "<<empno;
cout<<"\n
Employee name: "<<ename;
cout<<"\n
Employee sal: "<<sal;
}
};
void main()
{
clrscr();
emp_sal e;
e.get_data();
e.salary_increment();
e.display();
getch();
}
Output:
If condition:
Else if condition:
Else condition:
No comments:
Post a Comment