PMT...c c c j cc c c 3 (cc c c c c cˇ c cˇ c c cˇ c cˇ c c c
C++
Transcript of C++
Program 1
/*wap to display dearness allowance, house rent allowance,
gross salary if basic salary is entered by user.
DA is 40% of basic salary and house rent allowance is 20% of basic salary.*/
#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)
{
|Practical File 1
int da,hra;
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(); |Practical File 3
}
int palindrome(int num1)
{
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);
}
double volume(double r,int h)
{
return(3.14*r*r*h);
}
int volume(int l,int b,int h)
{
return(l*h*b);
}
Output: |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>
|Practical File 9
class employee
{
char name[20];
int id;
long salary;
char post[20];
int dno;
public:
int grosssalary();
void getdata(void);
void display();
};
void employee:: getdata(void)
{
cout<<"enter the name,id,salary,post,dno:";
cin>>name>>id>>salary>>post>>dno;
}
int employee:: grosssalary()
{ |Practical File 10
int bonus,gsalary;
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(); |Practical File 11
getch();
}
Output:
Program 6
/*wap to sort a list of names in alphabetic order*/
#include <stdio.h>
#include <string.h>
#include <iostream.h>
|Practical File 12
#include <conio.h>
void sorting(char a[][50]);
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();
} |Practical File 13
void sorting(char a[][50])
{
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
|Practical File 15
Program 7
/*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;
} |Practical File 16
void deposit(void)
{
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; |Practical File 17
display();
}}
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 |Practical File 18
a.withdraw();
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";
}}; |Practical File 20
int purchase ::item;
void main()
{ 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;
}
|Practical File 22
void showcode()
{ 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(); |Practical File 23
getch();
}
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:
|Practical File 24
void setvalue(int i)
{
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; |Practical File 25
else
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
|Practical File 27
Program 11
/*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;
|Practical File 30
cout<<endl<<"Enter Emp ID: "; cin>>id; cout<<endl<<"Enter Name: "; gets(name); A: 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++)
|Practical File 31
{ cout<<endl<<"Employee: "<<i+1; e[i].show(); } getch();}
Output:
|Practical File 32
|Practical File 33
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 34
}
void complex :: getdata(double r,double i)
{
real=r;
imag=i;
}
void complex::display()
{
cout<<real <<"+"<<imag<<"i"<<endl;
}
void main()
{
clrscr();
complex c1,c2;
c1.getdata(2.0,3.0);
c2.getdata(3.0,4.0);
complex c3;
c3=c1+c2;
c3.display(); |Practical File 35
getch();
}
Output:
|Practical File 36
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)};
|Practical File 37
cout<<”Area of multiple Rectangles are:”<<endl;
for(int i=0;i<3;i++)
{
cout<<endl<<”Area of rectangle”<<””<<i+1<<””<<”is:”;
r1[i].display();
}
getch();
return 0;
}
Output:
|Practical File 38
Program 15
/*wap to generate fibonacci series using classes and objects.*/
#include <iostream.h> #include <conio.h> class fibonacci { int a,b,c,num,i; public: void get_input(); //Function Declaration void show_output(); //Function Declaration }; void fibonacci::get_input() { cout<<endl<<endl<<"How many fibonacci numbers you want? "; cin>>num; } void fibonacci::show_output() { cout<<endl<<"The fibonacci numbers are: "; a=0;
|Practical File 39
b=1; cout<<" "<<a;
|Practical File 40
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 41
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;
|Practical File 42
volume=width*height*depth;
}
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(); |Practical File 43
getch();
}
Output:
|Practical File 44
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)
|Practical File 45
{ amount=0.0;
principle=p;
rate=r;
time=t;
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 46
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();
|Practical File 47
void operator - ();
};
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; |Practical File 48
p.display();
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;
|Practical File 49
public:
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;
} |Practical File 50
void main()
{ 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 51
{
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 52
};
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 53
|Practical File 54
Program 21
/*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 55
{
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(); |Practical File 56
cout<<"Sum is: ";
c3.show();
getch();
}
Output:
|Practical File 57
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=”;
|Practical File 58
cout<<endl<<”Name=”<<name;cout<<endl<<”Roll_No=”<<roll_no;}Class result:public student_info{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];
|Practical File 59
cout<<endl<<”Percentage obtained is=”<<perc;}int main(){clrscr();result r1;r1.get_marks();r1.display();getch();return 0;}
Output:
|Practical File 60
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: ";
|Practical File 61
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; } }; 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;
|Practical File 62
double per; public: void get_per() { read(); get_marks(); show(); show_marks(); total=(sub_1+sub_2); per=(float)total/2; cout<<endl<<"PERCENTAGE OBTAINED IS: "<<per; } }; int main() { clrscr();result r1; r1.get_per(); getch(); return 0;}
Output:
|Practical File 63
Program 24
/* Write a program for hybrid inheritance */
#include<iostream.h>#include<conio.h>class student
|Practical File 64
{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; sub2=y;}void put_marks(void){ cout<<"marks obtained:"<<"\n"; cout<<"sub1="<<sub1<<"\n"; cout<<"sub2="<<sub2<<"\n";}};class sports{protected: float score;
|Practical File 65
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(); 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();
|Practical File 66
}
Output:
Program 25 |Practical File 67
/*To illustrate the working of virtual base class*/
#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: int total;};int main(){ clrscr(); Derived3 d; d.a=10; d.b=20; d.c=30; d.total=d.a+d.b+d.c;
|Practical File 68
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 69
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); } void show() { cout<<"\nEmployee code: "<<emp_code; cout<<"\nEmployee name: "<<emp_name; cout<<"\nAddress: "<<address; }};class emp_salary : public emp_basic{
|Practical File 70
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(); ob2.show(); getch();}Output:
|Practical File 71
Program 27
Design an application for hospital management for this create the following classes: person:name,dob doctor:specialization patient:case_no,disease, date_of_admission,discharge_date,bill. Use the Concept of hierarchical inheritence.
#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 { cout<<endl<<"ENTER THE NAME: "; gets(name); cout<<"ENTER THE D.O.B: ";
|Practical File 72
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]; public: void read(); void show();
|Practical File 73
}; 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:"; d.show(); patient p;
|Practical File 74
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 75
|Practical File 76
Program 28
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 77
};
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; }};
class part_time_student:public student,public employee{ float hours,total; public: void read() { student::read(); employee::read(); cout<<"Enter the number of hours: "; cin>>hours; } void cal()
|Practical File 78
{ total=sal*hours; } 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 79
Program 29
An educational institute wishes to maintain a database of its employees, the database of its employees . The databse is divided into of classes. STAFF: code, name TEACHER: subject,publication TYPIST: speed OFFICER:grade teacher, typist, officer are deriveed from STAFF... REGULAR: salary, CASUAL: daily_wages.. REGULAR and CASUAL are derived from TYPIST.
#include<iostream.h>#include<conio.h>#include<string.h>class staff{ int code; char name[20]; public:
|Practical File 80
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: 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; }
|Practical File 81
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; }};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)
|Practical File 82
{ 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"); 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();
|Practical File 83
obc.putwage(); cout<<endl; obo.putstaff(); obo.putgrade(); getch();}
Output:
Program 30
What is the difference between the overloaded and overridden function?
The main difference between overloading and overriding is that in overloading we can use same function name with different parameters for multiple times for different taskswith on a class. and overriding means we can use same name function name
|Practical File 84
with same parameters of the base class in the derived class. this is also called as reusability of code in the programme.
#include<iostream.h>#include<conio.h>class abc{public: void func(int a, int b) { 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
|Practical File 85
ob.msg(); //function overriding as msg() of derived will be called getch();}
Output:
Program 31
How do the pure virtual function differ from normal virtual function?
|Practical File 86
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 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 baseclass.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{
|Practical File 87
protected: int l,h; public:
void setdata(int a, int b) { l=a; h=b; } 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);
|Practical File 88
cout<<r.area()<<endl; cout<<t.area(); return 0;}
Output:
|Practical File 89
Program 32
Write a program to convert given amount in dollars to rupees by defining conversion function in the destination class?(Assume 1$=Rs. 44.20)
#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();
|Practical File 90
int d; conv ob; cout<<"\nEnter amount in dollar to convert in Rs : "; cin>>d; ob.getdata(d); ob.calc(); ob.show(); getch();}
Output:
|Practical File 91
Program 33
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: ";
|Practical File 92
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; }};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();
|Practical File 93
cout<<"\n\n"; bus b; v=&b; v->get(); v->show(); getch();}
Output:
|Practical File 94
Program 34
Write a program to copy the contents of one file to another file?
#include<fstream.h>#include<iostream.h>
void main(int argv,char *argc[]){ifstream src;ofstream des;src.open(argc[1],ios::binary);des.open(argc[2],ios::binary);des<<src.rdbuf();des.close();src.close();cout<<"file copied";}
/*
|Practical File 95
dosfcopy d:\aa.txt d:\aaa.txt
output:file copied*/
Program 35
Write a program to demonstrate how a friend function acts as a bridge between two classes?
#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
|Practical File 96
{ 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 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 97
|Practical File 98