Lab8
Exercise (1)
1. Write a class declaration section for each of the following specifications. In each case,
include a prototype for a constructor and a member method named ShowData( ) that can
be used to display data member values.
a. A class named Time that has integer data members named secs, mins, and hours
class Time
{
private :
int hrs;
int mins;
int secs;
public:
Time(int h, int m, int s);
void ShowData();
};
b. A class named Circle that has integer data members named xcenter and ycenter and a
double-precision data member named radius.
class Circle
{
private :
int xcenter;
int ycenter;
double radius;
public:
Circle (int x, int y, double r) ;
void Showdata( ) ;
};
Exercise (2)
a. Construct a class implementation section for the constructor and showData( )
member methods corresponding to the class declaration created for Exercise 1a.
Time::Time(int h, int m, int s)
{ hrs=h;
mins=m;
1
secs=s ;
}
void Time::Showtime()
{
Cout << hrs << " : " << mins << " : " << secs;
}
b. Construct a class implementation section for the constructor and showData( )
methods corresponding to the class declaration created for Exercise 1b.
. Circle :: Circle (int x, int y, double r)
{ xcenter = x;
ycenter = y;
radius = r ;
}
void Circle :: Showdata()
{
cout<< xcenter << “ “ << ycenter << “ “ << radius << “ “
<< endl;
}
Exercise (3)
. Construct a class named Rectangle that has double-precision data members named
length and width. The class should have member methods named perimeter( ) and
area( ) to calculate a rectangle’s perimeter and area, a member method named setData(
) to set a rectangle’s length and width, and a member method named showData( ) that
displays a rectangle’s length, width, perimeter, and area
b. Include the Rectangle class constructed in Exercise 4a in a working C++
program
Answer:
class Rectangle
{ private :
double length , width ;
public:
Rectangle(double l, double w )
{
length= l;
width = w;
2
}
void setData(double l, double w )
{
length= l;
width = w;
}
void perimeter( void)
{
cout << "perimeter = " << 2*( length + width) << endl ;
}
void area( )
{
cout << "Area = " << length * width << endl ;
}
void showData( )
{
cout << "length = " << length << " Width = " << width<< endl;
}
};
b. Include the Rectangle class constructed in Exercise 4a in a working C++
program
Answer:
int main()
{
Rectangle Room(5.5, 6.5);
Room.perimeter();
Room.area();
Room.showData();
return 0;
3
}
Exercise (4)
a. Create a base class named Rectangle containing length and width data members.
From this class, derive a class named Box with another data member named depth. The
member functions of the base Rectangle class should consist of a constructor and an
area() function. The derived Box class should have a constructor, a volume()
function, and an override function named area() that returns the surface area of the
box.
b. Include the classes constructed for Exercise 2a in a working C++ program.
Answer:
#include <iostream>
using namespace std;
class Rectangle
{
protected:
int length;
int width;
public:
Rectangle(int a = 0 , int b = 0)
{
length = a ;
width = b ;
}
int area()
{
return (length * width) ;
}
};
class Box : public Rectangle
{
private:
int depth ;
public:
Box(int a = 0)
{
depth = a ;
}
4
void setLW(int a , int b)
{
length = a ;
width = b ;
}
int volume()
{
return (Rectangle::area() * depth);
}
int area()
{
return (2 * (length + width) * depth) + (2 * Rectangle::area());
}
};
int main()
{
int x ;
Rectangle obj1(2 , 3);
Box obj2(4) ;
obj2.setLW(3,5);
cout << obj1.area() << endl;
cout << obj2.volume() << endl;
cout << obj2.area() << endl;
return 0 ;
}
5
Exercise (5)
1. Write a C++ program to find the volume and the surface area of two BOXES. The
program should contain a base class named Rectangle to find the area and perimeter
of the base of each box, and two derived classes: Volume class and SurfaceArea class.
Note that the volume = base area × height, and
The surface area = (base perimeter × height) + (2 * base area).
Answer
#include <iostream>
using namespace std;
class Rectangle
{
protected:
int length;
int width;
public:
int area()
{
return (length * width);
}
void setLW(int a , int b)
{
length = a ;
width = b ;
}
int perimeter()
{
return ((length + width) * 2);
}
};
class Volume : public Rectangle
{
private:
public:
int volume(int height)
{
return (height * Rectangle::area());
6
}
};
class SurfaceArea : public Rectangle
{
private:
public:
int SArea(int height)
{
return ((height * Rectangle::perimeter()) + (2 * Rectangle::area()));
}
};
int main()
{
Rectangle obj1;
Volume obj2;
SurfaceArea obj3;
obj2.setLW(2 , 3);
obj3.setLW(3 , 4);
cout << obj2.volume(5) << endl;
cout << obj3.SArea(5) << endl;
return 0 ;
}
Exercise (6)
7
Write a C++ program to find the volume of a BOX and a CUBE. The program should
contain two classes named Box and Cube. The first class obtains the volume of a box
with a rectangular base (length = 10 and width = 5), and the second class obtains the
volume of a cube (edge = 8). The program should create a new object called box to
find the volume of the box then delete it. After deleting Box object, the program should
create a new object called cube to find the volume of the cube then delete it.
Exercise (7)
Write a C++ program that displays the data members of two objects of a class (Obj1 &
Obj2) using a pointer class member. The class named First and contains a pointer
member named dispoint, and three data members as follows: an integer member called
x, a double precision member called y, and a string member called z. The member
functions of the class should consist of a constructor to initialize the data members of each
object with different data. Also, the class contains a display() function to display class
data members on the screen.