## Wednesday, October 12, 2011

### 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 <iostream>

using namespace std;

class Celcius{

private:

float c;

public:

cout<<"Enter temperature in Celcius: ";

cin>>c;

}

float toFarenheit(){

return((9.0/5.0)*c+32);

}

};

class Farenheit{

private:

float f;

public:

cout<<"Enter temperature in Farenhiet: ";

cin>>f;

}

float toCelcius(){

return((5.0/9.0)*(f-32));

}

};

int main(){

Celcius c1;

Farenheit f1;

cout<<c1.toFarenheit()<<" F"<<endl;

cout<<f1.toCelcius()<<" 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 <iostream>

using namespace std;

class prime{

private:

int num;

public:

cout<<"Enter a number ";

cin>>num;

}

bool isPrime(){

int p = 2;

while(p<=num/2){

if((num%p++) == 0){

return false;

}

}

return true;

}

};

int main(){

prime p1;

char res;

cout<<"Prime Checking program"<<endl;

do{

if(p1.isPrime())

cout<<"it is a prime number";

else

cout<<"it is not a prime number";

cout<<endl<<"Do 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 <iostream>

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

cout<<"car"<<carid<<" Charges"<<endl;

cout<<"Charge = "<<charge*time<<endl;

cout<<"Parked Hours = "<<time<<" 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 <iostream>

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

cout<<"had worked "<<otime<<" hours as a overtime in year "<<year<<endl<<endl;

}

};

int main(){

int n;

cout<<"Enter number of employees:- "; cin>>n;

employee e[n];

int eid,yr; float bons,overt;

cout<<"Enter the following information of employees \n";

for(int i = 0;i<n;i++){

cout<<"Employee Id:- "; cin>>eid;

cout<<"Bonus:- "; cin>>bons;

cout<<"Over Time:- "; cin>>overt;

cout<<"Year:- "; cin>>yr;

e[i].setpara(eid,bons,overt,yr);

}

cout<<"\n REPORT \n";

for(int i = 0;i<n;i++){

e[i].displayreport();

}

return 0;

}


#### 1 comment:

1. This is a great site. It really helpful for the student