OBJECT ORIENTED PROGRAMMING LAB
OBJECT ORIENTED
PROGRAMMING LAB
Lab Journal Solution -04
DESIGNED BY ALI MUHAMMAD
Question:01
Create an employee class. The member data should comprise an int for storing
the employee number and a float for storing the employee’s compensation. Member functions should
allow the user to enter this data and display it. Write main()
function that allows the user to enter data for three employees and display it (Use
Array of objects)
CODE:
#include<iostream>
#include<conio.h>
using namespace std;
class Employee{
private:
int num;
float compensation;
public:
Employee() :num(0), compensation(0){}
void setValue(int a, float b){
num=a;
compensation=b;
}
void display(){
cout << "Employee number is: " << num << endl;
cout << "compensationn is: " << compensation;
}
};
int main(){
int x;
float y;
Employee e[3];
for (int i = 0; i < 3; i++){
cout << "Enter employee " << i + 1 << " number: ";
cin >> x;
cout << "Enter employee " << i + 1 << " compenstaion: ";
cin >> y;
cout << endl;
e[i].setValue(x, y);
}
for (int i = 0; i < 3; i++){
e[i].display();
cout << endl;
}
_getch();
return 0;
}
OUTPUT:
1|Page
Question:02
Create a C++ class named Sphere, which has one data members, radius of the
sphere. Write set() and get() methods to store and return the radius of the Sphere
class. It also has a function named Area() which calculates the area of the Sphere.
Input the radius from the user and calculate the area of the Sphere
CODE:
#include<iostream>
#include<conio.h>
using namespace std;
static float pi = 3.14;
class Sphere{
private:
int radius;
public:
Sphere() :radius(0){}
void set(int r){
radius = r;
}
int getR(){
cout << "Enter the radius : ";
cin >> radius;
return radius;
}
double Area(){
2|Page
float area;
area = 4 * pi* radius*radius;
return area;
}
};
int main(){
Sphere s1;
s1.getR();
cout << "Area of Sphere = " << s1.Area();
Sphere s2;
s2.set(32);
cout << endl << "Area of second Sphere = " << s2.Area();
_getch();
return 0;
}
OUTPUT:
3|Page