0

When I run this code and create an instance of cylinderType by passing four parameters, debugger shows the height I want but, radius=x=y=0. So when I call method printVolume() on this object, it displays '0'. Am I missing something important with inheritance?

Thank you~

#include <iostream>
using namespace std;
class circleType
{
public:
    circleType();
    circleType(double r);
    double getArea() const;
private:
    double radius;
};
class cylinderType : public circleType
{
public:
    cylinderType(double h, double r);
    void printVolume() const;

private:
    double height;
};


int main()
{
    cylinderType cylinderA(2, 4);
    cylinderA.printVolume();

    return 0;
};

circleType::circleType()
{
    radius = 0;
};
circleType::circleType(double r)
{
    radius = r;
};
double circleType::getArea() const
{
    return (3.14 * radius* radius);
};

cylinderType::cylinderType(double h, double r)
{
    circleType::circleType(r);
    height = h;
};
void cylinderType::printVolume() const
{
    cout << (circleType::getArea() * height);
};
djphp
  • 1
  • 1
  • can you share more about the error – Raghu Ariga Feb 18 '18 at 04:46
  • Sure. For example, in main.cpp, I write: cylinderType cylinderB(14.1, 5.5, 2.1, -1.2); When I breakpoint after this, cylinderB's members are height = 14.1, radius = 0, x = 0, y = 0. When I alter the circleType's default constructor to set radius = 2 then, cylinderB's members are height = 14.1, radius 2, x = 0, y = 0. It's almost like cylinderType draws from circleType's default constructor even though I pass it with three parameters to draw its overloaded constructor – djphp Feb 18 '18 at 04:50
  • Went to add some indentation to your code and quickly notice that it can't compile at all. This makes it very hard for you to get any output from running the program. This could be the result of bundling up the program into a nice minimal example, but it ruins the complete and verifiable we normally ask for. – user4581301 Feb 18 '18 at 04:50
  • 1
    Once I clean up the code to the point where it compiles I seem to have eliminated your reported bug. Going to need a [mcve] to help you out. – user4581301 Feb 18 '18 at 04:55
  • Having cylinder extend circle extending point is not good design. Suffixing your type names with `Type` is just awful. – Aluan Haddad Feb 18 '18 at 04:58
  • 1
    `circleType::circleType(r,x,y);` doesn't do what you think. You need to use the constructor initialization list to pass parameters to the base class. That's also not a complete example as it does not compile. If, as you've shown, you don't have default constructors for your base classes then you would have a compile error. – Retired Ninja Feb 18 '18 at 05:02
  • 4
    Possible duplicate of [What are the rules for calling the superclass constructor?](https://stackoverflow.com/questions/120876/what-are-the-rules-for-calling-the-superclass-constructor) – Retired Ninja Feb 18 '18 at 05:04
  • Sorry completely edited so that it will run in a single .cpp file now and removed pointType for now – djphp Feb 18 '18 at 05:06
  • @RetiredNinja Aw shoot. I made that change without even thinking. No wonder I didn't see the bug. – user4581301 Feb 18 '18 at 05:15
  • Okay so I definitely need an initialization list as shown in the provided links. Will work on it, Thanks everyone! – djphp Feb 18 '18 at 05:22
  • The most important thing about inheritance is that it expresses the *is-kind-of* relationship. A cylinder is not a kind of circle, so inheritance is not appropriate here. – n. m. could be an AI Feb 18 '18 at 06:42

0 Answers0