-4

I'm trying to use the function that I have inside my base class "SHAPE" with the derived class "RECTANGLE" to create a bigger rectangle in my class "BIGRECTANGLE". I want to do have my sides transformation inside the class and not in the main, What should I do? Thanks!

#include <iostream>

using namespace std;

// Base class Shape
class Shape
{
public:

    void ResizeW(int w)
    {
        width = w;
    }
    void ResizeH(int h)
    {
        height = h;
    }
protected:

    int width;
    int height;
};

// Primitive Shape

class Rectangle: public Shape
{
public:

    int width = 2;
    int height = 1;
    int getArea()
    {
        return (width * height);
    }
};

// Derived class

class BIGRectangle: public Rectangle
{
public:

    int area;
    Rectangle.ResizeW(8);
    Rectangle.ResizeH(4);
    area = Rectangle.getArea();
};

int main(void)
{
    return 0;
}

These are the errors that I have: - 45:14: error: expected unqualified-id before '.' token - 46:14: error: expected unqualified-id before '.' token - 47:5: error: 'area' does not name a type

user4581301
  • 33,082
  • 7
  • 33
  • 54
FL93
  • 29
  • 5
  • 1
    Put that stuff in a constructor or whatever... Do you know what constructors are? You're not using them. – LogicStuff Jun 14 '16 at 17:12
  • @LogicStuff can you help me to figure out? – FL93 Jun 14 '16 at 17:15
  • Here's a link to a [tutorial](http://www.cplusplus.com/doc/tutorial/classes/) on constructors. And another link on [inheritance](https://www.cs.bu.edu/teaching/cpp/inheritance/intro/). Read them; Google is your friend. – NonCreature0714 Jun 15 '16 at 00:18
  • Also, your `main()` function is doing nothing. Either get rid of it, or do something with it relevant to demonstrating your issue. – NonCreature0714 Jun 15 '16 at 00:20

1 Answers1

1

There is a time and a place for everything. In

class BIGRectangle: public Rectangle
{
public:

    int area;
    Rectangle.ResizeW(8);
    Rectangle.ResizeH(4);
    area = Rectangle.getArea();
};

the best place to initialize class members in is Member Initializer List of the constructor. For example:

class BIGRectangle: public Rectangle
{
public:

    int area;

    BIGRectangle():Rectangle(8, 4), area(getArea())
    {
    }
};

This says, build me a BIGRectangle that is made of a Rectangle that is 8 by 4 and stores the Rectangle's computed area.

But that requires Rectangle to have a constructor that requires height and width.

class Rectangle: public Shape
{
public:

    // no need for these because they hide the width and height of Shape
    // int width = 2;
    // int height = 1;
    Rectangle(int width, int height): Shape(width, height)
    {

    }
    int getArea()
    {
        return (width * height);
    }
};

That addition builds a Rectangle that uses Shape's width and height rather than its own. When given two names in the same place, the compiler will pick the one declared inner most and hide the outer-most, and this leads to confusion and accidents. Rectangle sees its width and height and needs help to see those defined in Shape. Shape has no clue that Rectangle even exists because Rectangle was defined after Shape. As a result Shape only sees its width and height.

This leads to some real nasty juju when you call getArea. The current version sets Shape's width and height and uses Rectangle's width and height to compute the area. Not what you want to happen.

And that requires Shape to have a constructor that takes width and height

class Shape
{
public:
    Shape(int inwidth,
          int inheight): width(inwidth), height(inheight)
    {

    }
    void ResizeW(int w)
    {
        width = w;
    }
    void ResizeH(int h)
    {
        height = h;
    }
protected:

    int width;
    int height;
};

Note how the parameter is inwidth, not width. This isn't strictly necessary, but it's bad form to use the same name twice in the same place or close proximity for the same reasons as above: The same name in the same place at the same time is bad.

But this asks the question of what happens when you have Circle which is a Shape. Circles have no width or height, so you may want to rethink Shape.

user4581301
  • 33,082
  • 7
  • 33
  • 54
  • This is Perfect Thanks! – FL93 Jun 14 '16 at 17:48
  • Not quite perfect. The hierarchy falls down as soon as you have non-rectangular shapes. Every layer back to the root in an object hierarchy should know less than the previous because it has to be more abstract. Shape knows just as much as Rectangle, and this prevents Shape from also representing a circle, triangle, or anything else that isn't rectangular. Shape should be really, really, *really* dumb. It shouldn't know anything. – user4581301 Jun 14 '16 at 17:56