0% found this document useful (0 votes)
61 views19 pages

P.E.S College of Science: / To Accept and Print Employee Information - Name, Designation & Basic Salary

The document contains C++ code to demonstrate inheritance and polymorphism. It defines a base class called GF with member variables for dimensions and pure virtual functions to calculate area and display output. It derives two classes - Triangle and Rectangle from the base class. The derived classes override the pure virtual functions to calculate area specific to their shape and display the output. The main function creates objects of the derived classes, calls the base class function to input dimensions and the derived class functions to calculate and display area.

Uploaded by

chandan gowda
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)
61 views19 pages

P.E.S College of Science: / To Accept and Print Employee Information - Name, Designation & Basic Salary

The document contains C++ code to demonstrate inheritance and polymorphism. It defines a base class called GF with member variables for dimensions and pure virtual functions to calculate area and display output. It derives two classes - Triangle and Rectangle from the base class. The derived classes override the pure virtual functions to calculate area specific to their shape and display the output. The main function creates objects of the derived classes, calls the base class function to input dimensions and the derived class functions to calculate and display area.

Uploaded by

chandan gowda
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/ 19

P.E.

S COLLEGE OF SCIENCE

/*To accept and print employee information – name,


designation & basic salary*/
#include<iostream.h>
#include<conio.h>
class employee
{
char name[10];
char desig[20];
float salary;
public:
void getdata();
void display();
};
void employee::getdata(void)
{
cout<<"enter name:";
cin>>name;
cout<<"enter desig:";
cin>>desig;
cout<<"basic salary:";
cin>>salary;
}
void employee::display(void)
{
cout<<"\nname:"<<name;
cout<<"\ndesig:"<<desig;
cout<<"\nsalary:"<<salary;
}

void main()
{
employee e;
clrscr();
e.getdata();
e.display();
getch();
}

1
P.E.S COLLEGE OF SCIENCE

Output:
enter name:Raj
enter desig:software-developer
basic salary:26000

name:Raj
desig:software-developer
salary:26000

2
P.E.S COLLEGE OF SCIENCE

/*-To accept and print three employees information –


name, designation & basic pay*/
#include<iostream.h>
#include<conio.h>
class emp
{
char name[30];
char designation[30];
float salary;
public:
void getdata (void);
void putdata (void);
};
void emp::getdata(void)
{
cout<<"enter name:";
cin>> name;
cout<< "enter designation:";
cin>>designation ;
cout<<"enter salary:";
cin>>salary ;
}
void emp::putdata(void)
{
cout<<"name:"<<name<<"\n";
cout<<"designation:"<<designation<<"\n";
cout<<"salary :"<<salary<<"\n";
}
const int size=3;
void main()
{
clrscr();
emp employee[size];
for (int i=0;i<size;i++)
{
cout<<"\n details of employee"<<i+1<<"\n";
employee[i].getdata();
}
cout<<"\n";
for(i=0;i<size;i++)
{
cout<<"\n employee"<<i+1<<"\n";
employee[i].putdata();
}
getch();
}

3
P.E.S COLLEGE OF SCIENCE

Output:
details of employee1
Enter name:mantu
Enter designation:software-tester
Enter salary:50000

details of employee2
Enter name:raj
Enter designation:web-designer
Enter salary:40000
details of employee3
Enter name:harish
Enter designation:software-developer
Enter salary:35000

Employee1
name:mantu
designation:software-tester
salary:50000

Employee2
name:raj
designation:web-designer
salary:40000

Employee3
name:harish
designation:software-developer
salary:35000

4
P.E.S COLLEGE OF SCIENCE

/*Create a class to hold information for a customer


about his SB account & Current account in a bank. Using
friend function, find the total balance of both the
accounts*/
#include<iostream.h>
#include<conio.h>
class current;
class savings
{
public:
char name[20];
char acc_no[20];
char acc_type[20];
float balance;

void getdata(void);
friend void total(savings,current);
};
class current
{
public:
char name[20];
char acc_no[20];
char acc_type[10];
float balance;

void getdata(void);
friend void total(savings,current);
};
void savings::getdata(void)
{
cout<<"name:";
cin>>name;
cout<<"acc no:";
cin>>acc_no;
cout<<"acc type:savings"<<"\n";
cout<<"balance:";
cin>>balance;
}
void current::getdata(void)
{
cout<<"name:";
cin>>name;
cout<<"acc no:";
cin>>acc_no;

5
P.E.S COLLEGE OF SCIENCE

cout<<"acc type:current"<<"\n";
cout<<"balance:";
cin>>balance;
}

void total(savings m,current n)


{
float t;
t=m.balance+n.balance;
cout<<"total balance:";
cout<<t;
}
void main()
{
clrscr();
savings abc;
abc.getdata();
current xyz;
xyz.getdata();
total(abc,xyz);
getch();
}

Output:

Name:mantu
acc no:8901273491
acc type:savings
balance:60000

name:mantu
acc no:8912374234
acc type:current
balance:20000

total balance:80000

6
P.E.S COLLEGE OF SCIENCE

/*Create a Bank database (which includes customer name,


Account type, Account number & balance amount) using
i)Constructors ii) destructors iii) default
constructors v) input/output functions for 5 peoples.*/
#include<iostream.h>
#include<conio.h>
class account
{
char name[20];
float accnum;
char acctype[20];
float balance;
public:
account();
~account();
void getdata();
void putdata();
};
account::account()
{
clrscr();
cout<<"banking system using constructor & destructor"<<endl;
}
account::~account()
{
cout<<"database has been deleted"<<endl;
}
void account::putdata()
{
cout<<"enter customer name:";
cin>>name;
cout<<"enter acc no:";
cin>>accnum;
cout<<"enter acc type:";
cin>>acctype;
cout<<"enter balance:";
cin>>balance;
}
void account::getdata(void)
{
cout<<"customer:"<<name<<endl;
cout<<"acc no:"<<accnum<<endl;
cout<<"acc type:"<<acctype<<endl;
cout<<"balance:"<<balance<<endl;
}

7
P.E.S COLLEGE OF SCIENCE

const int size=5;


void main()
{
account accn[size];
for(int i=0;i<size;i++)
{
cout<<"details of accounts"<<i+1<<"\n";
accn[i].putdata();
}
cout<<"\n";
for(i=0;i<size;i++)
{
cout<<"account"<<i+1<<"\n";
accn[i].getdata();
}
getch();
}

Output:

details of accounts1
enter customer name:harish
enter acc no:912376
enter acc type:savings
enter balance:4500

details of accounts2
enter customer name:mantu
enter acc no:78912
enter acc type:current
enter balance:120000

details of accounts3
enter customer name:raj
enter acc no:712369
enter acc type:savings
enter balance:65000

details of accounts4
enter customer name:p2
enter acc no:789234
enter acc type:savings
enter balance:4300

details of accounts5
enter customer name:mrba

8
P.E.S COLLEGE OF SCIENCE

enter acc no:896762


enter acc type:current
enter balance:2540000

account1
customer:harish
acc no:912376
acc type:savings
balance:4500

account2
customer:mantu
acc no:78912
acc type:current
balance:120000

account3
customer:raj
acc no:712369
acc type:savings
balance:65000

account4
customer:p2
acc no:789234
acc type:savings
balance:4300

account5
customer:mrba
acc no:896762
acc type:current
balance:2540000

database has been deleted

database has been deleted

database has been deleted

database has been deleted

database has been deleted

9
P.E.S COLLEGE OF SCIENCE

/*Program to concatenate & compare 2 strings by


overloading + and == operators*/
#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
{
public:
char t[20];
int operator==(string c)
{
if(!strcmp(t,c.t))
return 1;
return 0;
}
};
string operator+(string a,string b);
void main()
{
string a,b,c,t;
clrscr();
cout<<"enter first string"<<endl;
cin>>a.t;
cout<<"enter second string"<<endl;
cin>>b.t;
c=a+b;
cout<<"the concatinated string is "<<endl;
cout<<c.t;
cout<<"\n";
if(a==b)
//here
cout<<"Strigs are Equal";

else

cout<<"Strings are Not Equal";

getch();
}
string operator+(string a,string b)
{
string c;
strcat(a.t,b.t);
strcpy(c.t,a.t);
return c;
}

10
P.E.S COLLEGE OF SCIENCE

Output:

Enter first string


qwerty
Enter secound string
qwerty
The concatenated string is
qwertyqwerty
Strings are equal

11
P.E.S COLLEGE OF SCIENCE

/*Interactive Program to find sum, difference and


product of two complex numbers by overloading +, -,*
operators*/

#include<iostream.h>
#include<conio.h>
class complex
{
float x;
float y;
public:
complex(){}
complex(float real,float imag)
{
x=real;
y=imag;
}
complex operator+(complex);
complex operator*(complex);
complex operator-(complex);
void display(void);
};
complex complex::operator+(complex sum)
{
complex temp;
temp.x=x+sum.x;
temp.y=y+sum.y;
return(temp);
}
complex complex::operator*(complex pro)
{
complex temp;
temp.x=x*pro.x;
temp.y=y*pro.y;
return(temp);
}
complex complex::operator-(complex diff)
{
complex temp;
temp.x=x-diff.x;
temp.y=y-diff.y;
return(temp);
}
void complex::display(void)
{
cout<<x<<"+j"<<"("<< y <<")"<<"\n";

12
P.E.S COLLEGE OF SCIENCE

}
void main()
{
complex summ,differ,product,c1,c2;
clrscr();
c1=complex(2.5,3.5);
c2=complex(1.6,2.7);
summ=c1+c2;
product=c1*c2;
differ=c1-c2;
cout<<"c1=";
c1.display();
cout<<"c2=";
c2.display();
cout<<"sum=";
summ.display();
cout<<"product=";
product.display();
cout<<"difference=";
differ.display();
getch();
}

Output:

C1=2.5+j(3.5)
C2=1.6+j(2.7)
Sum=4.1+j(6.2)
Product=4+j(9.45)
Difference=0.9+j(0.8)

13
P.E.S COLLEGE OF SCIENCE

/*To create a base class called G-F with a, b & area as


its public members. Two new classes namely Triangle &
Rectangle are inherited from the base class. The base
class contains two member functions called get() &
display() to read the data & print them.Develop an
interactive program to accept dimensions and
appropriate area.*/

#include<iostream.h>
#include<conio.h>
class GF
{
public:

double height;
double base;
void getdata()
{
cout<<"enter base:";
cin>>base;
cout<<"enter height:";
cin>>height;
}
virtual double calcarea()=0;
virtual void display()=0;

};
class triangle:public GF
{
public:
double triangle_area;
double calcarea()
{
triangle_area=0.5*base*height;
return 0;
}

void display()
{
cout<<"area of triangle="<<triangle_area<<endl;
}

};
class rectangle:public GF
{
public:

14
P.E.S COLLEGE OF SCIENCE

double rectangle_area;
double calcarea()
{
rectangle_area=base*height;
return 0;
}
void display()
{
cout<<"area of rectangle="<<rectangle_area<<endl;
}
};
void main()
{
clrscr();
triangle t1;
rectangle r1;
t1.getdata();
t1.calcarea();
t1.display();
r1.getdata();
r1.calcarea();
r1.display();
getch();
}

Output:

Enter base:3
Enter height:2
Area of triangle:3
Enter base:4
Enter height:6
Area of rectangle=24

15
P.E.S COLLEGE OF SCIENCE

/*Program to create a base class for stack and


implement push and pop operations. Include derived
class to check for stack criteria (stack is full and
stack is empty)*/

#include<iostream.h>
#include<conio.h>
#include<process.h>
const int max=4;
class stack
{
protected:
int stk[max];
int top;
public:
stack()
{
top=-1;
}
void push()
{
int n;
cout<<"enter item to be insert:";
cin>>n;
top++;
stk[top]=n;
cout<<"\n"<<n<<" inserted."<<endl;
}
int pop()
{
int m;
m=stk[top];
top--;
cout<<"\n"<<m<<"popped"<<endl;
return(m);
}
void show();
};
void stack::show()
{
if(top==-1)
cout<<"stack is empty";
else
{
cout<<"stack elements:";
for(int i=top;i>=0;i--)

16
P.E.S COLLEGE OF SCIENCE

cout<<"\n"<<stk[i];
}
}
class stack_status:public stack
{
public:
void push()
{
if(top==max-1)
cout<<"\n stack over flow"<<endl;
else
{
stack::push();
if(top==max-1)
cout<<"\n stack is full"<<endl;
}
}
int pop()
{
int m;
if(top==-1)
{
cout<<"\n stack under flow"<<endl;
return 0;
}
else
{
m=stack::pop();
if(top==-1)
cout<<"\n stack is empty";
return(m);
}
}
};
void main()
{
clrscr();
int num,s,choice;
stack_status S;
do
{
cout<<"\n\n......stack operation.....";
cout<<"\n 1.push";
cout<<"\n 2.pop";
cout<<"\n 3.display";
cout<<"\n 4.exit";
cout<<"\n enter your choice:";

17
P.E.S COLLEGE OF SCIENCE

cin>>choice;
switch(choice)
{
case 1:
S.push();
break;
case 2:
S.pop();
break;
case 3:
S.show();
break;
case 4:
exit(0);
default:
cout<<"\n wrong choice!!";
}
}while(choice!=4);
getch();
}

Output:

......stack operation......
1.push
2.pop
3.display
4.exit
enter your choice:1
enter item to be insert:12

12 inserted.

......stack operation......
1.push
2.pop
3.display
4.exit
enter your choice:1
enter item to be insert:23

23 inserted.

......stack operation......

18
P.E.S COLLEGE OF SCIENCE

1.push
2.pop
3.display
4.exit
enter your choice:3
stack elements:
23
12

......stack operation......
1.push
2.pop
3.display
4.exit
enter your choice:2

23 popped

......stack operation......
1.push
2.pop
3.display
4.exit
enter your choice:3
stack elements:
12

......stack operation......
1.push
2.pop
3.display
4.exit
enter your choice:4

19

You might also like