### 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:
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950`` ``````#include "stdafx.h" #include # 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="<

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:
 ``12`` ``````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:
 ``12`` ``````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

 ``12345678`` `````` 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:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748`` ``````#include 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.