100% found this document useful (1 vote)
109 views7 pages

Department of Computer Science: Bahria University, Lahore Campus

The document describes four tasks to practice object-oriented programming concepts in C++. The tasks involve defining classes for an employee, book, and flight with data members and member functions.

Uploaded by

qfq
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
100% found this document useful (1 vote)
109 views7 pages

Department of Computer Science: Bahria University, Lahore Campus

The document describes four tasks to practice object-oriented programming concepts in C++. The tasks involve defining classes for an employee, book, and flight with data members and member functions.

Uploaded by

qfq
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/ 7

Bahria University, Lahore Campus

Department of Computer Science


Lab Journal 02
(Spring 2020)

Course: Object Oriented Programming - Lab Date: __19 feb 2020_____


Course Code: CSL-210 Max Marks: 10
Faculty’s Name: Ms. Zupash Awais

Name: USAMAADNAN____ Enroll No: __057_____________ Class: __2b__________

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

};

void employee:: getinfo(int a, string b, int c, int d, string e)


{
Emp_id = a;
Emp_name = b;
Emp_age = c;
Emp_salary = d;
Emp_email = e;
%
Enrollment Number: ____________________________

void employee:: displayinfo()


{
cout << "Entered id:";
cout << Emp_id; cout << endl;
cout << "Entered Name :";
cout << Emp_name; cout << endl;
cout << "Entered age" << Emp_age; cout << endl;
cout << "Entered Salary :" << Emp_salary; cout << endl;
cout << "Entered Email :" << Emp_email; cout << endl;

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: ____________________________

void getinfo(int, string, int, int, string);


void displayinfo();

};

void employee:: getinfo(int a, string b, int c, int d, string e)


{
Emp_id = a;
Emp_name = b;
Emp_age = c;
Emp_salary = d;
Emp_email = e;

void employee:: displayinfo()


{
cout << "Entered id:";
cout << Emp_id; cout << endl;
cout << "Entered Name :";
cout << Emp_name; cout << endl;
cout << "Entered age" << Emp_age; cout << endl;
cout << "Entered Salary :" << Emp_salary; cout << endl;
cout << "Entered Email :" << Emp_email; cout << endl;

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();
};

void book:: takedata(int i, string k, int p)


{
bookno = i;
booktitle = k;
price = p;
}

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;

b1.takedata(2, "b", 1600);


int n;
do{
cout << "Enter choice...\n 1)-press 1 for show books \n 2)-press 2 for
purchase books \n 0)-press to exit \n ";

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);

} while (n != 0 && n>0 && n<3);

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);

} while (n != 0 && n>0 && n<2);

return 0;
}

Lab Grading Sheet :


Max
Obtained
Task Mark Comments(if any)
Marks
s
1. 02
2. 02
3. 03
4. 03
Total 10 Signature

Note : Attempt all tasks and get them checked by your Lab Instructor.

Page 7 of 7

You might also like