C++

115
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); |Practical File 1

Transcript of C++

Page 1: 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

Page 2: C++

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

Page 3: C++

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

Page 4: C++

}

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

Page 5: C++

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

Page 6: C++

{

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

Page 7: C++

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

Page 8: C++

{

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

Page 9: C++

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

Page 10: C++

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

Page 11: C++

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

Page 12: C++

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

Page 13: C++

#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

Page 14: C++

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

Page 15: C++

|Practical File 15

Page 16: C++

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

Page 17: C++

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

Page 18: C++

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

Page 19: C++

a.withdraw();

getch();

}

Output:

|Practical File 19

Page 20: C++

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

Page 21: C++

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

Page 22: C++

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

Page 23: C++

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

Page 24: C++

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

Page 25: C++

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

Page 26: C++

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

Page 27: C++

|Practical File 27

Page 28: C++

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

Page 29: C++

{

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

Page 30: C++

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

Page 31: C++

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

Page 32: C++

{ cout<<endl<<"Employee: "<<i+1; e[i].show(); } getch();}

Output:

|Practical File 32

Page 33: C++

|Practical File 33

Page 34: C++

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

Page 35: C++

}

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

Page 36: C++

getch();

}

Output:

|Practical File 36

Page 37: C++

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

Page 38: C++

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

Page 39: C++

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

Page 40: C++

b=1; cout<<" "<<a;

|Practical File 40

Page 41: C++

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

Page 42: C++

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

Page 43: C++

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

Page 44: C++

getch();

}

Output:

|Practical File 44

Page 45: C++

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

Page 46: C++

{ 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

Page 47: C++

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

Page 48: C++

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

Page 49: C++

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

Page 50: C++

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

Page 51: C++

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

Page 52: C++

{

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

Page 53: C++

};

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

Page 54: C++

|Practical File 54

Page 55: C++

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

Page 56: C++

{

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

Page 57: C++

cout<<"Sum is: ";

c3.show();

getch();

}

Output:

|Practical File 57

Page 58: C++

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

Page 59: C++

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

Page 60: C++

cout<<endl<<”Percentage obtained is=”<<perc;}int main(){clrscr();result r1;r1.get_marks();r1.display();getch();return 0;}

Output:

|Practical File 60

Page 61: C++

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

Page 62: C++

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

Page 63: C++

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

Page 64: C++

Program 24

/* Write a program for hybrid inheritance */

#include<iostream.h>#include<conio.h>class student

|Practical File 64

Page 65: C++

{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

Page 66: C++

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

Page 67: C++

}

Output:

Program 25 |Practical File 67

Page 68: C++

/*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

Page 69: 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 69

Page 70: C++

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

Page 71: C++

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

Page 72: C++

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

Page 73: C++

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

Page 74: C++

}; 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

Page 75: C++

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

Page 76: C++

|Practical File 76

Page 77: C++

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

Page 78: C++

};

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

Page 79: C++

{ 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

Page 80: C++

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

Page 81: C++

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

Page 82: C++

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

Page 83: C++

{ 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

Page 84: C++

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

Page 85: C++

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

Page 86: C++

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

Page 87: C++

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

Page 88: C++

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

Page 89: C++

cout<<r.area()<<endl; cout<<t.area(); return 0;}

Output:

|Practical File 89

Page 90: C++

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

Page 91: C++

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

Page 92: C++

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

Page 93: C++

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

Page 94: C++

cout<<"\n\n"; bus b; v=&b; v->get(); v->show(); getch();}

Output:

|Practical File 94

Page 95: C++

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

Page 96: C++

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

Page 97: C++

{ 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

Page 98: C++

|Practical File 98