1
#include <iostream>
using namespace std;

class Shape {
        protected:
        int _w, _h;
        public:
        Shape(int w, int h) : _w(w), _h(h) { }
//declaration of area and volume function
};

class Rectangle : public Shape {
        public:
        Rectangle(int w, int h) : Shape(w, h) { }
};

class Cube : public Shape {
        public:
        int _b;
        public:
        Cube(int w, int h, int b) : Shape(w, h), _b(b) { }
        int area() { return 2 * (_w * _h + _w * _b + _b * _h); }
        int volume() { return _w * _h * _b; }
};

int main() {
    Shape *pt;
    int w, h, b, v;
    cin >> w >> h >> b;
    pt = new Rectangle(w, h);
    cout << pt->area() << " ";
    if ((v = pt->volume()) == -1)
        cout << "Undefined ";
    else
        cout << v << " ";
    pt = new Cube(w, h, b);
    cout << pt->area() << " ";
    if ((v = pt->volume()) == -1)
        cout << "Undefined ";
    else
        cout << v << " ";
}

for the input 4 5 8 the output will be 20 Undefined 184 160 and in another test case the input is 10 20 10 and the output is 200 Undefined 1000 2000 how to declare and define area() and volume() to satisfy the given test cases.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Post complete code that illustrates the problem here, not via a link. –  Sep 13 '19 at 15:31
  • Please provide [mcve]. – Algirdas Preidžius Sep 13 '19 at 15:32
  • You have memory leak (whereas you doesn't need `new`). – Jarod42 Sep 13 '19 at 15:33
  • complete code posted – Barbara D. Fultz Sep 13 '19 at 15:36
  • 2
    Don't prompt people to post "complete code" Neil. This is what happens. We expect a [mcve]. – Lightness Races in Orbit Sep 13 '19 at 15:39
  • @XBlueCode: Your edit did not improve the formatting. It just added some redundant indentation to the whole lot. – Lightness Races in Orbit Sep 13 '19 at 17:00
  • @BarbaraD.Fultz. Your code should now be working, just one small tip, if you are planning on coding a lot in the future, using proper whitespace in your code will help you more easily read it. You will notice that the code I have written with you has some 'tabs' inserted in the functions for clarity. You can take a look at [this](https://www.learncpp.com/cpp-tutorial/whitespace-and-basic-formatting/) tutorial if you're interested. Best – Enthus3d Sep 13 '19 at 17:47
  • You'll need to have different paths for 2D shapes and 3D shapes. For example, a cube doesn't have an area, it has a volume. There are 6 sides to a cube and each side has an area. A square has an area. A cube can be made from 6 squares (faces, sides). – Thomas Matthews Sep 13 '19 at 18:19
  • If you need to expand your code to handle triangles, you're in for some more work. A triangle has an area. A triangle in 3 dimensions can be a tetrazoid (all 4 faces are the same), or a pyramid (square base), or a cone (a triangle rotated around one of its sides, circle for a base). The 3D shapes have volumes and surface areas, depending on which surface. – Thomas Matthews Sep 13 '19 at 18:22

1 Answers1

2

Welcome to SO.

I believe what you are looking for is how to declare the two functions for your two inherited classes, Rectangle and Cube.

The general topic that you can look into is called 'polymorphism', where the parent class can take many forms through its derived classes.

Here is an example of what you might be inclined to do, but won't work as intended:

class Shape {
   protected:
      int width, height;

   public:
      Shape( int a = 0, int b = 0){
         width = a;
         height = b;
      }
      int area() {
         cout << "Parent class area :" <<endl;
         return 0;
      }
      int volume() {
         cout << "Parent class volume:" <<endl;
         return 0;
      }
};

What will happen if you run the code above is the parent's area and volume functions may run in ill-defined cases instead. This is because the program will try to do static linking on the functions, basically gluing them in place and preventing us from changing them.

We want to be able to change the area and volume functions to match our derived class, so what you will need to do instead, is to define your area and volume functions as 'virtual' in your parent class, like so:

class Shape {
   protected:
      int width, height;

   public:
      Shape( int a = 0, int b = 0){
         width = a;
         height = b;
      }
      virtual int area() {
         cout << "Parent class area :" <<endl;
         return 0;
      }
      virtual int volume() {
         cout << "Parent class volume:" <<endl;
         return 0;
      }
};

The virtual functions will force the derived classes, such as rectangle or cube, to provide their own functions for those base functions, by telling the program we want to let the derived classes provide the functions instead.

Please have a look at the Stack Overflow post here for more details if you have questions in the future. They have many answers that elaborate on the subject, if there is anything I have missed here.

Hope these help you understand how to handle polymorphism better.

Enthus3d
  • 1,727
  • 12
  • 26
  • Please don't down vote me for attempting to guide them through their problem. I am trying to help them understand polymorphism in C++, and I am new to Stack Overflow as well, so I am also learning how to best help others. Thank you. – Enthus3d Sep 13 '19 at 15:34
  • 1
    This seems like it's intended to be a comment, instead of an answer. Yes, I realize, that you can't write comments yet, hence, relevant reading: [Why do I need 50 reputation to comment? What can I do instead?](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead) – Algirdas Preidžius Sep 13 '19 at 15:34
  • This is an answer. The link itself has a way for her to solve her problem. I do not want to simply provide her the code in this circumstance, because it does not help her understand the reason behind solving her problem in a particular way. – Enthus3d Sep 13 '19 at 15:37
  • 2
    @Enthus3d "*Please don't down vote me for attempting to guide them through their problem*" except that this is not what "answers" field is for. Answers are for answers not for guiding. You may want to see [answer] to get better understanding of what is proper answer. And no, link is not an answer either. A good answer should be self contained and it should be complete even if any external link expires. – Yksisarvinen Sep 13 '19 at 15:37
  • 2
    A link to some off-site tutorial is not an answer. – Blastfurnace Sep 13 '19 at 15:40
  • This is not an answer; it's a welcome and some pointers to general learning material. You can invite the OP to a chatroom if you wish to perform mentoring. Thanks! – Lightness Races in Orbit Sep 13 '19 at 15:40
  • Ok, all of you make a good point. I can see how my answer is inadequate. I will provide the answer, but supplement it with the two links to help her understand why the answer is a good way to solve her problem. – Enthus3d Sep 13 '19 at 15:40
  • 1
    Great, sounds promising! – Lightness Races in Orbit Sep 13 '19 at 15:40
  • 1
    @Enthus3d can you please elaborate it... it the link that you suggested the example was not clear and the calling types were also different can you please suggest how can I call define those functions.. by the way thanks for sharing the link – Barbara D. Fultz Sep 13 '19 at 15:44
  • 1
    @BarbaraD.Fultz I have made an edit elaborating the answer a bit more. Essentially, the article talks about how we need to use virtual functions in a 'base' class, like Shape. This is because Shape is an 'abstract' class, which we don't intend to actually **use**, but is a class that helps us get an **idea** of the functions we need to define. the Virtual functions force the derived class, that is, classes that are derived from shape, to define those functions for themselves. I Hope it helps! – Enthus3d Sep 13 '19 at 15:49
  • @Enthus3d thanks a lot for explaining it briefly and declaring the functions as virtual is definitely the optimal solution for this problem but according to the test cases I'm getting `0 0 184 160` as output instead of ` 20 Undefined 184 160` for the input `4 5 8` can you explain why its like this??? – Barbara D. Fultz Sep 13 '19 at 16:12
  • @Barbara give me a moment, let me have a look. – Enthus3d Sep 13 '19 at 16:36
  • 1
    Ah, I think you forgot to define the area and volume for rectangle, so it’s trying to use the shape’s area and volume functions instead. – Enthus3d Sep 13 '19 at 16:41
  • 1
    I didn't read this in sufficient detail to say whether it answers the question as posed, but regardless it is _much better_ and now looks like an answer. Thank you! – Lightness Races in Orbit Sep 13 '19 at 16:59
  • 1
    You may also need to set the shape functions area and volume as virtual, because otherwise square and rectangle’s functions may not be called if you call the object as a general shape. – Enthus3d Sep 13 '19 at 16:59
  • @LightnessRacesinOrbit thanks for the input from you and everyone else. I was somewhat misguided in my method of answering questions, before, but I'll try to make sure my answers in the future are all of a higher standard. – Enthus3d Sep 13 '19 at 17:22
  • i set them as virtual.. https://codeshare.io/GAoeBp this is my code – Barbara D. Fultz Sep 13 '19 at 17:22
  • 1
    @Enthus3d Good luck! – Lightness Races in Orbit Sep 13 '19 at 17:31
  • Hi @BarbaraD.Fultz, I have edited your code a bit at the link. https://codeshare.io/GAoeBp . The problem was that you did not define an area and volume function for rectangle; notice the functions area and volume that I added for you. Area for rectangle is just w * h, while the volume is undefined, so I have the function return a -1. I've tested the code and obtained the right answers – Enthus3d Sep 13 '19 at 17:40