Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
619 views

CS304P - Lab Exercises

The document provides instructions for students to solve problems independently without copying solutions. It includes 3 problem statements with sample inputs and outputs for coding exercises involving classes. The first problem asks students to identify objects, attributes, and behaviors from a scenario. The second asks students to apply generalization to file classes. The third asks students to create a Room class with different constructors. The problems provide guidance for students to practice object-oriented programming concepts.

Uploaded by

tariq qaisrani
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
619 views

CS304P - Lab Exercises

The document provides instructions for students to solve problems independently without copying solutions. It includes 3 problem statements with sample inputs and outputs for coding exercises involving classes. The first problem asks students to identify objects, attributes, and behaviors from a scenario. The second asks students to apply generalization to file classes. The third asks students to create a Room class with different constructors. The problems provide guidance for students to practice object-oriented programming concepts.

Uploaded by

tariq qaisrani
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Dear Student,

You have to solve the given problem statement by yourself. First read it carefully and try to
solve it. There is no need to copy it from anywhere or to solve it in the group. Try to analyze
yourself. There are no marks for this activity. It is only for your own understanding. In the
coming lab, we are going to perform this lab, we will discuss your solution, and try to find out
the mistakes.

Lab 01
Problem Statement
Consider the following scenario and identify all objects, their attributes and behaviors.

In Virtual University bookshop, the handouts of all courses are provided at reasonable price.
The handouts are made in accordance with the video lectures recorded by famous professors. It
is a step to facilitate students to easily digest the course contents.

Solution:
Object Attribute Behaviors

Bookshop Name Set Order

Address (Location) Get Order

Phone number Confirm order

List of Books Deliver Order

Update inventory

Payment collection

Order status

Handout Title Upload

No. of topics/chapters Download

Author name Edit


Publisher name Open

Creation date Close

Modify date Read

Edition Write

Price Share

Book form View

Course Course code Add information

Course name View information

Course instructor Edit information

Credit hours Read

Total marks Write

Grading scheme

No. of lectures

Course content

Video Lecture Video Name View / watch

Video number Record

Duration Upload

Quality Download

Type Stop

Video URL Share

caption Pause
Forward

Reverse

Increase_speed

Decrease_speed

Increase_volume

Decrease_volume

Playback

Professor Employee ID Teach

Name Develop assignment

Age Solve student’s problems

Address Develop quiz

Contact Number Develop GDB

Gender Prepare Exam

Qualification Take Exam

Designation Invigilator

Experience Mark papers

Salary Mark activities

No. of taught subjects Give Grades

Department Prepare Exam

Students Student ID / Roll number Study

Name Give Exam


Age Attempt Activities

Gender Write notes

Address Making time table

Contact number

Student program

Department

Semester

CGPA

N. of course

Course content Name Upload

No. of topics Edit

Topic Number View

Topic Description Share

Composed by Write

Published by Download

Written by Delete
Lab 02
Problem Statement
In the below diagram, we have two different types of file classes (PDF and Video). You are
required to apply the concept of generalization to the given classes and draw a class diagram
showing this generalization relationship.

Solution
Lab 03
Write a program which consists of a class named Room having two data members
Height and Width, the class should also consist of three constructors i.e., Default
constructor, one argument constructor and two arguments constructor.

After running your program, the following screen should display.

Solution
#include<iostream>
using namespace std;
class Room
{
private: //private variable of type float
float width;
float height;
public:
Room() //default constructor
{
width=0.0; //default value has been assigned to width
height=0.0; //default value has been assigned to height
cout<<"Default Constructor is called"<<endl;
cout<<"Width: "<<width<<endl;
cout<<"Height: "<<height<<endl;
}
Room(float w) //paramterized (one argument)
{
width=w;
height=0.0;
cout<<endl<<endl<<"One Argument Constructor is called"<<endl;
cout<<"Width: "<<width<<endl;
cout<<"Height: "<<height<<endl;
}
Room(float w, float h) //paramterized (two argument)
{
width=w;
height=h;
cout<<endl<<endl<<"Two Argument Constructor is called"<<endl;
cout<<"Width: "<<width<<endl;
cout<<"Height: "<<height<<endl;
}
};
int main()
{
Room R1,R2(10.90),R3(20.89, 50.78); //objects of class Room
return 0;
}
Lab 04
Write C++ coding program which creates a class Pyramid. The class should contain a static data
member “Pcount” to store the total number of Pyramid’s objects.

In the main() function, dynamically create three objects of Pyramid class and print the value of
“Pcount”. Afterwards, delete any two objects of Pyramid and print the value of “Pcount” again.

Following is a sample output for above scenario:

Solution
#include<iostream>
using namespace std;
class Pyramid
{
private:
static int Pcount; //private static variable of type int
public:
static int get_objectCount();
//static function to get the object count
Pyramid(); //constructor
~Pyramid(); //destructor
};
int Pyramid::Pcount=0;

int Pyramid::get_objectCount() //defination of function


get_objectCount
{
return Pcount;
}

Pyramid::Pyramid() //defination of constructor


{
cout<<endl<<"Pyramid Constructor is called."<<endl;
Pcount++; //increment the value of Pcount
}
Pyramid::~Pyramid() //defination of destructor
{
cout<<endl<<"Pyramid Destructor is called."<<endl;
Pcount--; //decrement the value of pcount
}
int main()
{
//This statement is creating an object dynamically and binds its
address to pointer.
//This is the declaration of variable named P1, P2, P3, whic is a
pointer
//to a Pyramid. This pointer object has automatic storafg
defination.
//it is then inirialized with the result of New Pyramid.
//This new keyword create a onject with dynamic memory and
then return a pointer to it.
Pyramid *P1 = new Pyramid;
Pyramid *P2 = new Pyramid;
Pyramid *P3 = new Pyramid;

cout<<endl<<"Total Objects :
"<<Pyramid::get_objectCount()<<endl<<endl;

delete P1;
delete P2;

cout<<endl<<"Total Objects :
"<<Pyramid::get_objectCount()<<endl<<endl;
system ("pause");
}
Lab 05
Problem Statement
Write a C++ program which consists of a class named Circle which has one data member radius and
setter and getter functions. Overload the + operator for this class.

In main () function, create three objects c1, c2, and c3. Call setter function for objects c1 and c2. The
program should be able to execute the following statement;

c3=c1+c2;

Call the getter function for c3.

After running your program, the following screen should display.

Sample Output

Solution:
#include<iostream>
using namespace std;
class circle{
private:
double radius;
public:
void setRadius(double r){
radius=r;
}
double getRadius(){
return radius;
}

circle operator+(const circle& c){


circle cir;
cir.radius=this->radius+c.radius;
return cir;
}
};

int main (){


circle c1, c2, c3;
double r;
c1.setRadius(6.0);
c2.setRadius(12.0);
c3=c1+c2;
r=c3.getRadius();
cout<<"Radius of Third Circle:"<<r<<endl;

Lab 06
Problem Statement
Write a C++ program which consists of a class named Complex which has data members real and
imaginary. Overload the stream insertion operator (<<) operator and stream extraction operator (>>)
for this class.

In main () function, create an object com1 and take input using stream extraction operator and print
output using stream insertion operator
After running your program, the following screen should display.
Sample Output
Solution:
#include<iostream>
using namespace std;

class Complex
{
private:
int real, imag;
public:
Complex(int r = 0, int i =0)
{
real = r;
imag = i;
}

friend ostream & operator << (ostream &outObj, const Complex


&com);

friend istream & operator >> (istream &inObj, Complex &com);


};
ostream & operator << (ostream &outObj, const Complex &com)
{

outObj << com.real;


outObj << "+" << com.imag << "i" << endl;
return outObj;
}
istream & operator >> (istream &inObj, Complex &com)
{

cout << "Please enter real part of complex number: ";


inObj >> com.real;
cout << "Please enter imaginary part of complex number: ";
inObj >> com.imag;
return inObj;
}
int main()
{
Complex com1;
cin >> com1;
cout << "The complex object is: "<<com1;
return 0;
}

You might also like