How can I make the object return the length specified

How can I make the int length be returned in the object from initialization in rectangle or square?

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
 #include<iostream>

class Shape
{
public:
Shape(){}
virtual ~Shape(){}
};

class Rectangle : public Shape
{
public:
Rectangle(int length, int width):Shape(){}
~Rectangle(){}
protected:
int length, width;
};

class Square : public Rectangle
{
public:
Square(int length):Rectangle(length,length){}
virtual Square(const Square & other):Rectangle(other.length, other.length){}
~Square(){}
int getLength(){return length;}
protected:
int length;
int width;
};

int main()
{
Square Object(5);
std::cout << "The length is << " <<Object.getLength()<<std::endl;
}
You need to set the value of length in the constructor.
Lines 27 and 28 should not exist. Square ALREADY contains length and width variables, because Square IS a Rectangle.

See how Rectangle contains variables? Square contains all of them as well, because Square IS a Rectangle.

You are focusing on the syntax of inheritance but you've missed the point of it.

Topic archived. No new replies allowed.