Menu

Saturday, 3 August 2013

Chapter 6,Problem 6:Object Oriented Programming by Robert Lafore in C++ Solution Manual

//Engr. Ahmad Furqan Attari
//Chapter 6, Problem 6
#include <iostream>
#include <conio.h>
using namespace std;
enum etype{laborer, secretary , manager };
class date
{
private:
int day,month,year;
public:
void getdate(void)
{
char ch;
cin>>month>>ch>>day>>ch>>year;
}
void showdate(void)
{
cout<<month<<"/"<<day<<"/"<<year;
}
};
class employee
{
private:
int e_num;
float salary;
date join_date;
etype type;
public:
void enter_data(void)
{
cout<<"Enter employee number: ";
cin>>e_num;
cout<<"Enter employee's salary: ";
cin>>salary;
cout<<"Enter employee's date of joining (mm/dd/yyyy): ";
join_date.getdate();
cout<<"Enter employee's type (1.laborer; 2.secretary; 3.manager): ";
switch(getch())
{
case '1':
type=laborer;break;
case '2':
type=secretary;break;
case '3':
type=manager;break;
}
cout<<endl;
}
void display(void)
{
cout<<"Employee number: "<<e_num<<endl;
cout<<"Employee salary: "<<salary<<endl;
cout<<"Employee joining date :";
join_date.showdate();
cout<<endl<<"Employee type: ";
switch(type)
{
case laborer:
cout<<"Laborer"<<endl;break;
case secretary:
cout<<"Secretary"<<endl;break;
case manager:
cout<<"Manager"<<endl;break;
}
}
};
void main(void)
{
employee emp1,emp2,emp3;
cout<<"Enter data for employee 1 "<<endl;
emp1.enter_data();
cout<<"Enter data for employee 2 "<<endl;
emp2.enter_data();
cout<<"Enter data for employee 3 "<<endl;
emp3.enter_data();
cout<<"The entered data is "<<endl;
emp1.display();
cout<<endl;
emp2.display();
cout<<endl;
emp3.display();
getch();
}

Chapter 6,Problem 5:Object Oriented Programming by Robert Lafore in C++ Solution Manual

//Engr. Ahmad Furqan Attari
//Chapter 6, Problem 5
#include <iostream>
#include <conio.h>
using namespace std;
class date
{
private:
int day,month,year;
public:
void getdate(void)
{
char ch;
cout<<"Enter data in the format mm/dd/yyyy : ";
cin>>month>>ch>>day>>ch>>year;
}
void showdate(void)
{
cout<<month<<"/"<<day<<"/"<<year;
}
};
void main(void)
{
date today;
today.getdate();
cout<<"The date you entered is "<<endl;
today.showdate();
getch();
}

Chapter 6, Problem 4:Object Oriented Programming by Robert Lafore in C++ Solution Manual

//Engr Ahmad Furqan Attari
//Chapter 6, Problem 4
#include <iostream>
#include <conio.h>
using namespace std;
class employee
{
private:
int e_num;
float salary;
public:
void enter_data(void)
{
cout<<"Enter employee number: ";
cin>>e_num;
cout<<"Enter employee's salary: ";
cin>>salary;
}
void display(void)
{
cout<<"Employee number: "<<e_num<<endl;
cout<<"Employee salary: "<<salary;
}
};
void main(void)
{
employee emp1,emp2,emp3;
cout<<"Enter data for employee 1 "<<endl;
emp1.enter_data();
cout<<"Enter data for employee 2 "<<endl;
emp2.enter_data();
cout<<"Enter data for employee 3 "<<endl;
emp3.enter_data();
cout<<"The entered data is "<<endl;
emp1.display();
cout<<endl;
emp2.display();
cout<<endl;
emp3.display();
getch();
}

Chapter 6 , Problem 3:Object Oriented Programming by Robert Lafore in C++ Solution Manual

//Engr Ahamd Furqan Attari
//Chapter 6, program 3
#include <iostream>
#include <conio.h>
using namespace std;
class time
{
private:
int hour,minute,second;
public:
time(): hour(0),minute(0),second(0)
{}
time(int h,int m,int s): hour(h),minute(m),second(s)
{}
void display(void) const
{
cout<<hour<<":"<<minute<<":"<<second;
}
void add(time a, time b)
{
second=a.second+b.second;
if(second>59)
{
second-=60;
minute++;
}
minute+=a.minute+b.minute;
if(minute>59)
{
hour++;
minute-=60;
}
hour+=a.hour+b.hour;
}
};
void main(void)
{
const time a(4,25,43),b(2,53,23);
time c;
c.add(a,b);
a.display();
cout<<" + ";
b.display();
cout<<" = ";
c.display();
getch();

}

Chapter 6, Problem 2:Object Oriented Programming by Robert Lafore in C++ Solution Manual

//Engr Ahmad Furqan Attari
//Chapter 6, Program 2
#include <iostream>
#include <conio.h>
using namespace std;
char getkey(void);
class toolBooth
{
private:
unsigned int cars;
double money;
public:
toolBooth():cars(0),money(0)
{}
void payingCar(void)
{
cars++;
money+=0.5;
}
void nopayCar(void)
{
cars++;
}
void display(void)
{
cout<<"Total number of cars passed: "<<cars<<endl
<<"Total amount of money collected: "<<money<<endl;
}
};
void main(void)
{
char choice;
toolBooth booth;
do
{
cout<<"Enter p to count paying car"<<endl
<<"Enter n to count nopaying car"<<endl
<<"Enter Esc to show result and exit "<<endl
<<"Enter your choice: ";
choice=getkey();
switch(choice)
{
case 'p':
cout<<'p'<<endl;
booth.payingCar();
break;
case 'n':
cout<<'n'<<endl;
booth.nopayCar();
break;
case 27:
cout<<"Esc"<<endl;
booth.display();
break;
default:
cout<<"Invalid input "<<endl;
}
}while(choice!=27);
getch();
}
char getkey(void)
{
char ch;
if((ch=getch())==0)
return getch();
else
return ch;
}

Chapter 6 , Problem 1:Object Oriented Programming by Robert Lafore in C++ Solution Manual

//Engr Ahmad Furqan Attari
//Chapter 6, Program 1
#include <iostream> //for input output
#include <conio.h>  //for getch
using namespace std;
class Int
{
private:
int intvar;
public:
Int(): intvar(0)
{}
Int(int a):intvar(a)
{}
void show(void)
{
cout<<intvar;
}
void add(Int a,Int b)
{
intvar=a.intvar+b.intvar;
}
};
void main(void)
{
Int a(12),b=32,c;
c.add(a,b);
a.show();
cout<<'+';
b.show();
cout<<'=';
c.show();
getch();
}