help with inheritance. constructors/destructors

Hello. I am supposed to have this program print the constructor and destructor of the base class and the inheritance classes. The output is printing the base class correctly, but only the constructor for the inheritance classes. Help is greatly appreciated. OUTPUT FOLLOWS CODE.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <iostream>
#ifndef POINT_H
#define POINT_H

using namespace std;

class point
{
     public:
            point(double = 0.0, double = 0.0);
            ~point();
     protected:
             double x;
             double y;
};
#endif

point::point(double a, double b)
{
     x = a;
     y = b;
     
     cout << "point constructor:  " << "[" << x << "," << y << "]" << endl;
}

point::~point()
{
     cout << "point destructor:  " << "[" << x << "," << y << "]" << endl;
}

#ifndef CIRCLE_H
#define CIRCLE_H

class circle : public point
{
     public:
            circle(double r = 0.0, double x = 0.0, double y = 0.0);
            ~circle();
     protected:
             double radius;
             double area;
};
#endif

circle::circle(double r, double a, double b): point(a, b)
{
     radius = r;
     area = 3.141593 * (radius * radius);
     
     cout << "circle constructor: radius is  " << radius << "[" << x << "," << y << "]" << endl;
     cout << "area is:  " << area << endl;
}

circle::~circle()
{
     cout << "circle destructor:  radius is  " << radius << "[" << x << "," << y << "]" << endl;
     cout << "area is:  " << area << endl;
}

#ifndef CYLINDER_H
#define CYLINDER_H

class cylinder : public point
{
     public:
            cylinder(double r = 0.0, double h = 0.0, double x = 0.0, double y = 0.0);
            ~cylinder();
     protected:
             double radius;
             double height;
             double area;
};
#endif

cylinder::cylinder(double r, double h, double a, double b): point(a, b)
{
     radius = r;
     height = h;
     area = 3.141593 * (radius * radius) * height;
     
     cout << "cylinder constructor:  radius is  " << radius << "[" << x << "," << y << "]" << endl;
     cout << "height is:  " << height << endl;
     cout << "area is:  " << area << endl;
}

cylinder::~cylinder()
{
     cout << "cylinder destructor:  radius is  " << radius << "[" << x << "," << y << "]" << endl;
     cout << "height is:  " << height << endl;
     cout << "area is:  " << area << endl;
}

int main()
{
     {
          point p(30.0, 40.5);
     }
     
     cout << endl;
     circle circle1(6.0, 20.0, 16.5);
     cout << endl;
     cylinder cylinder1(12.0, 30.0, 20.0, 40.0);
     cout << endl;
     system("pause");
     return 0;
}


OUTPUT:

point constructor: [30,40.5]
point destructor: [30,40.5]

point constructor: [20,16.5]
circle constructor: radius is 6[20,16.5]
area is: 113.097

point constructor: [20,40]
cylinder constructor: radius is 12[20,40]
height is: 30
area is: 13571.7

Press any key to continue . . .
This program works correctly for me. You will not see the de-constructors execute until after you have "pressed any key" as the program is still in execution at this point.

After you press a key and the "return 0" is executed all of the de-constructors are called as you'd expect.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
point constructor:  [30,40.5]
point destructor:  [30,40.5]

point constructor:  [20,16.5]
circle constructor: radius is  6[20,16.5]
area is:  113.097

point constructor:  [20,40]
cylinder constructor:  radius is  12[20,40]
height is:  30
area is:  13571.7

Press any key to continue . . . 

cylinder destructor:  radius is  12[20,40]
height is:  30
area is:  13571.7
point destructor:  [20,40]
circle destructor:  radius is  6[20,16.5]
area is:  113.097
point destructor:  [20,16.5]
Last edited on
in line 63 there is class cylinder: public point
as far as i know....that public should be declared inside the braces....
i m just a beginner...so wanna know wht actually is going on??
Just as an FYI (the program should work as is):
You might consider declaring your destructors virtual. It won't change the way the program works now, but if you were ever to destroy an object via a base class pointer, it wouldn't work correctly.
closed account (z05DSL3A)
@milee

http://www.cplusplus.com/doc/tutorial/inheritance.html
Topic archived. No new replies allowed.