Program 1: - Practical File 1
Program 1: - Practical File 1
Program 1: - Practical File 1
#include<conio.h>
#include<iostream.h>
void allowance(long);
void main()
{
long salary;
clrscr();
cout<<"enter the basic salary";
cin>>salary;
allowance(salary);
getch();
}
void allowance(long sal)
{
int da,hra;
|Practical File 1
cout<<"basic salary is:\t" <<sal<<endl;
da=(sal*40)/100;
cout<<"your dearness allowance is:\t" <<da<<endl;
hra=(sal*20)/100;
cout<<"your house rent allowence is:\t"<<hra;
}
Output:
|Practical File 2
Program 2
/*wap to check whether a given no is palindrome or not*/
#include<conio.h>
#include<iostream.h>
int palindrome(int);
void main()
{
int num,print,number;
clrscr();
cout<<"enter the no to check:";
cin>>num;
number=num;
print=palindrome(num);
if(number==print)
cout<<"palindrome no:"<<print;
else
cout<<"no is not palindrome.";
getch();
}
int palindrome(int num1)
|Practical File 3
{
int rem,newno;
int new0=0;
while(num1!=0)
{
rem=num1%10;
new0=(new0*10)+rem;
num1=num1/10;
}
return(new0);
}
Output:
|Practical File 4
Program 3
/*create a function power to raise a no x to the power n.
use the concept of function overloading to calculate 5^3, x^3, x^n*/
#include<conio.h>
#include<iostream.h>
#include<math.h>
int power(int,int);
int power(int);
int power();
void main()
{
|Practical File 5
int a,b,c;
clrscr();
a=power(5,3);
b=power(3);
c=power();
cout<<"a\tb\tc"<<a<<endl<<b<<endl<<c;
getch();
}
int power(int x,int n)
{return(pow(x,n));}
int power(int n)
{return(pow(5,n));}
int power()
{
int x,n;
cout<<"enter the value of x,n";
cin>>x>>n;
return(pow(x,n));}
Output:
|Practical File 6
Program 4
/*wap to calculate volume of cube, cuboid, cylinder using function
overloading.*/
#include<conio.h>
#include<iostream.h>
#include<math.h>
int volume(int);
double volume(double,int);
int volume(int,int,int);
void main()
|Practical File 7
{
clrscr();
cout<<"volume of cube is:"<<volume(3)<<endl;
cout<<"volume of cylinder is:"<<volume(2.5,5)<<endl;
cout<<"volume of cuboid is:"<<volume(4,5,6)<<endl;
getch();
}
int volume(int a)
{ return(a*a*a);
}
|Practical File 8
Program 5
/*create a class of employee, the class should be able to accept
the employees name, employee id, basic salary, designation,
department no. The class should also have a function to calculate
gross salary i.e. basic salary + 20% bonus and then it should
display emplyee data with new salary details.*/
#include<conio.h>
#include<iostream.h>
class employee
|Practical File 9
{
char name[20];
int id;
long salary;
char post[20];
int dno;
public:
int grosssalary();
void getdata(void);
void display();
};
|Practical File 10
bonus=(salary*20)/100;
gsalary=salary+bonus;
return(gsalary);
}
void employee::display()
{
cout<<"Name of employee:"<<name<<endl;
cout<<"ID of employee:"<<id<<endl;
cout<<"salary of employee:"<<grosssalary()<<endl;
cout<<"Post of employee:"<<post<<endl;
cout<<"Designation no. of employee:"<<dno<<endl;
}
void main()
{
clrscr();
employee p;
p.getdata();
p.display();
getch();
|Practical File 11
}
Output:
Program 6
/*wap to sort a list of names in alphabetic order*/
#include <stdio.h>
#include <string.h>
#include <iostream.h>
#include <conio.h>
void sorting(char a[][50]);
|Practical File 12
void main()
{
clrscr();
int i;
char list[10][50], temp[50];
cout<<"Enter 10 names:\n";
for(i=0; i<10; i++)
{
gets(list[i]);
}
sorting(list);
cout<<"\nSorted names are:\n";
for(i=0; i<10; i++)
{
puts(list[i]);
}
getch();
}
void sorting(char a[][50])
{
|Practical File 13
int i,j;
char temp[50];
for (i = 0; i < 10; i++)
{
for (j = i+1; j < 10; j++)
{
if (strcmp(a[j], a[i]) < 0)
{
strcpy(temp,a[i]);
strcpy(a[i],a[j]);
strcpy(a[j],temp);
}
}
}
}
Output:
|Practical File 14
Program 7
|Practical File 15
/*create a class account that stores the a/c no & balance amount
of depositor. Also create a member function to assign initial value,
to deposit an amount, to withdraw an amount after checking balance
(>500) & display the balance.*/
#include<conio.h>
#include<iostream.h>
class account
{
int acc_no;
long balance;
public:
void initial_value(int a,long b)
{
acc_no=a;
balance=b;
cout<<"Current value in account is:"<<endl;
cout<<"account no"<<acc_no<<endl;
cout<<"balance is:"<<balance<<endl;
}
void deposit(void)
|Practical File 16
{
long amount;
cout<<"enter the amount to deposit:"<<endl;
cin>>amount;
cout<<balance;
balance=balance+amount;
cout<<"account no:"<<acc_no<<endl;
cout<<"balance:"<<balance<<endl;
}
void withdraw()
{
long amount0;
if(balance<=500)
cout<<"insufficient cash"<<endl;
else
{
cout<<"enter the amount to withdraw:"<<endl;
cin>>amount0;
balance=balance-amount0;
display();
|Practical File 17
}}
void display()
{
cout<<"account no:"<<acc_no<<endl;
cout<<"balance:"<<balance<<endl;
}
};
void main()
{
int choice;
clrscr();
account a,b;
a.initial_value(1234,2000);
cout<<"press 1. for deposit"<<endl;
cout<<"press 2. for withdraw:"<<endl;
cin>>choice;
if (choice==1)
a.deposit();
else
a.withdraw();
|Practical File 18
getch();
}
Output:
|Practical File 19
Program 8
/*wap to illustrate static data variable.*/
#include<conio.h>
#include<iostream.h>
class purchase
{ static int item;
int count;
public:
void getdata(int a)
{ count=a;
item++;
}
void getitem()
{ cout<<"count:";
cout<<item<<"\n";
}};
int purchase ::item;
void main()
|Practical File 20
{ clrscr();
purchase a,b,c;
a.getitem();
b.getitem();
c.getitem();
a.getdata(100);
b.getdata(200);
c.getdata(300);
cout<<"data after reading:"<<endl;
a.getitem();
b.getitem();
c.getitem();
getch();
}
Output:
|Practical File 21
Program 9
/*wap to illustrate static member function.*/
#include<conio.h>
#include<iostream.h>
class test
{ static int count;
int code;
public:
void setcode(void)
{ code=++count;
}
void showcode()
|Practical File 22
{ cout<<"object number:"<<code<<"\n";
}
static void showcount()
{ cout<<"count:"<<count<<endl;
}};
int test::count;
void main()
{
clrscr();
test t1,t2;
t1.setcode();
t2.setcode();
test::showcount();
test t3;
t3.setcode();
test::showcount();
t1.showcode();
t2.showcode();
t3.showcode();
getch();
|Practical File 23
}
Output:
Program 10
/*wap to illustrate forward declaration of class*/
#include<conio.h>
#include<iostream.h>
class fst; //forword declaration
class snd
{
int x;
public:
void setvalue(int i)
|Practical File 24
{
x=i;
}
friend void max(fst,snd);
};
class fst
{
int a;
public:
void setvalue(int i)
{
a=i;
}
friend void max(fst,snd);
};
void max(fst f,snd s)
{
if(f.a<=s.x)
cout<<"second is largest:"<<s.x;
else
|Practical File 25
cout<<"first is largest:"<<f.a;
}
void main()
{
clrscr();
fst a;
a.setvalue(30);
snd b;
b.setvalue(20);
max(a,b);
getch();
}
Output:
|Practical File 26
Program 11
|Practical File 27
/*wap to illustrate parameterised constructor.*/
#include<conio.h>
#include<iostream.h>
class integer
{
int m,n;
public:
integer(int,int);
void display()
{
cout<<"\nvalue of m is:"<<m;
cout<<"\nvalue of n is:"<<n;
}};
integer::integer(int x,int y)
{
m=x;
n=y;
}
void main()
{
|Practical File 28
integer int1(0,100);
integer int2=integer(25,75);
cout<<"\n object1"<<endl;
int1.display();
cout<<"\n object2"<<endl;
int2.display();
getch();
}
Output:
|Practical File 29
Program 12
/*wap to increment employees salaries on the basis of theirs designation.
(manager-5000, general manager-10000, CEO-20000, worker-2000),
Use emp_id, ename, designation, salary as data member and
inc_sal() as member function (use array of objects).*/
#include<conio.h>
#include<string.h>
#include<stdio.h>
#include<iostream.h>
class emp
{
int id;
char name[30],des[30];
float sal;
public:
void inc_sal();
void show(){
cout<<endl<<"ID: \t"<<id;
cout<<endl<<"Name: \t"<<name;
cout<<endl<<"Designation: \t"<<des;
cout<<endl<<"Salary: \t"<<sal;
}
};
void emp::inc_sal()
{
int cd;
cout<<endl<<"Enter Emp ID: ";
cin>>id;
cout<<endl<<"Enter Name: ";
gets(name);
A:
|Practical File 30
cout<<endl<<"Enter Designation: ";
cout<<endl<<"1: Manager";
cout<<endl<<"2: General Manager";
cout<<endl<<"3: CEO";
cout<<endl<<"4: Worker";
cin>>cd;
switch(cd)
{
case 1:sal = 5000;strcpy(des,"Manager");break;
case 2:sal = 10000;strcpy(des,"General Manager");break;
case 3:sal = 20000;strcpy(des,"CEO");break;
case 4:sal = 2000;strcpy(des,"Worker");break;
default: cout<<endl<<"Wrong Choice";goto A;
}
}
void main()
{
int i;
int n=3;
clrscr();
emp e[3];
cout<<endl<<"Enter Details of Emloyees: ";
for(i=0 ; i<n ; i++)
{
cout<<endl<<"Enter Employee: "<<i+1;
e[i].inc_sal();
}
for(i=0 ; i<n ; i++)
{
cout<<endl<<"Employee: "<<i+1;
e[i].show();
}
getch();
}
|Practical File 31
Output:
|Practical File 32
Program 13
/*wap to add two complex no using friend function.*/
#include<conio.h>
#include<iostream.h>
class complex
{
double real;
double imag;
public:
void getdata(double,double);
void display();
friend complex operator +(complex,complex);
};
complex operator +(complex cc1,complex cc2)
{
complex temp;
temp.real=cc1.real+cc2.real;
temp.imag=cc1.imag+cc2.imag;
return(temp);
|Practical File 33
}
|Practical File 34
getch();
}
Output:
|Practical File 35
Program 14
/*wap to display area of multiple rectangles using array of objects
in which each object is initialised using constructor.*/
#include <iostream.h>
#include <conio.h>
class rect
{
int l,b;
public:
rect(int x,int y)
{
l=x;
b=y;
}
void display()
{
cout<<l*b<<endl;
}
};
int main()
{
clrscr();
rect r1[3]={rect(5,10),rect(7,9),rect(4,7)};
cout<<”Area of multiple Rectangles are:”<<endl;
for(int i=0;i<3;i++)
|Practical File 36
{
cout<<endl<<”Area of rectangle”<<””<<i+1<<””<<”is:”;
r1[i].display();
}
getch();
return 0;
}
Output:
|Practical File 37
Program 15
|Practical File 38
cout<<" "<<b;
for(i=3;i<=num;i++)
{
c=a+b;
a=b;
b=c;
cout<<" "<<c;
}
}
int main()
{
clrscr();
fibonacci f;
f.get_input();
f.show_output();
getch();
return 0;
}
Output:
|Practical File 39
Program 16
/*create a class box with its data member width, height and depth.
Initialize the data member using default, parameterized and copy
constructor. Calculate volume of box.*/
#include<conio.h>
#include<iostream.h>
class box
{
int width;
int height;
int depth;
int volume;
public:
box(){};
box(int w,int h,int d)
{
width=w;
height=h;
depth=d;
volume=width*height*depth;
}
|Practical File 40
box(box &x)
{
volume=x.width*x.height*x.depth;
}
void display()
{
cout<<volume;
}
};
void main()
{
clrscr();
box b(1,2,3);
box c=b;
cout<<"volume of object b=";b.display();
cout<<endl;
cout<<"volume of object c=";c.display();
getch();
}
|Practical File 41
Output:
|Practical File 42
Program 17
/*wap to calc S.I. by making a construcor with rate of interest as default
argument.*/
#include<conio.h>
#include<iostream.h>
class si
{ int principle;
double rate;
int time;
double amount;
public:
si(){}
si(int p,int t,double r=3.0);
void display();
};
si :: si(int p,int t,double r)
{ amount=0.0;
principle=p;
rate=r;
time=t;
|Practical File 43
amount=(principle*rate*time)/100;
}
void si::display()
{ cout<<"si is:"<<amount<<endl;
}
void main()
{ si s1;
clrscr();
s1=si(1000,1);
s1.display();
getch();
}
Output:
|Practical File 44
Program 18
/*wap to illustrate operator overloading using unary - operator.*/
#include<conio.h>
#include<iostream.h>
class change
{ int a;
int b;
int c;
public:
void getdata(int,int,int);
void display();
void operator - ();
|Practical File 45
};
void change:: getdata(int x,int y,int z)
{ a=x;
b=y;
c=z; }
void change::operator-()
{ a=-a;
b=-b;
c=-c; }
void change::display()
{ cout<<"a is :"<<a<<endl;
cout<<"b is :"<<b<<endl;
cout<<"c is :"<<c<<endl; }
void main()
{ change p;
clrscr();
p.getdata(10,20,30);
p.display();
-p;
p.display();
|Practical File 46
getch(); }
Output:
Program 19
/*wap to add two comples no using operator overloading using binary
operator.*/
#include<conio.h>
#include<iostream.h>
class add
{
float a;
float b;
public:
|Practical File 47
add()
{}
add(float,float);
void display();
add operator + (add);
};
add:: add(float x,float y)
{ a=x;
b=y; }
add add::operator+(add d)
{
add temp;
temp.a=a+d.a;
temp.b=b+d.b;
return(temp);
}
void add::display()
{ cout<<a<<"+"<<b<<"i"<<endl;
}
void main()
|Practical File 48
{ add a1(1.0,2.0),a2(2.0,5.0),a3;
clrscr();
a3=a1+a2;
a3.display();
getch();
}
Output:
Program 20
/*wap to concatanate two string using operator overloading.*/
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<iostream.h>
class string
{
|Practical File 49
private:
char a[20];
public:
string()
{ strcpy(a,'\0');
}
void read()
{
gets(a);
}
void display()
{ cout<<a<<endl;
}
string operator + (string s)
{
string temp;
strcpy(temp.a,strcat(a, s.a));
return(temp);
}
};
|Practical File 50
void main()
{
string a1,a2,a3;
clrscr();
cout<<"enter the string 1";
a1.read();
cout<<"enter the string 2";
a2.read();
a3=a1+a2;
a3.display();
getch();
}
Output:
|Practical File 51
Program 21
|Practical File 52
/*wap to add two complex no using friend function*/
#include<iostream.h>
#include<conio.h>
class complex
{
double real;
double imag;
public:
complex()
{
real=0.0;
imag=0.0;
}
complex(double r, double i)
{
real=r;
imag=i;
}
void show()
{
|Practical File 53
cout<<real<<"+"<<imag<<"i"<<endl;
}
friend complex operator + (complex, complex);
};
complex operator + (complex cc1, complex cc2)
{
complex temp;
temp.real=cc1.real+cc2.real;
temp.imag=cc1.imag+cc2.imag;
return temp;
}
void main()
{
complex c1(2.0,3.0), c2(1.5,2.5);
complex c3;
c3=c1+c2;
c1.show();
c2.show();
cout<<"Sum is: ";
|Practical File 54
c3.show();
getch();
}
Output:
|Practical File 55
Program 22
/*Write a program to extent the functionality of a base class by
maintaining basic information of a student for calculating the percentage
of marks obtained in four different subjects of 100 marks each using
public inheritance.*/
#include <iostream.h>
#include <conio.h>
class student_info //base class
{
char name[20];
int roll_no;
public:
void input(); //function declaration
void output(); //function declaration
};
void student_info::input() //function defination
{
cout<<endl<<"ENTER THE INFORMATION OF THE STUDENT: ";
cout<<endl<<"ENTER THE NAME: ";
cin>>name;
cout<<"ENTER THE ROLL_NO: ";
cin>>roll_no;
}
void student_info::output()
{
cout<<endl<<endl<<”Displaying information of the student=”;
cout<<endl<<”Name=”<<name;
cout<<endl<<”Roll_No=”<<roll_no;
}
Class result:public student_info
{
|Practical File 56
int marks[4];
double perc;
public:
void get_marks();
void cal();
void display();
};
void result::get_marks()
{
input();
cout<<”Enter the marks of 4 subjects one by one=”;
for(int i=0;i<4;i++)
cin>>marks[i];
}
void result::cal()
{
int total=0;
for(int i=0;i<4;i++)
total=total+marks[i];
perc=(float)(total)/4;
}
Void result::display()
{
call();
output();
cout<<endl<<”Entered Marks is=”;
for(int i=0;i<4;i++)
cout<<””<<marks[i];
cout<<endl<<”Percentage obtained is=”<<perc;
}
int main()
{
clrscr();
result r1;
|Practical File 57
r1.get_marks();
r1.display();
getch();
return 0;
}
Output:
|Practical File 58
Program 23
A Program to inherit class student. The derived class named test should
input and read marks,display marks.Another class result should be
derived from test to show the percentage obtained by the student
in his test.The result class should the result also
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
class student
{
char name[20];
char roll_no[20];
public:
void read()
{
cout<<"ENTER THE STUDENT DETAILS: "<<endl;
cout<<endl<<"ENTER THE NAME: ";
gets(name);
cout<<"ENTER THE ENROLLMENT NO. OF THE STUDENT: ";
cin>>roll_no;
}
void show()
{
cout<<endl<<"DISPLAYING THE STUDENT DETAILS: "<<endl;
cout<<endl<<endl<<"NAME IS: "<<name;
cout<<endl<<"ENROLLMENT NO. IS: "<<roll_no;
}
|Practical File 59
};
class test:public student
{
protected:
int sub_1,sub_2;
public:
void get_marks()
{
cout<<endl<<"ENTER THE MARKS : "<<endl;
cout<<"ENTER THE MARKS IN C++: ";
cin>>sub_1;
cout<<"ENTER THE MARKS IN D.B.M.S: ";
cin>>sub_2;
}
void show_marks()
{
cout<<endl<<endl<<"MARKS OBTAINED ARE: ";
cout<<endl<<endl<<"C++: "<<sub_1;
endl<<"D.B.M.S: "<<sub_2;
}
};
class result:public test
{
private:
int total;
double per;
public:
void get_per()
{
read();
get_marks();
show();
show_marks();
total=(sub_1+sub_2);
|Practical File 60
per=(float)total/2;
cout<<endl<<"PERCENTAGE OBTAINED IS: "<<per;
}
};
int main()
{
clrscr();
result r1;
r1.get_per();
getch();
return 0;
}
Output:
|Practical File 61
Program 24
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int roll_number;
public:
void get_number(int a)
{
roll_number=a;
}
void put_number(void)
{
cout<<"roll no:"<<roll_number<<"\n";
}};
class test:public student
{
protected:
float sub1,sub2;
public:
void get_marks(float x,float y)
{
sub1=x;
|Practical File 62
sub2=y;
}
void put_marks(void)
{
cout<<"marks obtained:"<<"\n";
cout<<"sub1="<<sub1<<"\n";
cout<<"sub2="<<sub2<<"\n";
}
};
class sports
{
protected:
float score;
public:
void get_score(float s)
{
score=s;
}
void put_score(void)
{
cout<<"sports result:="<<score<<"\n\n";
}
};
class result:public test,public sports
{
float total;
public:
void display(void);
};
void result::display(void)
{
total=sub1+sub2+score;
put_number();
put_marks();
|Practical File 63
put_score();
cout<<"total score:"<<total<<"\n";
}
void main()
{
clrscr();
result stu;
stu.get_number(350);
stu.get_marks(39.5,49.5);
stu.get_score(6.0);
stu.display();
getch();
}
Output:
|Practical File 64
Program 25
#include<iostream.h>
#include<conio.h>
class BASE
{
public:
int a;
};
class Derived1 : virtual public BASE
{
public:
int b;
};
class Derived2 : virtual public BASE
{
public:
int c;
};
class Derived3 : public Derived1, public Derived2
{
public:
|Practical File 65
int total;
};
int main()
{
clrscr();
Derived3 d;
d.a=10;
d.b=20;
d.c=30;
d.total=d.a+d.b+d.c;
cout<<"object a"<<d.a<<"\t";
cout<<"object b"<<d.b<<"\t";
cout<<"object c"<<d.c;
cout<<"\tTotal: "<<d.total;
getch();
return 0;
}
Output:
|Practical File 66
Program 26
WAP to maintain the basic info of an employee and calculate his salary.
The structure of inheritance should be: EMP_BASIC: emp_code,
emp_name, address, read(), show() EMP_SALARY: basic_pay,
income_tax, net_sal, read(), show(), calculate_salary() emp_salar is
derived from emp_basic
#include<iostream.h>
#include<conio.h>
#include<string.h>
class emp_basic
{
int emp_code;
char emp_name[20];
char address[20];
public:
void read(int ec, char en[20], char ad[20])
{
emp_code=ec;
strcpy(emp_name, en);
strcpy(address, ad);
|Practical File 67
}
void show()
{ cout<<"\nEmployee code: "<<emp_code;
cout<<"\nEmployee name: "<<emp_name;
cout<<"\nAddress: "<<address;
}};
class emp_salary : public emp_basic
{
int basic_pay;
int income_tax;
int net_sal;
public:
void read(int bp, int it)
{ basic_pay=bp;
income_tax=it;
}
void calcsalary()
{
net_sal=basic_pay-income_tax;
}
void show()
{
cout<<"\nBasic pay: "<<basic_pay;
cout<<"\nIncome tax: "<<income_tax;
cout<<"\nNet salary: "<<net_sal;
}};
void main()
{ clrscr();
emp_basic ob1;
emp_salary ob2;
ob1.read(89,"ABC","NOIDA");
ob2.read(10000,500);
ob2.calcsalary();
ob1.show();
|Practical File 68
ob2.show();
getch();
}
Output:
Program 27
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
class person
{
char name[20];
char dob[30];
public:
void read(); //Function declaration
void show(); //Function declaration
};
void person::read() //Function defination
|Practical File 69
{
cout<<endl<<"ENTER THE NAME: ";
gets(name);
cout<<"ENTER THE D.O.B: ";
gets(dob);
}
void person::show() //function defination
{
cout<<endl<<"NAME Is= "<<name;
cout<<endl<<"D.O.B. Is= "<<dob;
}
class doctor:public person
{
char spec[50];
public:
void read(); //function declaration
void show(); //function declaration
};
void doctor::read() //function defination
{
person::read();
cout<<"ENTER THE SPECIALIAZATION OF THE DOCTOR: ";
gets(spec);
}
void doctor::show()
{
person::show();
cout<<endl<<"SPECIALIZATION OF THE DOCTOR IS: "<<spec;
}
class patient:public person
{
int case_no,amt_bill;
char dis_dt[30],dt_adm[30];
char disease[30];
|Practical File 70
public:
void read();
void show();
};
void patient::read()
{
person::read();
cout<<"ENTER THE CASE NO. OF THE PATIENT: ";
cin>>case_no;
cout<<"ENTER THE DISEASE: ";
gets(disease);
cout<<"ENTER THE DATE OF ADMISSION: ";
gets(dt_adm);
cout<<"ENTER THE DISCHARGE DATE: ";
gets(dis_dt);
cout<<"ENTER THE BILL TO BE PAID BY THE PATIENT: ";
cin>>amt_bill;
}
void patient::show()
{
person::show();
cout<<endl<<"CASE No. Is:= "<<case_no;
cout<<endl<<"DISEASE Is:= "<<disease;
cout<<endl<<"DATE OF ADMISSION Is:= "<<dt_adm;
cout<<endl<<"DISCHARGE Is:= "<<dis_dt;
cout<<endl<<"BILL AMOUNT Is:= "<<amt_bill;
}
int main()
{
clrscr();
doctor d;
cout<<endl<<"ENTER THE DOCTOR DETAILS: "<<endl;
d.read();
cout<<endl<<"DISPLAYING THE DOCTOR DETAILS:";
|Practical File 71
d.show();
patient p;
cout<<endl<<endl<<endl<<"ENTER THE PATIENT DETAILS:
"<<endl;
p.read();
cout<<endl<<"DISPLAYING THE PATIENT DETAILS: ";
p.show();
getch();
return 0;
}
Output:
|Practical File 72
Program 28
|Practical File 73
create a class parttime_student publically derived from 2 base classes
student and employee. STUDENT: roll_no, name, course, read(), show()
EMPLOYEE: emp_code, salary_per_hour, read(), show().
PART_TIME_STUDENT: hrs_worked, read(), show(),
calculate_salary(). PART_TIME_STUDENT is derived from student
and employee
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int roll;
char name[20],course[10];
public:
void read()
{
cout<<"Enter the roll number of a student: ";
cin>>roll;
cout<<"Enter the name of a student: ";
cin>>name;
cout<<"Enter the course: ";
cin>>course;
}
void show()
{
cout<<"Roll Number is: "<<roll;
cout<<"\nName is: "<<name;
cout<<"\nCourse is: "<<course;
}
};
|Practical File 74
class employee
{
protected:
int emp_code,sal;
public:
void read()
{
cout<<"Enter the code of an employee: ";
cin>>emp_code;
cout<<"Enter the salary per hour of an employee: ";
cin>>sal;
}
void show()
{
cout<<"\nEmployee code is: "<<emp_code;
cout<<"\nSalary per hour is: "<<sal;
}
};
|Practical File 75
void show()
{
student::show();
employee::show();
cout<<"\nTotal Salary is: "<<total;
}
};
void main()
{
clrscr();
part_time_student p;
p.read();
p.cal();
p.show();
getch();
}
Output:
|Practical File 76
Program 29
#include<iostream.h>
#include<conio.h>
#include<string.h>
class staff
{
int code;
char name[20];
public:
void getstaff(int c, char n[20])
{
code=c;
strcpy(name,n);
}
void putstaff()
{
cout<<"\nStaff code: "<<code;
cout<<"\nStaff name: "<<name;
}};
class teacher : public staff
{
char subject[20];
char publication[20];
public:
|Practical File 77
void getteacher(char s[20], char p[20])
{
strcpy(subject,s);
strcpy(publication,p);
}
void putteacher()
{
cout<<"\nSubject is: "<<subject;
cout<<"\nPublication is: "<<publication;
}};
class typist : public staff
{
int speed;
public:
void getspeed(int sp)
{
speed=sp;
}
void putspeed()
{
cout<<"\nTyping speed in wpm is: "<<speed;
}};
class officer : public staff
{
char grade;
public:
void getgrade(char g)
{
grade=g;
}
void putgrade()
{
cout<<"\nGrade: "<<grade;
}};
|Practical File 78
class regular : public typist
{
int salary;
public:
void getsalary(int sal)
{
salary=sal;
}
void putsalary()
{
cout<<"\nSalary is: "<<salary;
}};
class casual : public typist
{
int dailywage;
public:
void getwage(int w)
{
dailywage=w;
}
void putwage()
{
cout<<"\nDaily wages: "<<dailywage;
}};
void main()
{
clrscr();
teacher obt;
regular obr;
casual obc;
officer obo;
obt.getstaff(88,"ABC");
obt.getteacher("c++","S Chand");
obr.getstaff(89,"CDE");
|Practical File 79
obr.getspeed(35);
obr.getsalary(9000);
obc.getstaff(90,"ACB");
obc.getspeed(50);
obc.getwage(1000);
obo.getstaff(91,"EDC");
obo.getgrade('A');
obt.putstaff();
obt.putteacher();
cout<<endl;
obr.putstaff();
obr.putspeed();
obr.putsalary();
cout<<endl;
obc.putstaff();
obc.putspeed();
obc.putwage();
cout<<endl;
obo.putstaff();
obo.putgrade();
getch();
}
Output:
|Practical File 80
Program 30
#include<iostream.h>
#include<conio.h>
class abc
{
public:
void func(int a, int b)
|Practical File 81
{
cout<<"\nMultiplication= "<<a*b;
}
void func(int a, int b, int c)
{
cout<<"\nSum= "<<a+b+c;
}
void msg()
{
cout<<"\nBase class";
}};
class pqr : public abc
{
public:
void msg()
{
cout<<"\nDerived class";
}};
void main()
{
clrscr();
pqr ob;
ob.func(2,4); //function overloading as func() will multiply here
ob.func(1,1,1); //function overloading as func() will add here
ob.msg(); //function overriding as msg() of derived will be called
getch();
}
Output:
|Practical File 82
Program 31
How do the pure virtual function differ from normal virtual function?
Pure virtual function differ from the normal function in the following
ways:
A virtual function has a body and provide the derived class the option of
overriding the base class virtual function whereas
A pure virtual function does not have any body.So, a class in which a
pure virtual function is declared cannot be instantiated i.e we cannot
create objects of such a class.
This class is Known as the abstract class
|Practical File 83
Syntax for defining pure virtual function:-
Virtual return_type fn_name()=0;
keyword
This statement is not an assignment statement.It has just a way to inform
the compiler that function has no body.
Syntax for defining virtual functions:-
Virtual return_type fn_name()
Keyword
{
//Function body
}
Now let us understand with the help of the Program:
In the given below program we are intented to calculate the particular
area of shape such as rectangle,triangle etc. For this, we have defined a
virtual fn. cal_area() in the base
class.However it never gets called as it does not have the sufficient
information to calculate the area of a shape in the given program below.
So, it is better to keep its body blank. A better way to make this fn. as a
pure virtual function i.e
virtual void cal_area()=0;
#include<iostream.h>
#include<conio.h>
class POLY
{
protected:
int l,h;
public:
void setdata(int a, int b)
{
l=a;
h=b;
}
|Practical File 84
virtual int area(void)=0; //pure virtual function as no definition is
here
};
class rect : public POLY
{
public:
int area(void)
{
return(l*h);
}
};
class tria : public POLY
{
public:
int area(void)
{
return((l*h)/2);
}
};
int main()
{
rect r;
tria t;
r.setdata(8,2);
t.setdata(8,2);
cout<<r.area()<<endl;
cout<<t.area();
return 0;
}
Output:
|Practical File 85
Program 32
|Practical File 86
#include<iostream.h>
#include<conio.h>
class base
{
public:
int dl;
void getdata(int a)
{
dl=a;
}};
class conv : public base
{
double rs;
public:
void calc()
{
rs=dl*44.20;
}
void show()
{
cout<<"\nEquivalent amount in Rs is "<<rs;
}};
void main()
{
clrscr();
int d;
conv ob;
cout<<"\nEnter amount in dollar to convert in Rs : ";
cin>>d;
ob.getdata(d);
ob.calc();
ob.show();
getch();
}
|Practical File 87
Output:
Program 33
|Practical File 88
Create three class vehicle, car and bus in such a way that car and bus are
derived from the vehicle class .Write the class implementation where
vehicle will have only pure virtual function?
#include<iostream.h>
#include<conio.h>
class vehicle
{
protected:
char comp[10];
int mod_no;
float price;
public:
virtual void get()=0;
virtual void show()=0;
};
class car:public vehicle
{
public:
void get()
{
cout<<"Enter the company name of a car: ";
cin>>comp;
cout<<"Enter the mod_no of car: ";
cin>>mod_no;
cout<<"Enter the price of car: ";
cin>>price;
}
void show()
{
cout<<"\nCompany of car: "<<comp;
cout<<"\nModel number of car: "<<mod_no;
cout<<"\nPrice of a car: "<<price;
}};
|Practical File 89
class bus:public vehicle
{
public:
void get()
{
cout<<"Enter the company name of a bus: ";
cin>>comp;
cout<<"Enter the price of bus: ";
cin>>price;
}
void show()
{
cout<<"\nCompany of bus: "<<comp;
cout<<"\nPrice of a bus: "<<price;
}};
void main()
{
clrscr();
car c;
vehicle *v;
v=&c;
v->get();
v->show();
cout<<"\n\n";
bus b;
v=&b;
v->get();
v->show();
getch();
}
Output:
|Practical File 90
|Practical File 91
Program 34
#include<fstream.h>
#include<iostream.h>
/*
dos
fcopy d:\aa.txt d:\aaa.txt
output:
file copied
*/
|Practical File 92
Program 35
#include<conio.h>
#include<iostream.h>
class ABC;
class XYZ
{
int x;
public:
void setvalue(int i)
{
x=i;
}
friend void max(XYZ, ABC); //function friend to class XYZ
};
class ABC
{
int a;
public:
void setvalue(int i)
{
a=i;
}
friend void max(XYZ, ABC); //function friend to class ABC
};
void max(XYZ m, ABC n)
{
if(m.x>n.a)
cout<<m.x;
else
|Practical File 93
cout<<n.a;
}
void main()
{
clrscr();
ABC p;
p.setvalue(10);
XYZ q;
q.setvalue(20);
max(q,p); //friend func max() as a bridge (accessible by objects of
both class)
getch();
}
Output:
|Practical File 94