PROGRAM:PAYROLL SYSTEM USING SINGLE INHERITANCE
#include<iostream.h
#include<conio.h
class emp
{
public:
int eno;
char name[20],des[20];
void get()
{
cout<"Enter the employee number:";
cineno;
cout<"Enter the employee name:";
cin>name;
cout<"Enter the designation:";
cin>des;
}
};
class salary:public emp
{
float bp,hra,da,pf,np;
public:
void get1()
{
cout<"Enter the basic pay:";
cinbp;
cout<"Enter the Humen Resource Allowance:";
cinhra;
cout<"Enter the Dearness Allowance :";
cin>da;
cout<"Enter the Profitablity Fund:";
cinpf;
}
void calculate()
{
np=bp+hra+da-pf;
}
void display()
{
cout<eno<"\t"<name<"\t"<des<"\t"<bp<"\t"<hra<"\t"<da<"\t"<pf<"\t"<np<"\n";
}
};
void main()
{
int i,n;
char ch;
salary s[10];
clrscr();
cout<"Enter the number of employee:";
cin>n;
for(i=0;i<n;i++)
{
s[i].get();
s[i].get1();
s[i].calculate();
}
cout<"\ne_no \t e_name\t des \t bp \t hra \t da \t pf \t np \n";
for(i=0;i<n;i++)
{
s[i].display();
}
getch();
}
Output:
Enter the Number of employee:1
Enter the employee No: 150
Enter the employee Name: ram
Enter the designation: Manager
Enter the basic pay: 5000
Enter the HR allowance: 1000
Enter the Dearness allowance: 500
Enter the profitability Fund: 300
E.No E.name des BP HRA DA PF NP
150 ram Manager 5000 1000 500 300 6200
To find out the student details using multiple inheritance.
#include<iostream.h
#include<conio.h
class student
{
protected:
int rno,m1,m2;
public:
void get()
{
cout<"Enter the Roll no :";
cinrno;
cout<"Enter the two marks :";
cin>m1>m2;
}
};
class sports
{
protected:
int sm; // sm = Sports mark
public:
void getsm()
{
cout<"\nEnter the sports mark :";
cinsm;
}
};
class statement:public student,public sports
{
int tot,avg;
public:
void display()
{
tot=(m1+m2+sm);
avg=tot/3;
cout<"\n\n\tRoll No : "<rno<"\n\tTotal : "<tot;
cout<"\n\tAverage : "<avg;
}
};
void main()
{
clrscr();
statement obj;
obj.get();
obj.getsm();
obj.display();
getch();
}
Output:
Enter the Roll no: 100
Enter two marks
90
80
Enter the Sports Mark: 90
Roll No: 100
Total : 260
Average: 86.66
Multilevel Inheritance
#include<iostream.h
#include<conio.h
class top //base class
{
public :
int a;
void getdata()
{
cout<"\n\nEnter first Number :::\t";
cin>a;
}
void putdata()
{
cout<"\nFirst Number Is :::\t"<a;
}
};
//First level inheritance
class middle :public top // class middle is derived_1
{
public:
int b;
void square()
{
getdata();
b=a*a;
cout<"\n\nSquare Is :::"<b;
}
};
//Second level inheritance
class bottom :public middle // class bottom is derived_2
{
public:
int c;
void cube()
{
square();
c=b*a;
cout<"\n\nCube :::\t"<c;
}
};
int main()
{
clrscr();
bottom b1;
b1.cube();
getch();
}
Output
Enter First Number:4
Square is :16
Cube is : 64
Hierarchal Inheritance
#include<iostream.h
#include<conio.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);
cout<"\n\n\t------";
}
};
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);
cout<"\n\n\t------";
}
};
int main()
{
clrscr();
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
getch();
}
Output
Enter number : 2
Square of the number :4
Enter Number : 3
Cube of the number : 27
Hybrid Inheritance
#include<iostream.h
#include<conio.h
class A //Base class
{
public:
int l;
void len()
{
cout<"\n\nLenght :::\t";
cin>l; //Lenght is enter by user
}
};
class B :public A //Inherits property of class A
{
public:
int b,c;
void l_into_b()
{
len();
cout<"\n\nBreadth :::\t";
cin>b; //Breadth is enter by user
c=b*l; //c stores value of lenght * Breadth i.e. (l*b) .
}
};
class C
{
public:
int h;
void height()
{
cout<"\n\nHeight :::\t";
cin>h; //Height is enter by user
}
};
//Hybrid Inheritance Level
class D:public B,public C
{
public:
int res;
void result()
{
l_into_b();
height();
res=h*c; //res stores value of c*h where c=l*b and h is height which is enter by user
cout<"\n\nResult (l*b*h) :::\t"<res;
}
};
int main()
{
clrscr();
D d1;
d1.result();
getch();
}
Output
Length : 20
Breadth: 30
Height : 40
Result (l*b*h) : 24000
Function Overloading
include <iostream
void test(int);
void test(float);
void test(int, float);
int main() {
int a = 5;
float b = 5.5;
test(a);
test(b);
test(a, b);
return 0;
}
void test(int var) {
cout<"Integer number: "<varendl;
}
void test(float var){
cout<"Float number: "<varendl;
}
void test(int var1, float var2) {
cout<"Integer number: "<var1;
cout<" And float number:"<var2;
}
Output
Integer number: 5
Float number: 5.5
Integer number: 5 And float number: 5.5
Ambiguity Problem
For example, consider the following program.
#include<iostream.hclass Person {
// Data members of person
public:
Person(int x) {
cout < "Person::Person(int ) called" < endl;
}
};
class Faculty : public Person {
// data members of Faculty
public:
Faculty(int x):Person(x) {
cout<"Faculty::Faculty(int ) called"< endl;
}
};
class Student : public Person {
// data members of Student
public:
Student(int x):Person(x) {
cout<"Student::Student(int ) called"< endl;
}
};
class TA : public Faculty, public Student {
public:
TA(int x):Student(x), Faculty(x) {
cout<"TA::TA(int ) called"< endl;
}
};
int main() {
TA ta1(30);
}
Output
Person::Person(int ) called
Faculty::Faculty(int ) called
Person::Person(int ) called
Student::Student(int ) called
TA::TA(int ) called
In the above program, constructor of ‘Person’ is called two times. Destructor of ‘Person’ will also be called two times when object ‘ta1′ is destructed. So object ‘ta1′ has two copies of all members of ‘Person’, this causes ambiguities. The solution to this problem is ‘virtual’ keyword. We make the classes ‘Faculty’ and ‘Student’ as virtual base classes to avoid two copies of ‘Person’ in ‘TA’ class.
#include<iostreamclass Person {
public:
Person(int x) { cout < "Person::Person(int ) called" < endl; }
Person() { cout < "Person::Person() called" < endl; }
};
class Faculty : virtual public Person {
public:
Faculty(int x):Person(x) {
cout<"Faculty::Faculty(int ) called"< endl;
}
};
class Student : virtual public Person {
public:
Student(int x):Person(x) {
cout<"Student::Student(int ) called"< endl;
}
};
class TA : public Faculty, public Student {
public:
TA(int x):Student(x), Faculty(x) {
cout<"TA::TA(int ) called"< endl;
}
};
int main() {
TA ta1(30);
}
Output:
Person::Person() called
Faculty::Faculty(int ) called
Student::Student(int ) called
TA::TA(int ) called