You are on page 1of 11

AIR UNIVERSITY

DEPARTMENT OF MECHATRONICS ENGINEERING

EXPERIMENT NO 2

Lab Title Introduction to classes


Student Name: Hammad Iftikhar Hanif
Reg. No: 201080

LAB ASSESSMENT:
Excellent Good Average Satisfactory Unsatisfactory
Attributes
(5) (4) (3) (2) (1)

Ability to Conduct
Experiment

Ability to assimilate the


results

Effective use of lab


equipment and follows
the lab safety rules

Total Marks: Obtained Marks:

LAB REPORT ASSESSMENT:


Excellent Good Average Satisfactory Unsatisfactory
Attributes
(5) (4) (3) (2) (1)

Data presentation

Experimental results

Conclusion
BEMTS-III Functions and Structures

Total Marks: Obtained Marks:

Date: Signature:

Department of Mechatronics Engineering

Data Structure and Object Oriented Programming Lab

Lab Report: 2
Introdroduction to Classes

Name: Hammad Iftikhar


Roll Number: 201080
Class: BEMTS-3A
Submitted To: Engr. M. Farooq Khan

2
Air University, Islamabad DS and OOP Lab

Date: 30 September, 2021


Lab Tasks

Q1. Create a class Book with three data members BookID, Pages and Price.
It also contains the following member functions:

• The get() function is used to input values (1st object)


• The show() function is used to display values
• The set() function is used to set the values of data members using parameters (2nd object)
• The getPrice() function is used to return the value of Price

The program should create two objects of the class and input values tor these objects. The
program displays the details of the most costly book.

#include <iostream> void textbooks::get()

using namespace std; {cout<<"Enter BookID: ";

class textbooks cin>>BookID;

{ cout<<"Enter number of Pages : ";

private: cin>>NoOfPages;

cout<<endl<<"Enter Price of Book : ";

int BookID,Paisay,NoOfPages; cin>>Paisay;

public: system("cls");

void get();

void show(); void textbooks::show()

void set(int a,int b,int c); {cout<<"BookID is:"<<BookID<<endl;

int getPaisay(); cout<<"No of Pages of book are:


"<<NoOfPages<<endl;

cout<<"Price of book
}; is:"<<Paisay<<endl;

3
BEMTS-III Functions and Structures

int main()

{ void textbooks::set(int a,int b,int c)

double x,y; {BookID=a;

Paisay=b;

textbooks bookA,bookB; NoOfPages=c;

bookA.get(); }

bookA.show(); int textbooks::getPaisay()

bookB.set(300,900,1500); {

x=bookA.getPaisay(); return Paisay;

y=bookB.getPaisay(); }

if(x>y)

cout<<"BookA is expensive.";

else if(y>x)

cout<<"BookB is expensive.";

else{

cout<<"Both textbooks are of


same Price.";

return 0;

4
Air University, Islamabad DS and OOP Lab

Q2) Create a class Student_Result that contains roll number, name, and marks of CP, OOP,
and DS. The class also contains the following member functions:

• The input() function is used to input the values in data members


• The show() function is used to displays the value of data members •
The total() function returns the total marks of a student.
• The avg() function returns the average marks of a student.

Create 2 instances of a class. One for Ali and other for Hassan. Get data for both Ali and
Hassan, display it. Calculate total marks and average for both the students and compare the
results.

#include<iostream> void result::input()

using namespace std; cout<<"Name :"<<endl;

cin>>name;

class result cout<<"Reg ID :"<<endl;

{ cin>>reg;

5
BEMTS-III Functions and Structures

private: cout<<"CP marks :"<<endl;

cin>>cp;

string name; cout<<"oop marks :"<<endl;

int reg; cin>>oop;

float cp,oop,ds; cout<<"DS marks :"<<endl;

cin>>ds;

public: system("cls");

void input(); float result::show()

float show(); {

int total(); cout<<"Student :


"<<name<<endl;
int average();
cout<<"Reg ID :
"<<reg<<endl<<endl;
};
cout<<"Marks
int main() scored :"<<endl<<endl;

{ cout<<"CP : "<<cp<<endl;

int a,b,c,d,e,f; cout<<"OOP :


"<<oop<<endl;
result studentA,studentB;
cout<<"DS :
studentA.input();
"<<ds<<endl<<endl;
studentB.input();
}
a=studentA.show();

b=studentA.total();
int result::total()
c=studentA.average();
{

6
Air University, Islamabad DS and OOP Lab

d=studentB.show(); int Total;

e=studentB.total(); Total=cp+oop+ds;

f=studentB.average(); return Total;

cout<<endl; }

cout<<endl;

int result::average()
if(studentA.average()>studentB.average
{
())
int avg;
cout<<"Student 1
performed better. "<<endl; avg=(cp+oop+ds)/3;

else cout << "Average =


"<<avg<<endl;
cout<<"Student 2 performed
better. "<<endl;

return 0; return avg;

} }

7
BEMTS-III Functions and Structures

Q3) Create a class called BankAccount that models a checking account at a bank. The program
creates an account with an opening balance, displays the balance, makes a deposit and a
withdrawal, and then displays the new balance.

#include <iostream> void bankacc::show()


#include <string> {
using namespace std; b=93748;
cout<<"Account Name =
class bankacc "<<endl<<accname<<endl;
{ cout<<"Initial balance =
private: "<<b<<endl;
cout << "Amount deposited =
string accname; "<<d<<endl;
double b,d,wd; cout << "Amount withdrawn =
"<<wd<<endl;

8
Air University, Islamabad DS and OOP Lab

public: cout << "Current balance =


"<<b+d-wd<<endl;
void get(); }
void show();

};

int main()
{
bankacc b;
b.get();
b.show();
return 0;
}

void bankacc::get()
{
cout<<"Enter Account
Name:";
cin>>accname;
cout<<endl<<"Enter the
amount to deposit :";
cin>>d;
cout<<endl<<"Enter the
amount to withdraw : ";
cin>>wd;
}

9
BEMTS-III Functions and Structures

Q4)
Create a class Rectangle. The class has attributes length and width. Provide methods that
calculate the perimeter and the area of the rectangle. Provide set and get methods for both
length and width. The set methods should verify that length and width are each floating point
number greater than or equal to 0.0 and less than 20.0. Write a program to test class
Rectangle.

#include<iostream> void r::get()


using namespace std; {
cout<<"Enter length of
class r rectangle :";
{ cin>>l;
private: cout<< "Enter Width of
rectangle :";
double a; cin>> w;
double l; }
double w;
double p; void r::show()
{
public: a=l*w;
cout<<"Area of rectangle is =
void get(); "<<a<<endl;
void show(); p=(l+w)*2;

10
Air University, Islamabad DS and OOP Lab

cout<<"Perimeter of rectangle
}; is = "<<p<<endl;
}
int main()
{
r Rect;
Rect.get();
cout << endl << "Your Answers are
: " << endl;
Rect.show();
return 0;
}

11

You might also like