1

I am new to OOPs, so I was trying to create a program implementing inheritance.Here I am accepting the values for width and height in the derived class.Is there any way to reflect the same value in the super class ie Rectangle also.?ie if iam inputting 3 and 4 as breadth and height in Input() of RectangleArea then it should display the same value in Display() in Rectangle class.

    #include <iostream>

    using namespace std;

    class Rectangle
    {
    public:
        int width;
        int height;
    public:
        void Display()
        {
            cout<<"Height"<<height<<",Width"<<width<<endl;
        }
    };

    class RectangleArea : public Rectangle
    {
    public:
        void Input()
        {
            cin>>width;
            cout<<" ";
            cin>>height;
        }
        void Display(int area)
        {
            cout<<"Area"<<area;
        }
    };

    int main()
    {
        Rectangle rect;
        RectangleArea rect1;
        rect1.Input();
        rect.Display();
        return 0;
    }
47aravind
  • 13
  • 2
  • 8
  • 2
    I highly recommend picking up an introductory C++ book from the book list: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Mark B Mar 24 '16 at 18:56
  • 1
    Make sure you understand the distinction between classes and objects (instances of a class) in C++. – TerraPass Mar 24 '16 at 18:57
  • With C++, superclasses are called base classes, and subclasses are derived classes. – alain Mar 24 '16 at 19:19
  • Thanks @MarkB, i just went through a book as u suggested and understood the mistake..Thanks guys. – 47aravind Mar 25 '16 at 01:09

4 Answers4

2

You are already changing the variables in the Rectangle class. But you have two instances, one (rect) derived from Rectangle, one (rect1) from RectangleArea, thus rect.Display returns undefined values -- its Input method has not been called.

fuenfundachtzig
  • 7,952
  • 13
  • 62
  • 87
1

There is no need to "reflect" the same value in the super class, as height and width are members of the super class, not the subclass. I think what you want to do is remove the Display function from your subclass, and let the super class handle the printing.

So, you'd want the following code:

#include <iostream>

using namespace std;

class Rectangle
{
public:
    int width;
    int height;
public:
    void Display()
    {
        cout<<"Height"<<height<<",Width"<<width<<endl;
    }
};

class RectangleArea : public Rectangle
{
public:
    void Input()
    {
        cin>>width;
        cout<<" ";
        cin>>height;
    }
};

int main()
{
    RectangleArea rect1;
    rect1.Input();
    rect1.Display();
    return 0;
}
TriskalJM
  • 2,393
  • 1
  • 19
  • 20
1

I don't think you have the right idea in terms of inheritance which is why this question feels a bit odd. When you think of OOP and classes, you're trying to "classify" things. For instance, you could create a class of vehicles. If we think about vehicles, we know that this class of objects should have a velocity - since they carry you from one place to another. A child class could be a different class of objects, but objects that are still vehicles. For instance, Cars are vehicles and they would have a velocity, plus their own unique member variables such as the number of wheels, or miles per gallon, etc. some property that all cars should have.

So, it's hard to think in OOP about the way you've set things up, since a Rectangle Area is not... a Rectangle! Something that would make sense is a class of Closed Shapes, and then one of the members of that class could be Area, since all closed figures have finite area. Then a child class of Closed Figures could be Rectangles. Like Mark B, I'd recommend picking up a good C++ text.

class ClosedFigure {
    private: 
        unsigned int area;
    public: 
        void displayArea() { cout << area << endl;};
};

class Rectangle : public ClosedFigure {
    private: 
        // already gets area from ClosedFigure
        unsigned int length;
        unsigned int width;
    public: 
        void getDimensions() { 
            cin >> length >> width; 
            area = length * width
        }
};

ClosedFigure a;
Rectangle b;
b.getDimensions();
// User inputs say 3 and 4
b.displayArea();
// program outputs 12
guptashark
  • 119
  • 7
0

I don't quite understand where you are asking as you already have, and are altering, the width and height of the rectangle super class. What I would change about your code is to remove the Display method from your base class into your super class, so it will look something like this:

#include <iostream>

using namespace std;

class Rectangle
{
public:
    int width;
    int height;
public:
    void DisplayHeightAndWidth()
    {
        cout<<"Height"<<height<<",Width"<<width<<endl;
    }
    void DisplayArea()
    {
        cout << "Area" << (height*width) << endl;
    }
};

class RectangleArea : public Rectangle
{
public:
    void Input()
    {
        cin>>width;
        cout<<" ";
        cin>>height;
    }
    void Display(int area)
    {
        cout<<"Area"<<area;
    }
};

int main()
{
    Rectangle rect;
    RectangleArea rect1;
    rect1.Input();
    rect.Display();
    return 0;
}

But then I guess it wouldn't make sense to have your class called rectangle area because it will never do anything with the area of a rectangle, it will just input it.

The thing with inheritance is that whenever you inherit from something, you now have access(kind of ownership) to all public and protected entities within that class. So, any RectangleArea object will be able to do anything to height or width that is allowed to be done in Rectangle.

Cmoraski
  • 78
  • 4