Arthur Lee
Arthur Lee

Reputation: 121

C++ pointer or header issues

I have an example program whose main cpp file has

    Rectangle rect(5, 5);
    Triangle tri(5, 5);
    Shape *rectangle1 = ▭
    Shape *triangle1 = &tri;
    rectangle1->setValues(3, 3);
    triangle1->setValues(3, 3);
    int area = rectangle1->getArea();

    std::cout << area << std::endl;
    area = triangle1->getArea();
    std::cout << area << std::endl;

    std::cin.get();

    return 0;
}

The header files of rectangle, triangle and shape are as follows:

Rectangle.h

#ifndef RECTANGLE_H
#define RECTANGLE_H

#include "Shape.h"

class Rectangle: public Shape {
public :
    int length;
    int width;
    Rectangle(int length, int width);
    int getArea();
    //Rectangle::~Rectangle(void);
};
#endif

Triangle.h

#ifndef TRIANGLE_H
#define TRIANGLE_H

#include "Shape.h"

class Triangle: public Shape {
    public :
        int length;
        int width;
        Triangle(int length, int width);
        int getArea();
        //Triangle::~Triangle(void);
};

#endif

Shape.h

#ifndef STDAFX_H
#define STDAFX_H
#ifndef SHAPE_H
#define SHAPE_H
#include <iostream>

class Shape 
{
    protected:
        int width;
        int length;
    public:
        Shape(int length, int width);
        void setValues(int length, int width);
        virtual int getArea() = 0;
};

#endif
#endif

The cpp files of shape, rectangle and triangle are as follows:

Rectangle.cpp

#include "stdafx.h"
#include "Rectangle.h"
Rectangle::Rectangle(int length, int width): length(length), width(width), Shape(length, width)
{
}
    int Rectangle::getArea() {
        return length * width;
    }

Triangle.cpp

#include "stdafx.h"
#include "Triangle.h"
Triangle::Triangle(int length, int width): length(length), width(width), Shape(length, width)
{
}
    int Triangle::getArea() 
    {
        return length/2 * width;
    }

Shape.cpp

#include "stdafx.h"
#include "Shape.h"
#include <iostream>

Shape::Shape(int length, int width): length(length), width(width) {

}

void Shape::setValues(int length, int width) {
    this->length = length;
    this->width = width;
}

My line of thinking is that because triangle and rectangle inherit from Shape and I set rectangle1 to point to the rectangle object and triangle1 to point to the triangle object, once the 2 shapes are created with their default constructor values, I call setValue() (which should be available to all objects that inherit from Shape. This should change each shape's length and width values to 3, 3, and when I run getArea for each shape instance, rectangle should return 3*3 and triangle should return 3*1.5. However, they both return the original areas from before I called setValues (25 and 10).

Is there something I'm not understanding about my pointers or header files?

Thanks!

Upvotes: 1

Views: 150

Answers (3)

Stjepan Bakrac
Stjepan Bakrac

Reputation: 1134

Your Triangle and Rectangle shapes define their own length and width values, which hide the Shape's length and width. But Shape is not aware of that - when you're using setValues it sets the base class's variables. But your getArea will use the child class values.

Upvotes: 1

Matriac
Matriac

Reputation: 382

The problem is that in Shape you have widht and height but also in Rectangle class and Triangle class. This mean that in every instance of Triangle , you have 2 variables Width and height ( Shape::width and Triangle::width) since your function setValues is in shape, it will modify the variables in the shape class and the getArea is define in the rectangle class so it will use the variables in Rectangle)

The solution would be to remove these duplicates variables in Rectangle and Triangle.

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409404

The problem is that you have separate width and length member variables in the child classes. So the getArea functions in the child classes will use those variables instead of the variables from the base class.

Upvotes: 1

Related Questions