Vous êtes sur la page 1sur 90

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) { 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:

Program 2
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) { int rem,newno; int new0=0; while(num1!=0) { rem=num1%10; new0=(new0*10)+rem; num1=num1/10; } return(new0); } Output:

Program 3
4

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() { 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)); }
5

Output:

Program 4
6

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() { 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:

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 { 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() { int bonus,gsalary; bonus=(salary*20)/100; gsalary=salary+bonus; return(gsalary); } void employee::display() { cout<<"Name of employee:"<<name<<endl;
9

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(); } Output:

Program 6 wap to sort a list of names in alphabetic order


10

#include <stdio.h> #include <string.h> #include <iostream.h> #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(); } 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);
11

} } } } Output:

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.
12

#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) { 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;
13

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 a.withdraw(); getch(); }

Output:

14

Program 8
15

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() { 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();
16

getch(); }

Output:

Program 9 wap to illustrate static member function.

17

#include<conio.h> #include<iostream.h> class test { static int count; int code; public: void setcode(void) { code=++count; } 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(); getch();
18

} Output:

Program 10 wap to illustrate forward declaration of class.


19

#include<conio.h> #include<iostream.h> class fst; class snd { int x; public: 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; else cout<<"first is largest:"<<f.a; } void main() { clrscr(); fst a;
20

a.setvalue(30); snd b; b.setvalue(20); max(a,b); getch(); }

Output:

Program 11 wap to illustrate parameterized constructor. #include<conio.h>


21

#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() { integer int1(0,100); integer int2=integer(25,75); cout<<"\n object1"<<endl; int1.display(); cout<<"\n object2"<<endl; int2.display(); getch(); }

Output:

22

Program 12

23

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: 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)
24

{ 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(); } Output:
25

Program 13 wap to add two complex no using friend function. #include<conio.h>


26

#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); } 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();
27

getch(); } Output:

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
28

{ 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++) { cout<<endl<<Area of rectangle<<<<i+1<<<<is:; r1[i].display(); } getch(); return 0; }

Output:

29

Program 15 wap to generate fibonacci series using classes and objects.*/


30

#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; b=1; cout<<" "<<a; 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; }
31

Output:

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.
32

#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; } 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();
33

getch(); } Output:

Program 17 wap to calc S.I. by making a construcor with rate of interest as default argument.

34

#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; 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:
35

Program 18 wap to illustrate operator overloading using unary - operator. #include<conio.h> #include<iostream.h> class change
36

{ int a,b,c; public: void getdata(int,int,int); void display(); 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; p.display(); getch(); }
37

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

float b; 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; } void main() { add a1(1.0,2.0),a2(2.0,5.0),a3; clrscr(); a3=a1+a2; a3.display(); getch(); } Output:
39

Program 20 wap to concatanate two string using operator overloading. #include<stdio.h> #include<string.h> #include<conio.h> #include<iostream.h> class string { private:
40

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); } }; 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:
41

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() {
42

real=0.0; imag=0.0; } complex(double r, double i) { real=r; imag=i; } void show() { 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: "; c3.show(); getch(); } Output:
43

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
44

{ 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 { 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();
45

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; r1.get_marks(); r1.display(); getch(); return 0; }

Output:

46

Program 23

47

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

} void show_marks() { cout<<endl<<endl<<"MARKS OBTAINED ARE: "; cout<<endl<<endl<<"C++: "<<sub_1; cout<<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); per=(float)total/2; cout<<endl<<"PERCENTAGE OBTAINED IS: "<<per; } }; int main() { clrscr(); result r1; r1.get_per(); getch(); return 0; }

Output:

49

Program 24
50

Write a program for hybrid inheritance #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; sub2=y; } void put_marks(void) { cout<<"marks obtained:"<<"\n"; cout<<"sub1="<<sub1<<"\n"; cout<<"sub2="<<sub2<<"\n"; } }; class sports { protected: float score; public:
51

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(); } Output:

52

Program 25 To illustrate the working of virtual base class


53

#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; cout<<"object a"<<d.a<<"\t"; cout<<"object b"<<d.b<<"\t"; cout<<"object c"<<d.c; cout<<"\tTotal: "<<d.total; getch(); return 0; }

54

Output:

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,
55

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

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:

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.
57

#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: "; 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();
58

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(); }; 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();
59

cout<<endl<<"DISPLAYING THE DOCTOR DETAILS:"; 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:

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>
60

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

} }; 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() { 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:

62

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>
63

#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: 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;
64

} 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) { dailywage=w; }
65

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(); obc.putwage(); cout<<endl; obo.putstaff(); obo.putgrade(); getch(); } Output:
66

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 tasks with on a class. and overriding means we can use same name function name 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:
67

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); ob.func(1,1,1); ob.msg(); getch(); } Output:

68

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
69

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; } virtual int area(void)=0; //pure virtual function as no definition is here }; class rect : public POLY { public: int area(void) { return(l*h);
70

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

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

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(); } Output:

72

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?
73

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

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

75

Program 34 Write a program to copy the contents of one file to another file?
76

#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"; } /* dos fcopy 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?
77

#include<conio.h> #include<iostream.h> class ABC; class XYZ { int x; public: void setvalue(int i) { x=i; } friend void max(XYZ, ABC); }; class ABC { int a; public: void setvalue(int i) { a=i; } friend void max(XYZ, 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);
78

//function friend to class XYZ

//function friend to class ABC

max(q,p); both class) getch(); } Output:

//friend func max() as a bridge (accessible by objects of

Q1: Does the delete operator call the destructor of the class? Ans: The delete operator is used to free memory, it doesn't change whether the destructor is called or not. First the destructor is called, and only after that is the delete operator used to deallocate the memory.
79

In other words it's not possible to achieve the semantics you're aiming at with C+ +'s destructors and delete operators. EXAMPLE #include <iostream> #include <new> using namespace std; struct foo { ~foo() { cout << "destructor\n"; } void operator delete(void* p) { cout << "operator delete (not explicitly calling destructor)\n"; free(p); cout << "After delete\n"; } }; int main() { void *pv = malloc(sizeof(foo)); foo* pf = new (pv) foo; // use placement new delete pf; }

Output: destructor operator delete (not explicitly calling destructor) After delete
80

Q2: what is the size of the object of an empty class? Ans: Because each object needs to have a unique address (also defined in the standard) you can't really have zero sized objects. Imagine an array of zero sized objects. Because they have zero size they would all line up on the same address location. So it is easier to say that objects cannot have zero size. Even though an object has a non-zero size, if it actually takes up zero room it does not need to increase the size of derived class: Example: #include <iostream> class A {}; class B {}; class C: public A, B {}; int main() { std::cout << sizeof(A) << "\n"; std::cout << sizeof(B) << "\n"; std::cout << sizeof(C) << "\n"; g++ ty.cpp a.out } OUTPUT: 1 1 1
81

Q3: what is dangling pointer ?

Ans: Dangling pointer leads the memory leak problems. The following circumstances are memory leak (dangling pointers), not for good programming practice , try to avoid 1) Try to avoid the empty object pointer creation 2) Memory allocation without de allocation. 3) Without memory allocation, But de allocation. 4) Memory allocation, De allocation, But again unnecessary de allocation.

EXAMPLE: class Sample { public: int *ptr; Sample(int i) { ptr = new int(i); } ~Sample() { delete ptr; } void PrintVal() { cout << "The value is " << *ptr;
82

} }; void SomeFunc(Sample x) { cout << "Say i am in someFunc " << endl; } int main() { Sample s1 = 10; SomeFunc(s1); s1.PrintVal(); } In the above example when PrintVal() function is called it is called by the pointer that has been freed by the destructor in SomeFunc.

Q4: What is memory leak? Ans: Memory leaks generally occur due to inefficient usage of new operator. When memory is dynamically allocated using new, the memory is assigned in the heap storage area of the program. This memory will not be deleted on its own since it is not dependent on the scope of the defined object as in the case of those objects that are created on the stack. to delete such memory we have to call the destructor using delete operator. AS a result, it is advisable to match a new with a proper delete.

Q5: exit () & exit (1) difference?


83

Ans: Status value returned to the parent process. Generally, a return value of 0 or EXIT_SUCCESS indicates success, and any other value or the constant EXIT_FAILURE is used to indicate an error or some kind of abnormal program termination.

EXAMPLE: #include <iostream.h> #include <stdlib.h> int main () { FILE * pFile; pFile = fopen ("myfile.txt","r"); if (pFile==NULL) { Cout<<"Error opening file"; exit (1); } else { (file operations here) } return 0; }

Q6: Macro versus Inline? Ans: inline functions are similar to macros (because the function code is expanded at the point of the call at compile time), inline functions are parsed by the compiler, whereas macros are expanded by the preprocessor. As a result, there are several important differences:

84

Inline functions follow all the protocols of type safety enforced on normal functions. Inline functions are specified using the same syntax as any other function except that they include the inline keyword in the function declaration. Expressions passed as arguments to inline functions are evaluated once. In some cases, expressions passed as arguments to macros can be evaluated more than once

EXAMPLE: #include <iostream.h> #include <conio.h> inline char toupper( char a ) { return ((a >= 'a' && a <= 'z') ? a-('a'-'A') : a ); } int main() { cout<<"Enter a character: "; char ch = toupper( getc(stdin) ); cout<<ch ; } Intput: a Output: enter a character: a Q7: How to create user defined manipulator with example program Ans: Manipulators Use of the setf() , width() , and similar member functions is very convenient if the ame specifications are used for a large number of inserted items. If the formatting frequently changes, it is more convenient to use a manipulator. A manipulator is an object that can be an operand to the << or >> operator, but which modifies the state of the stream, rather than actually inserting or extracting any data. For instance, the manipulators hex and dec can be used to request hexadecimal or decimal printing of integral values. Thus, the following sequence can be used to write out the value of i in decimal and the value of x[i] in hexadecimal:
85

EXAMPLE: #define IOMANIPdeclare(T) class SMANIP(T) { public: SMANIP(T)(ios&(*f)(ios&, T), T d); friend istream& operator >>(istream& i, SMANIP(T)& m); friend ostream& operator <<(ostream& o, SMANIP(T)& m); }; class SAPP(T) { public: SAPP(T)(ios&(*f)(ios&, T)); SMANIP(T)operator()(T d); }; class IMANIP(T) { public: IMANIP(T)(ios&(*f)(ios&, T), T d); friend istream& operator >>(istream& i, IMANIP(T)& m); }; class IAPP(T) { public: IAPP(T)(ios&(*f)(ios&, T)); IMANIP(T)operator()(T d); }; class OMANIP(T) { public: OMANIP(T)(ios&(*f)(ios&, T), T d); friend ostream& operator <<(ostream& o,
86

OMANIP(T)& m); }; class OAPP(T) { public: OAPP(T)(ios&(*f)(ios&, T)); OMANIP(T)operator()(T d); }; class IOMANIP(T) { public: IOMANIP(T)(ios&(*f)(ios&, T), T d); friend istream& operator >>(istream& i, IOMANIP(T)&m); friend ostream& operator <<(ostream& o, IOMANIP(T)&m); }; class IOAPP(T) { public: IOAPP(T)(ios&(*f)(ios&, T)); IOMANIP(T)operator()(T d); }; // End of IOMANIPdeclare macro IOMANIPdeclare(int); IOMANIPdeclare(long); SMANIP(int) setw(int width); SMANIP(int) setbase(int base); SMANIP(int) setfill(int fill_char); SMANIP(int) setprecision(int precision); SMANIP(long) setiosflags(long flags); SMANIP(long) resetiosflags(long flags); }

87

Q8: What is virtual table? Ans: To implement virtual functions, C++ uses a special form of late binding known as the virtual table. The virtual table is a lookup table of functions used to resolve function calls in a dynamic/late binding manner. The virtual table sometimes goes by other names, such as vtable, virtual function table, virtual method table, or dispatch table. Because knowing how the virtual table works is not necessary to use virtual functions, this section can be considered optional reading. The virtual table is actually quite simple, though its a little complex to describe in words. First, every class that uses virtual functions (or is derived from a class that uses virtual functions) is given its own virtual table. This table is simply a static array that the compiler sets up at compile time. A virtual table contains one entry for each virtual function that can be called by objects of the class. Each entry in this table is simply a function pointer that points to the most-derived function accessible by that class.

Q9: Example of putback(), ignore(), leak() with example ? Ans: PUTBACK(): Decrements the internal get pointer by one, and c becomes the character to be read at that position by the next input operation. The function effectively calls the sputbackc member function of the streambuff object associated to the stream. A subsequent call to member gcount will return zero. EXAMPLE: #include <iostream> using namespace std;
88

int main () { char c; int n; char str[256]; cout << "Enter a number or a word: "; c = cin.get(); if ( (c >= '0') && (c <= '9') ) { cin.putback (c); cin >> n; cout << "You have entered number " << n << endl; } else { cin.putback (c); cin >> str; cout << " You have entered word " << str << endl; } return 0; } IGNORE():The extraction ends when n characters have been extracted and discarded or when the character delim is found, whichever comes first. In the latter case, the delim character itself is also extracted. EXAMPLE: #include <iostream> using namespace std; int main () { char first, last; cout << "Enter your first and last names: "; first=cin.get(); cin.ignore(256,' ');
89

last=cin.get(); cout << "Your initials are " << first << last; return 0; }

90