0% found this document useful (0 votes)
60 views4 pages

OOP LAB JOURNAL 04 28122022 015042am

This document discusses two programming exercises involving object-oriented programming in C++. The first exercise involves creating an Employee class with data members for employee number and compensation. Member functions allow entering and displaying this data for three employee objects stored in an array. The second exercise creates a Sphere class with a radius data member. Set and get methods store and return the radius. An Area method calculates the sphere's surface area using the radius and pi value. The program prompts the user for a radius and calculates two sphere areas.

Uploaded by

Nazia butt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views4 pages

OOP LAB JOURNAL 04 28122022 015042am

This document discusses two programming exercises involving object-oriented programming in C++. The first exercise involves creating an Employee class with data members for employee number and compensation. Member functions allow entering and displaying this data for three employee objects stored in an array. The second exercise creates a Sphere class with a radius data member. Set and get methods store and return the radius. An Area method calculates the sphere's surface area using the radius and pi value. The program prompts the user for a radius and calculates two sphere areas.

Uploaded by

Nazia butt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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

You might also like