<< Chapter < Page | Chapter >> Page > |
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:
Notification Switch
Would you like to follow the 'Programming fundamentals in c++' conversation and receive update notifications?