You are here: Home » Content » Inheritance

Inheritance

Module by: Dr. Duong Tuan Anh

1. OBJECTIVE

The objectives of Lab session 10 are (1) to get familiar with class inheritance; (2) to learn the workings of the constructor and destructor of a derived class.
This lab session will use the inheritance hierarchies in the following figure.
Figure 1: Class hierarchies in this Lab session

2. EXPERIMENT

2.1 Given the Point class defined as follows.
class Point{
private:
int color;
protected:
double x;
double y;
public:
Point(double x=0, double y=0){
this->x=x; this->y=y;
}
void move(double dx, double dy){
x=x+dx;
y=y+dy;
}
~Point(){
cout<<"Destructor Point called";
}
};
Derive from the class Point, another class named Point_Derive1 class, which is defined as follows.
class Point_Derive1:public Point{
private:
double z;
public:
Point_Derive1();
void move(double dx, double dy, double dz);
~Point_Derive1();
};
a. List out the data members and member functions of the Point_Derive1 class. And determine the access specifier of each of these members.
Derive from the class Point, another class named Point_Derive2 class, which is defined as follows.
class Point_Derive2:protected Point{
private:
double z;
public:
Point_Derive1();
void move(double dx, double dy, double dz);
~Point_Derive1();
};
b. List out the data members and member functions of the Point_Derive2 class. And determine the access specifier of each of these members.
Derive from the class Point, another class named Point_Derive3 class, which is defined as follows.
class Point_Derive3:private Point{
private:
double z;
public:
Point_Derive1();
void move(double dx, double dy, double dz);
~Point_Derive1();
};
c. List out the data members and member functions of the Point_Derive3 class. And determine the access specifier of each of these members.
2.2 Given the following program in which the Cylinder class is a subclass derived from the Circle class
#include <iostream.h>
#include <math.h>
const double PI = 2.0 * asin(1.0);
// class declaration
class Circle
{
protected:
double radius;
public:
Circle(double = 1.0); // constructor
double calcval();
};
// implementation section for Circle
// constructor
Circle::Circle(double r)
{
radius = r;
}
// calculate the area of a circle
double Circle::calcval()
{
return(PI * radius * radius);
}
// class declaration for the derived class
// Cylinder which is derived from Circle
class Cylinder : public Circle
{
protected:
double length; // add one additional data member and
public: // two additional function members
Cylinder(double r = 1.0, double l = 1.0) : Circle(r), length(l) {}
double calcval();
};
// implementation section for Cylinder
double Cylinder::calcval() // this calculates a volume
{
return length * Circle::calcval(); // note the base function call
}
int main()
{
Circle circle_1, circle_2(2); // create two Circle objects
Cylinder cylinder_1(3,4); // create one Cylinder object
cout << "The area of circle_1 is " << circle_1.calcval() << endl;
cout << "The area of circle_2 is " << circle_2.calcval() << endl;
cout << "The volume of cylinder_1 is " << cylinder_1.calcval() << endl;
circle_1 = cylinder_1; // assign a cylinder to a Circle
cout << "\nThe area of circle_1 is now " << circle_1.calcval() << endl;
return 0;
}
a. Run the program in a Visual C++ environment and determine the output of the program.
b. Modify the above program by deriving a subclass named Sphere from the base class Circle. Member functions of Sphere class include a constructor and a function named calcval() which returns the volume of the sphere. (The formula to compute the volume of a sphere is (4/3)*pi* R*R*R). And modify the main() function in order that it invokes all the member functions of the Sphere class.
2.3 Define a base class named Rectangle that contains two data members length and width. From this class, derive a subclass named Box which includes one more data member, depth. Two member functions of the base class are the constructor and a function named area() which returns the area of the rectangle. The derived class Box should have its own constructor and an overriding function area() which returns the surface area of the box and the function volume() that returns the volume of the box.
Write a complete C++ program which invokes all the member functions of the two above classes. Besides, the main() function also invokes the area() function of the base class to apply to a Box object. Explain the result of this function call.
2.4 a. Construct a class named CPoint that consists of the following data members and member functions:
  • x, y coordinates
  • a constructor with two parameters representing x, y coordinates (their default values are allowed to be 0)
  • a member function named display, which prints out two coordinates on the screen.
  • A member function named getInfo, which accepts two input data from the user for x, y coordinates.
  • Two member functions named setX, setY to update values to x, y, respectively.
  • Two member functions named getX, getY to retrieve values from x, y, respectively.
  • A member function named distance, which accepts as parameter an object of class CPoint and calculates the distance from the object invoking the function to the parameter object.b. Construct a derived class named CPoint_3D from the class CPoint, described as follows:
  • There is one more data member: coordinate z.
  • There are overriding member functions in CPoint_3D for all corresponding member functions in the class CPoint.c. Include the class constructed in a) and b) in a working C++ program. Have your program call all of the member functions in the CPoint_3D class.

Comments, questions, feedback, criticisms?

Send feedback