Inheritance-call function from base class

Hello,

Thank you for the last time when i got a solution for my c++ begginer problems.
Today i learnt about inheritance and i tried to apply some things. Here is my 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
#include "stdafx.h"
#include <iostream>
# define pi 3.14
using namespace std;

class Circle{
protected:
	double radius;
public:
	void init_radius (double);
	double area() {return pi*radius*radius;}
};
void Circle::init_radius(double a){
radius = a;
}

class Cylinder : public Circle {
	double height;
public:
	double volume(); 
	void init_height(double);
	double area(){return 2*Circle::area()+ 2*pi*radius*height;}
};
void Cylinder::init_height (double a){
height = a;
}
double Cylinder::volume()
{
	return Circle::area()*height;
}



int main()
{
	double radius, height;
	cout<<"radius="; cin>>radius;
	cout<<"height="; cin>>height;
	Circle a;
	Cylinder b;
	a.init_radius(radius);
	b.init_height(height);
	cout<<"area circle="<<a.area()<<endl;
	cout<<"area cylinder="<<b.area()<<endl;
	cout<<"volume cylinder="<<b.volume()<<endl;


system("pause");
return 0;
}


I have a class Circle where I compute the area of the circle. Then I have a derived class, Cylinder, where i want to compute the area and the volume. I have no errors about syntax or something else, but when i run i get something like Circle::area() doesn`t work in Cylinder class. I can`t understand what is wrong. Maybe somebody will be nice and explain me few things. Thanks in advance.
┬┐Do you understand what Circle foo, bar; does?
Yes, declare two objects of Cricle type, so?
Your problem is that you're never initializing radius in the base class of b.
a and b are separate instances. Just because Cylinder inherits from Circle, doesn't mean a and b are related.

There are multiple ways to accomplish this. The traditional way is with constructors.
Line 39:
 
Circle a (radius);

This implies that Circle has a constructor that takes a double:
1
2
Circle::Circle(double r)
{  radius = r; } 


Line 40 - Construct a Cynlinder from a circle and a height.
 
Cynlinder b (a, height);

This requires that Cynlinder has a constructor that accepts a Circle and a double for the height:
1
2
Cynlinder::Cynlinder (const Circle & c, double h) : Circle (c)
{ height = h; }


Delete lines 41 and 42.





Though you do inherit radius from circle, it's initialized with garbage value and hence the results.

When you are using inheritance, you have to think of the "is-a" relationship. In your example, cylinder inherits from circle, but the cylinder is clearly not a circle. It makes more sense for these to be separate classes and to also have an instance of Circle as a member in Cylinder

1
2
3
4
5
6
7
8
 
class Cylinder 
{
private: 
    double height; 
    Circle   base; 
... 
}; 


Thanks for answers, i analyzed your code and i understand the mistake. First idea is to believe that the instances are related. Thanks again!
Prefer to use constants to #define macros in C++.

Without going into detail here, it should be mentioned that in the following code, area() should be a virtual function, but since we're only dealing with static types it doesn't make a difference here. Just remember it when you're introduced to virtual class methods.

Generally, you should use constructors rather than intialization functions.

Rewritten to be correct:

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
#include <iostream>
const double pi = 3.14 ;

class Circle {
public:
    Circle( double radius ) : _radius(radius) {}

    virtual double area() const { return pi*_radius*_radius ; }
    double radius() const { return _radius ; }

protected:
    double _radius ;
};

class Cylinder : public Circle {
public:
    Cylinder( double radius, double height ) 
        : Circle(radius), _height(height) {}

    double volume() const { return area() * _height ; }
    double area() const { return 2 * Circle::area() + 2*pi*_radius* _height ; }

protected:
    double _height ;
};


int main()
{
    using std::cout ;
    using std::cin ;

    double radius, height;
    cout << "radius= "; 
    cin>>radius;

    cout<<"height= "; 
    cin>>height;
    
    Circle a(radius) ;
    Cylinder b(radius, height) ;

    cout << "area circle= " << a.area();
    cout << "\narea cylinder= " << b.area();
    cout << "\nvolume cylinder= " << b.volume()<< '\n' ;

    return 0;
}


I realize you came up with this off-the-cuff as a way to test your knowledge, but, just for the record, this is a poor candidate for inheritance.
Topic archived. No new replies allowed.