Classes and inheritance

Hi,

Im trying to do a program with classes and inheritance.
Im not sure if i have misunderstood the whole thing with inheritance..

Make a class ”Pyramid” (which have a square as base). The class should have the attributes side1, side2 (float).
The class should have the operations area(), volume() and change (float side1, float side2) which will change place with the attributes width and height.

Im not asking you to give me the code i just need it explained..
Am I supposed to give the integers side1 and side2 a predefined value, change the names on the floats and calculate volume and area with the operations? I don’t think thats it?

I would really appreciate som help…

Thank you!
If this is a homework could you post the whole text please.
Normally a Pyramid is not a kind of square so inheritance makes no sense.

However if you have to do I would implement the Square with one value for width and a method for area(). The constructor should take a float and you also should have a setter - the Pyramid needs to take a width and height in its ctor and a setter for both members as well.
The Pyramid class also needs a method volume().

Hope this makes sense.
First I would implement the Square class and write a main function to test it.
Once this is working you can implement the Pyramid class.
Last edited on
Hi,

Yes, this is a homework. This is also the whole text/assignment that i got from my teacher. Haha yeah, I'm really confused .. This is a distance course. And it is really hard to get in touch with the teacher,. I have a deadline next week so i am really stressed.

But i think I understand a little now when you have explained it for me. I will try to code it .. Thank you!
Physically the base of a pyramid can be very well a square when just width is needed.
The second member variable is the height.
You need this for calculating area and volume.

It sounds as if you are supposed to inherite from a square class and use the provided side1/side2 as width/height. And area()/volume() are virtual function that should be overridden for the pyramid.

change the names on the floats
I would think that you don't change the names but the use of them.
A square has no need for side1/side2, because all its sides are equal by definition. If you do let the user give more than one side to a square, then the results will be "predictable".

A square has no volume either.

I would not say that a pyramid IS-A square.
Neither pyramid really HAS-A square.
Closest thing is probably that (part of) pyramid is implemented with a square.

If the square has virtual methods, then it has. However, "make a class Pyramid" implies that you don't touch bases.


Just to verify, the homework is about inheritance?
Hi,

Sorry I missed the last sentence of the assignment, it says:

The volume in a Pyramid with a square as a base calculates by the formula V=(width * width* height) / 3.

I hope you understand my English.

Its this part I don't understand what to do with..

1
2
3
4
5
    void change(float side1, float side2)
    {
    ....
    ....
    }


Im confused about the "side1/side2" also....

Yes! The homework is about inheritance :)


side1 = width;
side2 = height;

That can't be it, its to easy, and weird?
Last edited on
closed account (E0p9LyTq)
A pyramid is a polyhedron that has a base, which can be any polygon, and three or more triangular faces that meet at a point called the apex.

Your homework defines your pyramid as one with a square base, so create a Square class. A square has only one size of side. Your single data member should preferably be a double, for precision.

Your Square class should have two overloaded constructors, default (no parameters) using a default value for the side and the other constructor that passes the value of the side.

The Square class needs a class function (method) that calculates the square's area from the single side value.

You might want to provide methods to change the square's side value, and get the value.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Square
{
private:
   double side;

public:
  Square();
  Square(double new_side);
  
public:
   double Area() const;
   
public:
   double GetSide() const;
   void SetSide(double new_side);
};

The volume of a pyramid is calculated from the area of the base and the height from the base to the apex.

Inheriting a pyramid from square could be as easy as:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Pyramid : public Square
{
private:
   double height;

public:
   Pyramid();
   Pyramid(double new_height);
   Pyramid(double new_height, double new_side);

public:
   double Volume() const;

public:
   double GetHeight() const;
   void SetHeight(double new_height);
};

This should get you started.
Last edited on
Thank you!!!!
That was really helpful, I think i got it now :D
closed account (E0p9LyTq)
The volume in a Pyramid with a square as a base calculates by the formula V=(width * width* height) / 3.


The height of a pyramid is not a side. It is the distance from the base to the apex, the point where the triangular sides come together on top.

You only need to know two things to calculate the volume of a pyramid, the length of the sides of the square base (width) and the height.
Last edited on
closed account (E0p9LyTq)
Thank you!!!!
That was really helpful, I think i got it now :D

Your main function should be as simple as this, with proper class inheritance:
1
2
3
4
5
6
int main()
{
   Pyramid myPyramid(105.5, 87.3);

   std::cout << "My pyramid's volume is: " << myPyramid.Volume() << '\n';
}
Topic archived. No new replies allowed.