You are on page 1of 59

Contents

Complex Number...................................................................................................................................................1
Stack......................................................................................................................................................................3
Income class..........................................................................................................................................................6
The area of two triangles.......................................................................................................................................9
Employee.............................................................................................................................................................11
Student................................................................................................................................................................15
User of computer with password........................................................................................................................20
Cost of carpeting.................................................................................................................................................24
Students are to specific department...................................................................................................................28
Calculate the cost of publishing conference........................................................................................................34
Booking rooms in the hotel.................................................................................................................................37
Booking rooms in the hotel with friend function.................................................................................................40
Tax for each employee at a flat...........................................................................................................................42
Bank maintains account of two types (saving, current).......................................................................................46
Develop a test to establish order of constructor and destructor.........................................................................51
Copy constructor invoke......................................................................................................................................53
Calculate the surface area and volume...............................................................................................................55
Total tax should paid at the end each day...........................................................................................................58
Calculate the surface area and volume...............................................................................................................61
Generic queue of element...................................................................................................................................65
Build a generic class array...................................................................................................................................68
Bank maintains account of two types (saving, current) ....................................................................................... 39

Develop a test to establish order of constructor and destructor ......................................................................... 42

Copy constructor invoke...................................................................................................................................... 43

Calculate the surface area and volume............................................................................................................... 45

Total tax should paid at the end each day........................................................................................................... 48

Calculate the surface area and volume............................................................................................................... 51

Generic queue of element................................................................................................................................... 52

Build a generic class array................................................................................................................................... 55

0
Complex Number
#include<iostream.h>
#include<conio.h>
class complex
{
float real;
float imag;
public:
void getnum();
void putnum();
void sum(complex,complex);
void dif(complex,complex);
};
void complex::getnum()
{
cout<<"\nEnter the real part : ";
cin>>real;
cout<<"\nEnter the imaginary part : ";
cin>>imag;
}
void complex::putnum()
{
cout<<real;
if(imag<0)
cout<<imag<<"i\n";
else
cout<<"+"<<imag<<"i\n";
}
void complex::sum(complex a,complex b)
{
real=a.real+b.real;
imag=a.imag+b.imag;
}
void complex::dif(complex a,complex b)
{
real=a.real-b.real;

1
imag=a.imag-b.imag;
}
int main()
{
complex c1,c2,c3,c4;
cout<<"\nEnter first complex number\n";
c1.getnum();
cout<<"\nEnter second complex number\n";
c2.getnum();
//Sum of two inputted numbers
c3.sum(c1,c2);
cout<<"\nThe Sum is : ";
c3.putnum();
//Difference of two inputted numbers
c4.dif(c1,c2);
cout<<"\nThe Difference is : ";
c4.putnum();
}

Output:
Enter first complex number
Enter the real part : 4
Enter the imaginary part : 5
Enter second complex number
Enter the real part : 3
Enter the imaginary part : 6
The Sum is : 7+11i
The Difference is : 1-1i

2
Stack
#include<iostream.h>
#include<conio.h>
const int size=10;
class stack
{
int arr[size];
int top;
public:
void initialize();
void push(int);
int pop();
int stack_top();
void show();
};
void stack::initialize()
{
top=-1;
}
void stack::push(int n)
{
if(top!=size-1)
arr[++top]=n;
else
cout<<"\nOverflow!!!\n";
}
int stack::pop()
{
if(top!=-1)
return arr[top--];
else
{
cout<<"\nUnderflow!!!\n";
return NULL;
}
}

3
int stack::stack_top()
{
if(top==-1)
{
cout<<"\nStack is Empty!\n";
return NULL;
}
else
return arr[top];
}
void stack::show()
{
if(top==-1)
cout<<"\nEmpty Stack!!!\n";
else
{
for(int i=0;i<=top;i++)
cout<<arr[i]<<" ";
cout<<"<--TOP\n";
}
}
int main()
{
int p;
stack s1;
s1.initialize();
//Pushing Values
cout<<"\nPUSHING 3,5,7 onto stack\n";
s1.push(3);
s1.push(5);
s1.push(7);
cout<<"\nStack is : ";
s1.show();
//Show Top Value
cout<<"\nTop of Stack is :"<<s1.stack_top()<<endl;
//Popping Values
cout<<"\nPOPPING\n";
p=s1.pop();

4
if(p!=NULL)
cout<<"\nPopped out value is : "<<p<<endl;
p=s1.pop();
if(p!=NULL)
cout<<"\nPopped out value is : "<<p<<endl;
p=s1.pop();
if(p!=NULL)
cout<<"\nPopped out value is : "<<p<<endl;
//Show Top of Stack
p=s1.stack_top();
if(p!=NULL)
cout<<"Top of Stack is : "<<p<<endl;
return 0;
}

Output:
PUSHING 3,5,7 onto stack
Stack is : 3 5 7 <--TOP
Top of Stack is :7
POPPING
Popped out value is : 7
Popped out value is : 5
Popped out value is : 3
Stack is Empty!

5
Income class
#include<iostream.h>
#include<conio.h>
class income
{
double BS;
double DA;
public:
void initialize(double,double);
double pay_sal();
double deduction();
double calc_tax();
void income_detail();
};
void income::initialize(double b,double d)
{
BS=b;
DA=d;
}
double income::pay_sal()
{
double HRA;
HRA = 0.15 * BS;
return (BS+DA+HRA);
}
double income::deduction()
{
double SC,PF;
SC = PF = 0.08 * BS;
return (SC+PF);
}
double income::calc_tax()
{
double sal,tax,sc;
sal = pay_sal();
sal*=12;

6
if(sal<100000)
{
tax = 0.2 * sal;
}
else
{
tax = 0.3 * sal;
sc = 0.1 * tax;
tax+=sc;
}
return tax;
}
void income::income_detail()
{
cout<<"\nBasic Salary is : "<<BS;
cout<<"\nDearness Allowance is : "<<DA;
cout<<"\nMonthly Deduction is : "<<deduction();
cout<<"\nTotal Monthly Salary is : "<<pay_sal();
cout<<"\nTotal Annual Salary is : "<<12*pay_sal();
cout<<"\nAnnual Payable Tax is : "<<calc_tax();
}
int main()
{

double bs,da;
income s;
//Initializing Income
cout<<"\nEnter Basic Pay : ";
cin>>bs;
cout<<"\nEnter Dearness Allowance : ";
cin>>da;
s.initialize(bs,da);
s.income_detail();
return 0;
}

Output:
7
Enter Basic Pay : 5000
Enter Dearness Allowance : 5
Basic Salary is : 5000
Dearness Allowance is : 5
Monthly Deduction is : 800
Total Monthly Salary is : 5755
Total Annual Salary is : 69060
Annual Payable Tax is : 13812
Process returned 0 (0x0) execution time : 36.459 s
Press any key to continue.

8
The area of two triangles
#include<iostream.h>
#include<conio.h>
#include<math.h>
class right_triangle
{
double base;
double height;
public:
void initialize(double,double);
double area();
double peri();
};
void right_triangle::initialize(double b, double h)
{
base=b;
height=h;
}
double right_triangle::area()
{
return (0.5*base*height);
}
double right_triangle::peri()
{
double hypt;
hypt = sqrt(base*base + height*height);
return base+height+hypt;
}
int main()
{
right_triangle r1,r2;
double bs,ht;
//Initializing triangles
cout<<"\nINPUT\n";
cout<<"\nEnter base of first triangle : ";
cin>>bs;

9
cout<<"Enter height of first triangle : ";
cin>>ht;
r1.initialize(bs,ht);
cout<<"\nEnter base of second triangle : ";
cin>>bs;
cout<<"Enter height of second triangle : ";
cin>>ht;
r2.initialize(bs,ht);
//Calculating area and perimeter
cout<<"\nArea of first triangle : "<<r1.area();
cout<<"\nPerimeter of first triangle : "<<r1.peri();
cout<<"\n\nArea of second triangle : "<<r2.area();
cout<<"\nPerimeter of second triangle : "<<r2.peri();
return 0;
}

Output:
INPUT
Enter base of first triangle : 7
Enter height of first triangle : 4
Enter base of second triangle : 6
Enter height of second triangle : 2
Area of first triangle : 14
Perimeter of first triangle : 19.0623
Area of second triangle :6
Perimeter of second triangle : 14.3246
Process returned 0 (0x0) execution time : 26.804 s
Press any key to continue.

10
Employee
#include<iostream.h>
#include<conio.h>
enum{gm=1,dgm,md,manager,asst_manager,supervisor,mechanic,clerk=8};
class emp
{
int ssn;
char *dob;
int rank;
double salary;
public:
emp();
emp(int,char*,int,double);
void sal_raise(double);
void rank_raise();
void show();
};
emp::emp()
{
ssn=rank=0;
dob=NULL;
salary=0.0;
}
emp::emp(int s,char *d,int r,double sal)
{
ssn=s;
dob=d;
rank=r;
salary=sal;
}
void emp::sal_raise(double rate=10)
{
salary += (rate/100.0 * salary);
}
void emp::rank_raise()
{

11
if(rank!=1)
--rank;
sal_raise(25);
}
void emp::show()
{
cout<<"\nEmployee's number is : "<<ssn;
cout<<"\nEmployee's date of birth is : "<<dob;
cout<<"\nEmployee's rank is : "<<rank;
cout<<"\nEmployee's salary is : "<<salary<<endl<<endl;
}
int main()
{
emp e1(101,"10.05.1976",gm,75000);
int n,r;
char *d;
double s;
cout<<"INPUTTING\n";
cout<<"=========\n";
cout<<"\nEnter the number associated to employee : ";
cin>>n;
cout<<"\nEnter date of birth : ";
cin>>d;
cout<<"\nEnter rank of employee : ";
cin>>r;
cout<<"\nEnter salary of the employee : ";
cin>>s;
emp e2(n,d,r,s);
cout<<"\nFirst employee created\n\n";
cout<<"\nEnter the number associated to employee : ";
cin>>n;
cout<<"\nEnter date of birth : ";
cin>>d;
cout<<"\nEnter rank of employee : ";
cin>>r;
cout<<"\nEnter salary of the employee : ";
cin>>s;
emp e3(n,d,r,s);

12
cout<<"\nSecond employee created\n\n";
cout<<"\nGiving salary raise to first employee\n";
e2.sal_raise();
e2.show();
cout<<"\nGiving rank raise to second employee\n";
e3.rank_raise();
e3.show();
cout<<"\nAnother Object\n";
e1.show();
return 0;
}

Output:

INPUTTING
=========
Enter the number associated to employee : 1
Enter date of birth : 6/6/1978
Enter rank of employee :2
Enter salary of the employee : 8000
First employee created
Enter the number associated to employee : 2
Enter date of birth : 5/5/1968
Enter rank of employee :1
Enter salary of the employee : 14000
Second employee created
Giving salary raise to first employee
Employee's number is :1
Employee's date of birth is : 5/5/1968
Employee's rank is :2
Employee's salary is : 8800
Giving rank raise to second employee
Employee's number is :2
Employee's date of birth is : 5/5/1968
Employee's rank is :1
Employee's salary is : 17500
Another Object
Employee's number is : 101
13
Employee's date of birth is : 10.05.1976
Employee's rank is :1
Employee's salary is : 75000

14
Student
#include<iostream.h>
#include<conio.h>
enum departments{USIT=1,USBAS,USMS,USBT,USCT,USAP,IGIT};
class student
{
int roll;
int dept;
int year;
int sem;
public:
student();
void registration(int,int,int,int);
void dept_change(int);
int promotion();
void show();
};
student::student()
{
roll=dept=year=sem=0;
}
void student::registration(int r,int d,int y,int s)
{
roll=r;
dept=d;
year=y;
sem=s;
}
void student::dept_change(int d)
{
if(d>=1 && d<=7)
dept=d;
}
int student::promotion()
{
if(sem>8)

15
return 0; //pass-out
else
{
++sem;
return 1; //promoted
}
}
void student::show()
{
cout<<"\n\nSTUDENT DETAILS\n";
cout<<"---------------";
cout<<"\nStudent's Roll Number is : "<<roll;
cout<<"\nStudent's Department is : "<<dept;
cout<<"\nStudent's Year of Admission is : "<<year;
if(sem<=8)
cout<<"\nStudent's Semester of Study : "<<sem;
else
cout<<"\nStudent is a Pass-Out now!\n";
}
int main()
{
student s1,s2,s3;
int rn,dp,yr,sm;
cout<<"REGISTRATION\n";
cout<<"^^^^^^^^^^^^\n";
cout<<"\nSTUDENT 1\n";
cout<<"\nEnter roll number : ";
cin>>rn;
cout<<"\nEnter department : ";
cin>>dp;
cout<<"\nEnter year of admission : ";
cin>>yr;
cout<<"\nEnter semester of study : ";
cin>>sm;
s1.registration(rn,dp,yr,sm);
cout<<"\nSTUDENT 2\n";
cout<<"\nEnter roll number : ";
cin>>rn;

16
cout<<"\nEnter department : ";
cin>>dp;
cout<<"\nEnter year of admission : ";
cin>>yr;
cout<<"\nEnter semester of study : ";
cin>>sm;
s2.registration(rn,dp,yr,sm);
cout<<"\nSTUDENT 3\n";
cout<<"\nEnter roll number : ";
cin>>rn;
cout<<"\nEnter department : ";
cin>>dp;
cout<<"\nEnter year of admission : ";
cin>>yr;
cout<<"\nEnter semester of study : ";
cin>>sm;
s3.registration(rn,dp,yr,sm);
cout<<"Student 1, Earlier\n";
cout<<"==================";
s1.show();
s1.dept_change(USMS);
cout<<"\n\nStudent 1, Later\n";
cout<<"================";
s1.show();
cout<<"\n\nStudent 2, Earlier\n";
cout<<"==================";
s2.show();
s2.promotion();
cout<<"\n\nStudent 2, Later\n";
cout<<"================";
s2.show();
cout<<"\n\nStudent 3, Earlier\n";
cout<<"==================";
s3.show();
s3.dept_change(USAP);
s3.promotion();
cout<<"\n\nStudent 3, Later\n";
cout<<"================";

17
s3.show();
return 0;
}

Output:
REGISTRATION
^^^^^^^^^^^^
STUDENT 1
Enter roll number : 11111
Enter department :1
Enter year of admission : 2008
Enter semester of study : 3
STUDENT 2
Enter roll number : 11112
Enter department :1
Enter year of admission : 2009
Enter semester of study : 2
STUDENT 3
Enter roll number : 11113
Enter department :2
Enter year of admission : 2010
Enter semester of study : 1
Student 1, Earlier
==================
STUDENT DETAILS
---------------
Student's Roll Number is : 11111
Student's Department is :1
Student's Year of Admission is : 2008
Student's Semester of Study : 3
Student 1, Later
================
STUDENT DETAILS
---------------
Student's Roll Number is : 11111
Student's Department is :3
Student's Year of Admission is : 2008
Student's Semester of Study : 3

18
Student 2, Earlier
==================
STUDENT DETAILS
---------------
Student's Roll Number is : 11112
Student's Department is :1
Student's Year of Admission is : 2009
Student's Semester of Study : 2
Student 2, Later
================
STUDENT DETAILS
---------------
Student's Roll Number is : 11112
Student's Department is :1
Student's Year of Admission is : 2009
Student's Semester of Study : 3
Student 3, Earlier
==================
STUDENT DETAILS
---------------
Student's Roll Number is : 11113
Student's Department is :2
Student's Year of Admission is : 2010
Student's Semester of Study : 1
Student 3, Later
================
STUDENT DETAILS
---------------
Student's Roll Number is : 11113
Student's Department is :6
Student's Year of Admission is : 2010
Student's Semester of Study : 2

19
User of computer with password
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
enum access_rights{X=1,R,W,ALL};
class user
{
char *name;
char *pswd;
int right;
public:
user();
user(char*);
~user();
void create_profile(char*,char*,int);
int change_pswd(char*);
int change_right(int);
void show_profile();
};
user::user()
{
name=pswd=NULL;
right=0;
}
user::user(char *n)
{
int len = strlen(n);
name = new char[len+1];
pswd = new char[4];
strcpy(name,n);
pswd[0]=name[0];
pswd[1]=name[1];
pswd[2]=name[2];
pswd[3]='\0';
right=ALL;

20
}
user::~user()
{
delete name;
delete pswd;
}
void user::create_profile(char *n,char *p,int r)
{
int len1,len2;
len1=strlen(n);
len2=strlen(p);
name = new char[len1+1];
pswd = new char[len2+1];
strcpy(name,n);
strcpy(pswd,p);
right=r;
}
int user::change_pswd(char *p)
{
char *t;
cout<<"\nEnter previous password : ";
cin>>t;
if(strcmp(pswd,t))
{
cout<<"\nWrong Password Entered!\n";
return 0;
}
else
{
strcpy(pswd,p);
return 1;
}
}
int user::change_right(int r)
{
char *t;
cout<<"\nEnter previous password : ";
cin>>t;

21
if(strcmp(pswd,t))
{
cout<<"\nWrong Password!\nAccess Denied.\n";
return 0;
}
else
{
if(r>=1 && r<=4)
right=r;
else
cout<<"\nWrong Value Provided.\n";
return 1;
}
}
void user::show_profile()
{
cout<<"\nUSER INFO";
cout<<"\n^^^^^^^^^";
cout<<"\nName of the user is : "<<name;
cout<<"\nPassword of the profile : "<<pswd;
cout<<"\nAccess Rights of user : ";
if(right==1)
cout<<"X"<<endl;
else if(right==2)
cout<<"R"<<endl;
else if(right==3)
cout<<"W"<<endl;
else
cout<<"ALL"<<endl;

}
int main()
{
int temp;
user r1="Rajesh";
r1.show_profile();

user r2;
22
r2.create_profile("Vipin","asdf",2);
r2.show_profile();
cout<<"\nChanging password to zxcv\n";
temp = r2.change_pswd("zxcv");
if(temp==1)
cout<<"\nPassword changed successfully\n";
r2.show_profile();
cout<<"\nChanging access rights to ALL\n";
temp = r2.change_right(ALL);
if(temp==1)
cout<<"\nAccess rights changed successfully\n";
r2.show_profile();
return 0;
}
Output:

USER INFO
^^^^^^^^^
Name of the user is : Rajesh
Password of the profile : Raj
Access Rights of user : ALL
USER INFO
^^^^^^^^^
Name of the user is : Vipin
Password of the profile : asdf
Access Rights of user :R
Changing password to zxcv
Enter previous password : asdf

23
Cost of carpeting
#include<iostream.h>
#include<conio.h>
class rectangle
{private:
float length; float breadth;
public:
rectangle(){ length=breadth=0; }
void getdata(){cout<<"\n\n";
cout<<"Enter Length in feet : "; cin>>length;
cout<<"Enter Breadth in feet : "; cin>>breadth;}
float area(){return length*breadth; }};
class room
{private:
rectangle wall[4]; rectangle window[2];
rectangle door; rectangle floor;
public:
float calc_cost(float, float);
void input();};
void room::input()
{for(int i=0;i<4;i++)
{cout<<"\n\nEnter dimension for wall "<<i+1<<" : ";
wall[i].getdata(); }
for(int i=0;i<2;i++)
{cout<<"\n\nEnter dimension for window "<<i+1<<" : ";
window[i].getdata(); }
cout<<"\n\nEnter dimension for door ";
door.getdata();
cout<<"\n\nEnter dimension for floor ";
floor.getdata();}
float room::calc_cost(float c, float p)
{float ws=0,wis=0; float cc,cp,tc; float w[4], win[2],d,f;
for(int i=0;i<4;i++){w[i]=wall[i].area();ws=ws+w[i]; }
win[0]=window[0].area(); win[1]=window[1].area();
wis=win[0]+win[1];d=door.area(); f=floor.area();
cc=c*f; cp=p*(ws-wis-d);tc=cc+cp; return tc; }
int main()
{int n,i; room *r; float carpeting,papering;
cout<<"Enter how many rooms are there : "; cin>>n;
r = new room[n];
for(i=0;i<n;i++)
{cout<<"\nDETAILS OF ROOM #"<<i+1<<endl;
cout<<"..................\n";r[i].input(); }
cout<<"\n\nEnter cost of carpeting the floor : ";
cin>>carpeting; cout<<"\nEnter cost of papering the walls : ";
cin>>papering; cout<<"\n\n";
for(i=0;i<n;i++) {cout<<"Total cost for room #"<<i+1
<<" is : Rs."<<r[i].calc_cost(carpeting,papering)<<endl;}
delete r; return 0; }

Output:
Enter how many rooms are there : 1
DETAILS OF ROOM #1
..................
Enter dimension for wall 1 :
Enter Length in feet : 6
Enter Breadth in feet : 6

Enter dimension for wall 2 :

24
Enter Length in feet : 6
Enter Breadth in feet : 7
Enter dimension for wall 3 :
Enter Length in feet : 6
Enter Breadth in feet : 6
Enter dimension for wall 4 :
Enter Length in feet : 6
Enter Breadth in feet : 7
Enter dimension for window 1 :
Enter Length in feet : 3
Enter Breadth in feet : 3
Enter dimension for window 2 :
Enter Length in feet : 2
Enter Breadth in feet : 2
Enter dimension for door
Enter Length in feet : 2
Enter Breadth in feet : 3
Enter dimension for floor
Enter Length in feet : 6
Enter Breadth in feet : 7
Enter cost of carpeting the floor : 5
Enter cost of papering the walls : 5
Total cost for room #1 is : Rs.895

25
Students are to specific department
#include<iostream.h> #include<conio.h>
#include<string.h> #include<stdio.h>
enum departments{USIT=100,USMS,USBT,USCT,BTECH};
enum course_specific_dept{MCA=150,BTECH_CS,BTECH_CT,BTECH_EC,
BTECH_M,BTECH_C,MTECH,BT,MBT};
enum course_any_dept{MBA=200,OTHER};
class admission
{ char name[25]; int roll; int dept; float fee; long int ddn;
public:
admission()
{ roll=dept=0; name[0]='\0'; fee=0; ddn=0; }
admission(int r,char n[],int d,float f,long int dd)
{roll=r; strcpy(name,n); dept=d; fee=f; ddn=dd; }
int exist(){return dept;}
void initialize(); void admit();
float withdraw_admission();
void details(); };
void admission::initialize()
{ roll=dept=0; name[0]='\0'; fee=0; ddn=0; }
void admission::admit()
{ cout<<"Enter name of the candidate : "; gets(name);
cout<<"Enter roll number of candidate :"; cin>>roll;
cout<<"Enter department code : "; cin>>dept;
cout<<"Enter fee to be deposit : "; cin>>fee;
cout<<"Enter Demand Draft number : "; cin>>ddn; }
float admission::withdraw_admission()
{ float penalty; penalty = 0.2 * fee;
return (fee-penalty); }
void admission::details()
{ cout<<endl; cout<<"\t\tRoll Number : "<<roll<<endl;
cout<<"\t\tName : "<<name<<endl;
cout<<"\t\tDepartment : "<<dept<<endl; }
class registration
{ char dor[15]; int course;
public:
registration()
{dor[0]='\0'; course=0; }
registration(admission s)
{ if(!s.exist())
s.initialize();
dor[0]='\0'; course=0; }
registration(admission s,char d[],int c)
{ if(!s.exist())
s.admit();
strcpy(dor,d); course=c; }
int register_course(admission);
void withdraw_course();
void report(admission); };
int registration::register_course(admission s)
{ int c; if(!s.exist())
s.admit(); cout<<"Enter date of registration : ";
gets(dor);
cout<<"Enter course code for registration : "; cin>>c;
if(s.exist()==USIT)
{ if(c==MCA || c==BTECH_CS || c==BTECH_EC
|| c==MBA || c==OTHER)
course=c;
else
{cout<<"\nStudent can not register for this course!\n";
return 0; } }
else if(s.exist()==USBT)
{ if(c==BT || c==MBT || c==MBA || c==OTHER)
course=c;
else
{cout<<"\nStudent can not register for this course!\n";
return 0; } }
else if(s.exist()==USMS)
{ if(c==MBA || c==OTHER)

26
course=c;
else
{cout<<"\nStudent can not register for this course!\n";
return 0; } }
else if(s.exist()==USCT)
{ if(c==BTECH_CT || c==MBA || c==OTHER)
course=c;
else
{cout<<"\nStudent can not register for this course!\n";
return 0; }}
else if(s.exist()==BTECH)
{ if(c==BTECH_M || c==BTECH_C || c==MTECH
|| c==MBA || c==OTHER)
course=c;
else
{cout<<"\nStudent can not register for this course!\n";
return 0; }}
return 1; }
void registration::withdraw_course()
{ course=0; }
void registration::report(admission s)
{ cout<<"\n\tREGISTRATION REPORT\n";
cout<<"\t-------------------\n";
s.details();
cout<<"\t\tCourse taken : "<<course<<endl<<endl; }
int main()
{ cout<<"USIT=100,USMS=101,USBT=102,USCT=103,BTECH=104\n\n";
cout<<"MCA=150,BTECH_CS=151,BTECH_CT=152,BTECH_EC=153,\n"
<<"BTECH_M=154,BTECH_C=155,MTECH=156,BT=157,MBT=158\n\n";
cout<<"MBA=200,OTHER=201\n\n";
admission stud; stud.admit();
registration r1,r2,r3;
r1.register_course(stud);
r2.register_course(stud);
r1.report(stud); r2.report(stud);
r3.report(stud); return 0;}

Output:
USIT=100,USMS=101,USBT=102,USCT=103,BTECH=104
MCA=150,BTECH_CS=151,BTECH_CT=152,BTECH_EC=153,
BTECH_M=154,BTECH_C=155,MTECH=156,BT=157,MBT=158
MBA=200,OTHER=201
Enter name of the candidate : usit
Enter roll number of candidate : 12345
Enter department code : 100
Enter fee to be deposit : 2100
Enter Demand Draft number :2
Enter date of registration : Enter course code for registration : 150
Enter date of registration : Enter course code for registration : 150
REGISTRATION REPORT
-------------------
Roll Number : 12345
Name : usit
Department : 100
Course taken : 150

27
REGISTRATION REPORT
-------------------
Roll Number : 12345
Name : usit
Department : 100
Course taken : 150
REGISTRATION REPORT
-------------------
Roll Number : 12345
Name : usit
Department : 100
Course taken : 0

28
Calculate the cost of publishing conference

#include<iostream.h>
#include<string.h>
#include<conio.h>
#include<stdio.h>
const int std_size=15;
const float std_cost=5;
const float penalty=0.5;
class paper;
float totalcost(paper*,int);
class paper
{
char author[25];
int page;
public:
paper()
{
author[0]='\0';
page=0;
}
paper(char a[],int p)
{
strcpy(author,a);
page=p;
}
void input();
float papercost();
};
void paper::input()
{
cout<<"\nEnter name of the author : ";
gets(author);
cout<<"\nEnter number of pages in his research paper : ";
cin>>page;
}
29
float paper::papercost()
{
int extra;
float cost;
if(page<=std_size)
return page*std_cost;
else
{
extra = page - std_size;
cost = (std_size*std_cost) + extra*(std_cost + penalty);
return cost;
}
}
int main()
{
int count;
paper *conf;
cout<<"Enter total number of papers : ";
cin>>count;
cout<<"\nEnter details of the papers\n";
cout<<"---------------------------\n";
conf = new paper[count];
for(int i=0;i<count;i++)
conf[i].input();
cout<<"\n\nThe total cost of publishing is : Rs."<<totalcost(conf,count);
delete conf;
return 0;
}
float totalcost(paper *p,int n)
{
float tcost=0;
for(int i=0;i<n;i++)
{
tcost += p[i].papercost();
}
return tcost;
}

30
Output:
Enter total number of papers : 2

Enter details of the papers


---------------------------
Enter name of the author :
Enter number of pages in his research paper : 3
Enter name of the author :
Enter number of pages in his research paper : 3
The total cost of publishing is : Rs.30

31
Booking rooms in the hotel

#include<iostream.h>
#include<conio.h>
const double price_s=3000;
const double price_d=5000;
//CLASS FOR SINGLE ROOMS
class single_room
{
int s_room_num;
static int occupy;
public:
single_room();
static double calc_tax100();
};
int single_room::occupy=0;
single_room::single_room()
{
++occupy;
s_room_num = occupy;
}
double single_room::calc_tax100()
{
double ttax,etax,ltax;
etax = 0.1 * price_s;
ltax = 0.3 * price_s;
ttax = occupy * (etax+ltax);
return ttax;
}
//CLASS FOR DOUBLE ROOMS
class double_room
{
int d_room_num;
static int occupy;
public:
double_room();
32
static double calc_tax200();
};
int double_room::occupy=0;
double_room::double_room()
{
++occupy;
d_room_num = occupy;
}
double double_room::calc_tax200()
{
double ttax,etax,ltax;
etax = 0.1 * price_d;
ltax = 0.3 * price_d;
ttax = occupy * (etax+ltax);
return ttax;
}
//IMPLEMENTATION
int main()
{
int s,d; // how many rooms were occupied
double tax_single,tax_double,tax_total;
cout<<"ROOM'S INFO\n";
cout<<"^^^^^^^^^^^\n";
cout<<"Enter how many single rooms were occupied : ";
cin>>s;
cout<<"Enter how many double rooms were occupied : ";
cin>>d;
single_room *sr;
double_room *dr;
sr = new single_room[s];
dr = new double_room[d];
tax_single = single_room::calc_tax100();
tax_double = double_room::calc_tax200();
tax_total = tax_single + tax_double;
cout<<"\nThe total payable tax for today is : Rs."<<tax_total;
return 0;
}

33
Output:
ROOM'S INFO
^^^^^^^^^^^
Enter how many single rooms were occupied : 2
Enter how many double rooms were occupied : 1
The total payable tax for today is : Rs.4400

34
Booking rooms in the hotel with friend function

#include<iostream.h>
#include<conio.h>
const double price_s=3000;
const double price_d=5000;
//CLASS FOR SINGLE ROOMS
class single_room
{
int s_room_num;
static int occupy;
public:
single_room();
friend double calc_tax();
};
int single_room::occupy=0;
single_room::single_room()
{
++occupy;
s_room_num = occupy;
}
//CLASS FOR DOUBLE ROOMS
class double_room
{
int d_room_num;
static int occupy;
public:
double_room();
friend double calc_tax();
};
int double_room::occupy=0;
double_room::double_room()
{
++occupy;
d_room_num = occupy;
}
35
double calc_tax()
{
double ttaxs,etaxs,ltaxs;
etaxs = 0.1 * price_s;
ltaxs = 0.3 * price_s;
ttaxs = single_room::occupy * (etaxs+ltaxs);
double ttaxd,etaxd,ltaxd;
etaxd = 0.1 * price_d;
ltaxd = 0.3 * price_d;
ttaxd = double_room::occupy * (etaxd+ltaxd);
return (ttaxs+ttaxd);
}
//IMPLEMENTATION
int main()
{
int s,d; // how many rooms were occupied
double tax_total;
cout<<"ROOM'S INFO\n";
cout<<"^^^^^^^^^^^\n";
cout<<"Enter how many single rooms were occupied : ";
cin>>s;
cout<<"Enter how many double rooms were occupied : ";
cin>>d;
single_room *sr;
double_room *dr;
sr = new single_room[s];
dr = new double_room[d];
tax_total = calc_tax();
cout<<"\nThe total payable tax for today is : Rs."<<tax_total;
return 0;
}

Output:
ROOM'S INFO
^^^^^^^^^^^
Enter how many single rooms were occupied : 2
Enter how many double rooms were occupied : 1
The total payable tax for today is : Rs.4400

36
Tax for each employee at a flat

#include<iostream.h>
#include<conio.h>
class tax
{double sal; double agri_in; double cons_in;
double intr_in; static int emp;
public:
tax();
void initialize(double,double,double,double);
double calc_tax();
friend double frnd_calc_tax(tax);
};
int tax::emp=10; tax::tax()
{sal=agri_in=cons_in=intr_in=0;}
void tax::initialize(double s,double a,double c,double i)
{ sal=s; agri_in=a; cons_in=c; intr_in=i; }
double tax::calc_tax()
{ double tsal,tx=0; tsal = sal + agri_in + cons_in + intr_in;
if((12*tsal)>50000)
tx = 0.3 * tsal * 12; return tx; }
double frnd_calc_tax(tax t)
{double tsal,tx=0; tsal = t.sal + t.agri_in + t.cons_in + t.intr_in;
if((12*tsal)>50000)
tx = 0.3 * tsal * 12; return tx; }
//IMPLEMENTATION
int main()
{const int e=1; double s,a,c,i,etax,ftax;
tax e_tax[e]; tax e_frn[e];
cout<<"Enter employee's salaries\n";
cout<<"''''''\n";
for(int j=0;j<e;j++)
{
cout<<"\nEMP #"<<j+1<<endl;
cout<<"Enter salary : "; cin>>s;
cout<<"Enter agricultural income : ";
cin>>a;
cout<<"Enter consulting income : ";
cin>>c;
cout<<"Enter interest income : ";
cin>>i;
e_tax[j].initialize(s,a,c,i); }
cout<<"\n\nEnter employee's salaries\n";
cout<<"'''''''''''''''''''''''''\n";
for(int j=0;j<e;j++)
{ cout<<"\nEMP #"<<j+1<<endl;
cout<<"Enter salary : ";
cin>>s;
cout<<"Enter agricultural income : ";
cin>>a;
cout<<"Enter consulting income : ";
cin>>c;
cout<<"Enter interest income : ";
cin>>i;
e_frn[j].initialize(s,a,c,i); }
//Calling Class Function to calculate tax
cout<<"TAX USING CLASS FUCTION\n";
for(int j=0;j<e;j++)
{cout<<"\nTax of EMP #"<<j+1<<" is : Rs."<<e_tax[j].calc_tax();
}
//Calling Friend Function to calculate tax
cout<<"\n\nTAX USING FRIEND FUCTION\n";
for(int j=0;j<e;j++)
{cout<<"\nTax of EMP #"<<j+1<<" is : Rs."<<frnd_calc_tax(e_frn[j]);
} return 0; }

Output:

37
Enter employee's salaries
'''''''''''''''''''''''''
EMP #1
Enter salary : 90000
Enter agricultural income : 1000
Enter consulting income : 2000
Enter interest income : 1000
Enter employee's salaries
'''''''''''''''''''''''''
EMP #1
Enter salary : 60000
Enter agricultural income : 2000
Enter consulting income : 1000
Enter interest income : 1000
TAX USING CLASS FUCTION
Tax of EMP #1 is : Rs.338400
TAX USING FRIEND FUCTION
Tax of EMP #1 is : Rs.230400

38
Bank maintains account of two types (saving, current)
#include<iostream.h> #include<conio.h> #include<string.h>
class account
{public:
int id; char *name; float amount;
void deposit(float d)
{ amount=amount+d;
cout<<"\ncurrent amount is : "<<amount; }};
class savings:public account
{ public:
savings(int i, char *n, float d)
{ id=i; strcpy(n,name);
amount =d; }
void withdrawl(float d)
{ float temp; temp=amount -d;
if(d >0.7*amount || temp<500)
cout<<"\n transaction not successful";
else
{ cout<<"\n transaction successful ";
cout<<"\n ur current bal is "<<amount; } }
void statement()
{ cout<<"\n\n Account information for month of november";
cout<<"\n-----";
cout<<"\n customer id :"<<id;
cout<<"\n name :"<<name;
cout<<"\n interest for month"<<0.4*amount/12;
cout<<"\n amount :"<<amount;
cout<<"\n thank you !!"; } };
class current :public account
{ public:
current(int i, char *n, float d)
{ id=i; strcpy(n,name);
amount =d;
} void withdrawl(float d)
{ float temp; temp=amount -d;
if(temp <0)
cout<<"\n transaction not successful";
else { cout<<"\n transaction successful ";
amount=amount-d;
cout<<"\n ur current bal is "<<amount;
}}
void statement()
{ cout<<"\n\n Current Account information for month of november";
cout<<"\n-------"; cout<<"\n id :"<<id;
cout<<"\n name :"<<name;
cout<<"\n charge for month"<<1000/12;
cout<<"\n amount :"<<amount;
cout<<"\n thank you !!";
}};
void main()
{ int i; char *n; float d;
cout<<"enter id :"; cin>>i;
cout<<"\n name :"; cin>>n;
cout<<"\n amount :"; cin>>d;
savings s(i,n,d);
cout<<"\n\n enter amount to deposit :"; cin>>d;
s.deposit(d);
cout<<"\n\n enter amount to withdraw :"; cin>>d;
s.withdrawl(d);
s.statement();
cout<<"\n\n for new current account";
cout<<"enter id :"; cin>>i;
cout<<"\n name :"; cin>>n;
cout<<"\n amount :"; cin>>d; current c(i,n,d);

39
cout<<"\n\n enter amount to deposit :"; cin>>d;
c.deposit(d);
cout<<"\n\n enter amount to withdraw :"; cin>>d;
c.withdrawl(d); c.statement(); getch(); }

OUTPUT

---------

OUTPUT

-------

enter id : 23

name : mahesh

amount : 3000

enter amount to deposit : 2000

current amount is : 5000

enter amount to withdraw : 1000

transaction successful

ur current balance is 4000

Account information for month of november

-----------------------------------------

customer id :23

name: mahesh

interest for month : 15.21

amount=4000

thank you !!

for new current account

40
enter id : 6

name : ram

amount : 1000

enter amount to deposit : 200

current amount is : 1200

enter amount to withdraw : 300

transaction successful

ur current balance is 900

current Account information for month of november

-----------------------------------------

customer id :6

name: ram

interest for month : 5.718

amount=900

thank you !!

41
Develop a test to establish order of constructor and destructor

#include<iostream.h>

#include<conio.h>

class base

public :

base()

cout<<"\n base class constructor";

~base()

cout<<"\n base class destructor";

};

class derived:public base

public :

derived()

cout<<"\n derived class constructor";

~derived()

cout<<"\n derived class destructor";

};

42
void main()

derived d;

getch();

OUTPUT

-------

base class constructor

derived class constructor

derived class destructor

base class destructor

Copy constructor invoke

#include <iostream.h>

#include <string.h>

class base

int wheels;
43
public:

base(int n)

wheels = n;

int get_wheels()

return wheels;

};

// Public inheritance of the base class - pass constructor arg n

class car:public base

char name[20];

public:

car(char * s, int n) : base(n) //preventing dangling pointer

strcpy(name, s);

void show();

};

void car::show()

cout << "Car " << name << " has " << get_wheels() << " wheels.\n";

void main()

44
car xr3("XR3 'Orrid", 4);

xr3.show();

return(0);

OUTPUT

-------

car xr3 orrid has 4 wheels

Calculate the surface area and volume

#include<iostream.h>

#include<conio.h>

class shape

public:

float sa,vol;

void sarea(float r,float h)

sa= (2*3.147*r*r)+(2*3.147*r*h);

cout<<"\n surface area of cylinder= "<<sa;

45
void sarea(float r)

sa=4*3.147*r*r;

cout<<"\n surface area of sphere = "<<sa;

void volume(float r)

vol=(4/3)*3.147*r*r*r;

cout<<"\n volume of sphere = "<<vol;

void volume(float r,float h)

vol=3.147*r*r*h;

cout<<"\n volume of cylinder = "<<vol;

};

class cylinder:public shape

float r,h;

public:

cylinder()

cout<<"radius :"; cin>>r;

cout<<"\n height :"; cin>>h;

void area()

46
sarea(r,h);

void volu()

volume(r,h);

};

class sphere:public shape

float r;

public:

sphere()

cout<<"\n radius of sphere :"; cin>>r;

void area()

sarea(r);

void volu()

volume(r);

};

void main()

cylinder c;

47
sphere s;

c.area();

s.area();

c.volu();

s.volu();

getch();

OUTPUT :

------------------

radius : 11

height : 3

radius of sphere : 6

surafce area of cylinder : 969.276001

surafce area of sphere : 453.197999

volume of cylinder :1142.360982

volume of sphere : 679.752014

Total tax should paid at the end each day

#include<iostream.h>

#include<conio.h>

class room

int single,doubl;

float tax,gincome;

public:

48
room()

tax=0;

gincome=0;

void getdata()

cout<<"\n single room occupied (<100) "; cin>>single;

cout<<"\n double room occupied (<200) "; cin>>doubl;

void cal_tax()

float exp=0,lux=0,income=0;

income= (single*3000) + (doubl*5000);

exp=0.1*income;

lux=0.3*income;

cout<<income;

tax=exp+lux;

gincome=income-tax;

void detail()

cout<<"\n\nINCOME DETAILS";

cout<<"--------------------";

cout<<"\n tax payable "<<tax;

cout<<"\n gross income "<<gincome;

};

49
void main()

room r;

r.getdata();

r.cal_tax();

r.detail();

getch();

OUTPUT

-------

single room occupied(<100) : 20

double room occupied(<200) : 102

INCOME DETAILS

---------------

tax payable : 29324.21

gross income : 79734.36

50
Calculate the surface area and volume

#include<iostream.h>
#include<conio.h>
class shape
{public:
float sa,vol;
void sarea(float r,float h)
{ sa= (2*3.147*r*r)+(2*3.147*r*h);
cout<<"\n surface area of cylinder= "<<sa; }
void sarea(float r)
{ sa=4*3.147*r*r;
cout<<"\n surface area of sphere = "<<sa; }
void volume(float r)
{ vol=(4/3)*3.147*r*r*r;
cout<<"\n volume of sphere = "<<vol; }
void volume(float r,float h)
{ vol=3.147*r*r*h;
cout<<"\n volume of cylinder = "<<vol; }};

class cylinder:public shape


{float r,h;
public:
cylinder()
{ cout<<"radius :"; cin>>r;
cout<<"\n height :"; cin>>h; }
void area()
{ sarea(r,h); }
void volu()
{ volume(r,h); }};

class sphere:public shape


{float r;public:
sphere()
{ cout<<"\n radius of sphere :"; cin>>r; }
void area()
{ sarea(r); }
void volu()
{ volume(r); }};

class cube:public shape


{float side;
public:
cube()
{ cout<<"\nside of cube :"; cin>>side; }
void sarea()
{ sa=6*side*side;
cout<<"\n surface area of cube :"<<sa; }
void volume()
{ vol=side*side*side;
cout<<"\n volume of cube :"<<vol; }};
void main()
{cylinder c; sphere s;
cube cu; c.area(); s.area();
c.volu(); s.volu(); cu.sarea();
cu.volume(); getch();}

51
OUTPUT :

------------------

radius : 11

height : 3

radius of sphere : 6

side of cube : 9.1

surafce area of cylinder : 969.276001

surafce area of sphere : 453.197999

volume of cylinder :1142.360982

volume of sphere : 679.752014

surface area of cube : 496.860046

volume of cube : 753.571106

Generic queue of element

#include<iostream.h>

#include<iomanip.h>

#include<ctype.h>

template<class Type>

class queue

private:

Type size;

Type start;

Type end;
52
Type *data;

public:

queue()

size=20;

start=end=0;

data=new Type[size];

queue(Type n)

size=n;

start=end=0;

data=new Type[size];

~queue()

delete data;

void put(Type value)

if((start+1)size==end)

cout<<"\n***Queue is full!***\n";

return ;

data[start]=value;

start=(start+1)size;

cout<<"You have put a data into the queue!\n";

53
return;

Type get()

Type value;

if(start==end)

cout<<"\n***Queue is empty!***\n";

return(0);

value=data[end];

end=(end+1)size;

cout<<"\n Get "<<value<<" from the queue!\n";

return(value);

void clear()

start=end;

cout<<"\n ***Queue is empty!***\n";

void ShowQueue()

if(start==end)

cout<<"\n The queue has no data!\n";

return;

54
Type i;

cout<<"\n The content of queue:\n";

for(i=end;i!=start;i=((i+1)size))

cout<<setw(5)<<data[i];

cout<<"\n\n";

};

main()

queue<char> ss(5);

char value = 'a';

ss.put(value);

ss.put('b');

value=ss.get();

ss.clear();

ss.ShowQueue();

Build a generic class array

55
#include<iostream.h>

#include<conio.h>

template <class t,int N>

class array

t a[N];

public:

void setdata(int x, t value)

a[x]=value;

void getresult()

int i;

t sum=0,pro=1;

for( i=0;i<N;i++)

sum=sum +a[i];

pro=pro*a[i];

cout<<"\n sum = "<<sum;

cout<<"\n product= "<<pro;

};

void main()

array<int,5> integer;

array<float,3> real;

56
clrscr();

integer.setdata(0,4);

integer.setdata(1,54);

integer.setdata(2,94);

integer.setdata(3,76);

integer.setdata(4,11);

real.setdata(0,3.65);

real.setdata(1,65.29);

real.setdata(2,29.5);

cout<<"\n\n for integer array : ";

integer.getresult();

cout<<"\n\n for real array : ";

real.getresult();

getch();

OUTPUT

---------

for integer array:

sum = 239

product = 16974144

for real array:

sum=98.44

57
product= 6030.10075

58

You might also like