Search code examples
c++oopinheritancemulti-level

How to achieve Multilevel Inheritance with classes


I am trying to do some multilevel inheritance from the Shape class to Rectangle, Circle and Triangle classes. From Rectangle I need to inherit a Square class and print the area, info, etc.. as well as Ellipse from Circle and Isosceles from Triangle. So far my first inherited classes work fine, but whenever I try to make the "grandchildren" class work I cannot make it work. I have no idea what I might be doing wrong. Here is the code:

 #include <iostream>
 using namespace std;

 class Shape{
 protected:
 string name;
 float area;

 public:
 Shape(string nm):name(nm){}

 //Getters
 string     getName(){    return name;    }
 float    getArea(){}

 //Setters
 virtual void    setArea(){}

 //Print
 virtual void printInfo()
 {
    cout << "Name: " << name << " Color: " << endl;
}

};

class Rectangle :  public Shape{
private:
float length, width;

public:
Rectangle(string nm, float l, float w):Shape::Shape(nm), length(l), width(w){}
Shape::getName();

//Setters
void setArea(){ area = length*width; }

void printInfo(){
    //Shape::printInfo();
    cout << "Name: " << name << " L: " << length << " W: " << width << " A: " << area << endl;
}
};

class Square : public Rectangle{

private:
float length;

public:
Square(string nm, float l):length(l),Rectangle::Rectangle(nm){}
float     getLength(){return length;}

//Setters
void setArea(){ area = length *length; }

};

class Circle : public Shape{
private:
float radius;
const float pi = 3.0;

public:
Circle(string nm, float r):Shape::Shape(nm), radius(r){}

//Setters
void setArea(){ area = pi*radius*radius; }

void printInfo(){
    //Shape::printInfo();
    cout << "Name: " << name << " R: " << radius << " A: " << area <<      endl;
}
};

 //class Ellipse : public Circle{
 //
 //private:
 //    float length, width, radius1, radius2;
   //
//public:
//    Ellipse(string nm, int clr, float l, float w);
//
//    //Setters
//void setArea(){ area = radius1 * radius2; }
//
//};

class Triangle : public Shape{
private:
float a, base, c, height;

public:
Triangle(string nm, float a, float b, float c, float h):Shape::Shape(nm), a(a), base(b), c(c), height(h){}

//Setters
void setArea(){ area = (base*height)/2; }

void printInfo(){
    //Shape::printInfo();
    cout << "Name: " << name << " Color: " << " A: " << a << " Base: " << base <<  " C: " << c  << " H: " << height << " P: " << " A: " << area << endl;
}
};

//class Isosceles : public Triangle{
//
//private:
//    float base, height;
//
//public:
//    Isosceles(string nm, int clr, float l, float w);
//
//    //Setters
//    void setArea(){ area = (base*height)/2; }
//
//}; 

int main() {

Rectangle r("Rectangle", 10, 20);
Circle c("Circle", 1);
Triangle tt("Triangle", 2, 2, 3, 3);
Square ss("Square", 10);

Shape* s;
Shape* t;
Shape* u;
Shape* v;
s = &r;
t = &c;
u = &tt;
v = &ss;

//Set and print area of Rectangle
s->setArea();
s->printInfo();

//Set and print area of Circle
t->setArea();
t->printInfo();

//Set and print area of Triangle
u->setArea();
u->printInfo();

//Set and print area of Rectangle
v->setArea();
v->printInfo();


return 0;
}

I get an error while setting up the Square class over here:

class Square : public Rectangle{

private:
float length;

public:
Square(string nm, float l):length(l),Rectangle::Rectangle(nm){}

I commented out the Ellipse and Isosceles classes just so I could set up correctly Square and work no them later. This is my first time asking something, so if something is not correct please let me know. Thank you for your help.


Solution

  • In your Square class I believe I found one mistake...

    Try doing the following with your square constructor:

    Square(string nm, float l):length(l),Rectangle::Rectangle(nm, l, l){} 
    

    As opposed to what you had... that will fix the errors you are getting with the Square class.

    The reason for the difference is because when you were passing arguments to the Rectangle constructor from the Square constructor you were leaving some arguments unintialized (in the Rectangle constructor).