CS304P - Lab Exercises
CS304P - Lab Exercises
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
Update inventory
Payment collection
Order status
Edition Write
Price Share
Grading scheme
No. of lectures
Course content
Duration Upload
Quality Download
Type Stop
caption Pause
Forward
Reverse
Increase_speed
Decrease_speed
Increase_volume
Decrease_volume
Playback
Designation Invigilator
Contact number
Student program
Department
Semester
CGPA
N. of course
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.
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.
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;
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;
Sample Output
Solution:
#include<iostream>
using namespace std;
class circle{
private:
double radius;
public:
void setRadius(double r){
radius=r;
}
double getRadius(){
return radius;
}
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;
}