C++ Tutorial: Some Lab assignments on Objects and Classes

Question: Write a simple program that convert the temperature in degree Celsius to degree Fahrenheit  and vice versa using the basic concept of class and object. Make separate class for Centigrade and Fahrenheit which will have the private member to hold the temperature value and make conversion functions in each class for conversion from one to other. For example you will have function toFahrenheit() in class Celsius that converts to Fahrenheit scale and returns the value.

Solution:

 
#include 
 
using namespace std;
 
 
 
class Celcius{
 
private:
 
float c;
 
public:
 
void readCelcius(){
 
coutEnter temperature in Celcius: ";
 
cin>>c;
 
}
 
float toFarenheit(){
 
return((9.0/5.0)*c+32);
 
}
 
};
 
 
 
class Farenheit{
 
private:
 
float f;
 
public:
 
void readFarenheit(){
 
coutEnter temperature in Farenhiet: ";
 
cin>>f;
 
}
 
float toCelcius(){
 
return((5.0/9.0)*(f-32));
 
}
 
};
 
 
 
int main(){
 
Celcius c1;
 
Farenheit f1;
 
c1.readCelcius();
 
cout F"
 
f1.readFarenheit();
 
cout C";
 
return 0;
 
}
 

Question: Write a program using basic concept of objects and classes to check whether given number is prime or not.

Solution:

 
#include 
 
using namespace std;
 
 
 
class prime{
 
private:
 
int num;
 
public:
 
void readnumber(){
 
coutEnter a number ";
 
cin>>num;
 
}
 
bool isPrime(){
 
int p = 2;
 
while(p
 
if((num%p++) == 0){
 
return false;
 
}
 
}
 
return true;
 
}
 
};
 
 
 
int main(){
 
prime p1;
 
char res;
 
coutPrime Checking program"
 
do{
 
p1.readnumber();
 
if(p1.isPrime())
 
coutit is a prime number";
 
else
 
coutit is not a prime number";
 
coutDo you want to do another check(y/n): ";
 
cin>>res;
 
}while(res == 'y');
 
return 0;
 
}
 
Question: Create a class called carpart that has int member data for car id, int member data for charge/hour and float member data for time. Set the data and show the charges and parked hours of corresponding car id. Make two member functions for setting and showing the data. Member function should be called from other functions.
Solution:
 
#include 
 
using namespace std;
 
 
 
class carpart{
 
private:
 
int carid;
 
float charge,time;
 
public:
 
void setdata(int id, float chag, float t){
 
carid = id;
 
charge = chag;
 
time = t;
 
}
 
void showdata(){
 
coutcar" Charges"
 
coutCharge = "
 
coutParked Hours = " hours";
 
}
 
};
 
 
 
int main(){
 
carpart c1;
 
c1.setdata(505,10,5.5);
 
c1.showdata();
 
return 0;
 
}
 
 
 
Question: Assume that object represents an employee report that contains the information about employee id, total bonus, total overtime in a particular year. Use four objects to represent four employees' reports. Write a program that display report information. Use setpara() member function to set report attributes by passing the arguments and member function displayreport() to show the reports according to parameter passed.
Solution:
 
#include 
 
using namespace std;
 
 
 
class employee{
 
private:
 
int empid;
 
float bonus,otime;
 
int year;
 
public:
 
void setpara(int id, float bns, float ot,int y){
 
empid = id;
 
bonus = bns;
 
otime = ot;
 
year = y;
 
}
 
void displayreport(){
 
coutEmployee with " had received Rs. " as bonus \nand\n";
 
couthad worked " hours as a overtime in year "
 
}
 
};
 
 
 
int main(){
 
int n;
 
coutEnter number of employees:- "; cin>>n;
 
employee e[n];
 
int eid,yr; float bons,overt;
 
coutEnter the following information of employees \n";
 
for(int i = 0;i
 
coutEmployee Id:- "; cin>>eid;
 
coutBonus:- "; cin>>bons;
 
coutOver Time:- "; cin>>overt;
 
coutYear:- "; cin>>yr;
 
e[i].setpara(eid,bons,overt,yr);
 
}
 
cout\n REPORT \n";
 
for(int i = 0;i
 
e[i].displayreport();
 
}
 
return 0;
 
}