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.
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
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.