Department of Computer Science: Bahria University, Lahore Campus
Department of Computer Science: Bahria University, Lahore Campus
Objective(s):
Upon completion of this lab session, learners will be able to:
• Define classes and objects
Lab Tasks:
Task 1
Write a program to create class named employee.
Data Members:
Emp_id
Emp_name
Emp_age
Emp_email
Emp_salary
Member Functions:
void getinfo(int,string,int,int,string,int);
void displayinfo();
#include<iostream>
#include<string>
using namespace std;
class employee
{
int Emp_id,Emp_age,Emp_salary;
string Emp_name, Emp_email;
public:
void getinfo(int, string, int, int, string);
void displayinfo();
};
int main()
{
employee z;
int a, b, c;
string d, e;
cout << "Enter id :";
cin >> a;
cin.ignore();
cout << "Enter name :";
getline(cin, d);
cout << "Enter age :";
cin >> b;
cin.ignore();
cout << "Enter email :";
getline(cin, e);
cout << "Enter salary :";
cin >> c;
z.getinfo(a, d, b, c,e);
z.displayinfo();
return 0;
}
Task 2
Perform Task 1 using scope resolution operator and pointer object.
#include<iostream>
#include<string>
using namespace std;
class employee
{
int Emp_id,Emp_age,Emp_salary;
string Emp_name, Emp_email;
public:
Page 2 of 7
%
Enrollment Number: ____________________________
};
int main()
{
employee *z;
z = new employee;
int a, b, c;
string d, e;
cout << "Enter id :";
cin >> a;
cin.ignore();
cout << "Enter name :";
getline(cin, d);
cout << "Enter age :";
cin >> b;
cin.ignore();
cout << "Enter email :";
getline(cin, e);
cout << "Enter salary :";
cin >> c;
z->getinfo(a, d, b, c,e);
z->displayinfo();
return 0;
}
Page 3 of 7
%
Enrollment Number: ____________________________
Task 3
Define a Class BOOK with the following specifications:
Data Members:
bookNo
bookTitle
price
Member Functions:
total_cost( ) A function to calculate the total cost for N no of copies where N is
passed to function as argument.
take_data( ) A function to input bookNo, bookTitle, price.
purchase_info( ) A function to ask the user to input the number of copies to be
purchased. It invokes total_cost( ) and prints the total cost paid by
the user.
Write a menu driven program.
#include<iostream>
#include<string>
using namespace std;
class book
{
int bookno, price;
string booktitle;
public:
void takedata(int i,string k,int p);
int totalcost(int);
void purchaseinfo(int );
void display();
};
int book::totalcost(int n)
{
return n*price;
}
void book::purchaseinfo(int n)
{
cout<<totalcost(n);
}
void book:: display()
{
cout << "book no. :";
cout << bookno << endl;
cout << "book title :";
cout << booktitle << endl;
cout << "book price :";
cout << price;
Page 4 of 7
%
Enrollment Number: ____________________________
int main()
{
book b1;
cin >> n;
if (n == 1)
b1.display();
else if (n == 2)
{
int n_copies;
cout << "enter no of copies :";
cin >> n_copies;
b1.purchaseinfo(n_copies);
else if (n == 0)
exit(0);
return 0;
Task 4
Define a C++ class FLIGHT with the following description:
Data Members:
flightNo Integer
destination String
distance Float
fuel Float
Member Functions:
calc_fuel( ) To calculate value of fuel as per following criteria
Distance Fuel
<=1000 500
More than 1000 and <=2000 1100
More than 2000 2200
feed_info( ) To allow user to enter values for flightNo, destination, distance
and fuel. It should invoke function calc_fuel( ) to calculate
quantity of fuel.
show_info( ) To display all the data member values on screen.
#include<iostream>
Page 5 of 7
%
Enrollment Number: ____________________________
#include<string>
using namespace std;
class FLIGHT
{
int flightno;
string destination;
float distance, fuel;
public:
void calc_fuel();
void feed_info();
void show_info();
};
void FLIGHT::feed_info()
{
cout << "Enter Flight Number :";
cin >> flightno;
cout << "Enter Destination :";
cin.ignore();
getline(cin, destination);
cout << "Enter Distance :";
cin >> distance;
cout << "Enter Fuel :";
cin >> fuel;
calc_fuel();
void FLIGHT::calc_fuel()
{
if (distance <= 1000)
fuel = 500;
else if (distance > 1000 && distance < 2000)
fuel = 1100;
else
fuel = 2200;
void FLIGHT::show_info()
{
cout << "Entered Flight Number :";
cout << flightno; cout << endl;
cout << "Enter Destination :";
cout << destination; cout << endl;
cout << "Enter Distance :";
cout << distance; cout << endl;
cout << "Enter Fuel :";
cout << fuel; cout << endl;
int main()
{
FLIGHT a;
Page 6 of 7
%
Enrollment Number: ____________________________
int n;
do{
cout << "Enter choice...\n 1)-press 1 for to enter data \n 0)-press to
exit \n ";
cin >> n;
if (n == 1){
a.feed_info();
a.show_info();
}
else if (n == 0)
exit(0);
return 0;
}
Note : Attempt all tasks and get them checked by your Lab Instructor.
Page 7 of 7