inheritance Classes [closed]

萝らか妹 提交于 2019-12-25 18:33:45

问题


Part A

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


回答1:


This is not an answer - so I apologies.

I cannot this of doing this in a comment - so forgive me

#include <iostream>

using namespace std; // This is a bad idea

// Base class Shape
class Shape // THIS IS THE BASE CLASS - It has height as a member 
{
public:

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

    int width;
    int height;
};

// Primitive Shape

class Rectangle: public Shape // This is derived class, it inherits height
{
public:

    int width = 2;
    int height = 1; // And here it is!

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

// Derived class

class BIGRectangle: public Rectangle
{
public:

    int area;
    Rectangle.ResizeW(8);
    Rectangle.ResizeH(4);
    area = Rectangle.getArea(); // This is not valid C++ code
};

int main(void)
{
    return 0;
}


来源:https://stackoverflow.com/questions/37599215/inheritance-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!