Professional Documents
Culture Documents
EXPERIMENT NO 2
LAB ASSESSMENT:
Excellent Good Average Satisfactory Unsatisfactory
Attributes
(5) (4) (3) (2) (1)
Ability to Conduct
Experiment
Data presentation
Experimental results
Conclusion
BEMTS-III Functions and Structures
Date: Signature:
Lab Report: 2
Introdroduction to Classes
2
Air University, Islamabad DS and OOP Lab
Q1. Create a class Book with three data members BookID, Pages and Price.
It also contains the following member functions:
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.
private: cin>>NoOfPages;
public: system("cls");
void get();
cout<<"Price of book
}; is:"<<Paisay<<endl;
3
BEMTS-III Functions and Structures
int main()
Paisay=b;
bookA.get(); }
bookB.set(300,900,1500); {
y=bookB.getPaisay(); }
if(x>y)
cout<<"BookA is expensive.";
else if(y>x)
cout<<"BookB is expensive.";
else{
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:
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.
cin>>name;
{ cin>>reg;
5
BEMTS-III Functions and Structures
cin>>cp;
cin>>ds;
public: system("cls");
float show(); {
{ cout<<"CP : "<<cp<<endl;
b=studentA.total();
int result::total()
c=studentA.average();
{
6
Air University, Islamabad DS and OOP Lab
e=studentB.total(); Total=cp+oop+ds;
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;
} }
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.
8
Air University, Islamabad DS and OOP Lab
};
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.
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