Polymorphism in C++, couldn't find what's wrong

Hello!
I just have a base class and two derived classes to calculate the area of Rectangle and Trianlge:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
  #include <iostream> 
using namespace std;
 
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;
      }
};
class Rectangle: public Shape{
   public:
      Rectangle( int a=0, int b=0)
      {
        Shape(a, b); 
      }
      int area ()
      { 
         cout << "Rectangle class area :" <<endl;
         return (width * height); 
      }
};
class Triangle: public Shape{
   public:
      Triangle( int a=0, int b=0)
      {
        Shape(a, b); 
      }
      int area ()
      { 
         cout << "Triangle class area :" <<endl;
         return (width * height / 2); 
      }
};
// Main function for the program
int main( )
{
   Shape *shape;
   Rectangle rec(10,7);
   Triangle  tri(10,5);
   
   // store the address of Rectangle
   shape = &rec;
   // call rectangle area.
   shape->area();

   // store the address of Triangle
   shape = &tri;
   // call triangle area.
   shape->area();
   
   return 0;
}


The output is:
1
2
Rectangle class area :
Triangle class area :


But I was expected:
1
2
3
// I can't get this result
Rectangle class area :70
Triangle class area : 25


Could you please point me what should I change to see the value of the area? Why I don't see the values of the area?

P.S. I just got this example from http://www.tutorialspoint.com/cplusplus/cpp_polymorphism.htm
Last edited on
1
2
 cout << "Triangle class area :" <<endl;
  return (width * height / 2); 

You aren't printing out the width * height / 2 you are simply returning it (and you aren't storing it into a value so it gets forgotten). If you want to print the entire thing out change it to something like this.
1
2
   cout << "Triangle class area : " << width * height / 2 <<endl;
         return (width * height / 2); 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream> 
using namespace std;

class Shape {
protected:
	int width, height;
public:
	Shape(int a, int b)
	{
		width = a;
		height = b;
	}
	virtual void area() = 0;

};
class Rectangle : public Shape{
public:
	Rectangle(int a, int b) :Shape(a, b){}

	void area()
	{
		cout << "Rectangle class area :" << (width * height) << endl;
	}
};
class Triangle : public Shape{
public:
	Triangle(int a, int b) :Shape(a, b){}

	void area()
	{
		cout << "Triangle class area :" << (width * height / 2) << endl;
	}
};
// Main function for the program
int main()
{

	Rectangle rec(10, 7);
	Triangle  tri(10, 5);

	rec.area();
	tri.area();

	return 0;
}
1
2
cout << "Triangle class area :" <<endl;
  return (width * height / 2); 


You aren't printing out the width * height / 2 you are simply returning it (and you aren't storing it into a value so it gets forgotten). If you want to print the entire thing out change it to something like this.

1
2
   cout << "Triangle class area : " << width * height / 2 <<endl;
         return (width * height / 2); 



Unfortunately, it didn't work.... The solution of motobus works fine, but I'd really like to understand, why it's not working using pointers to the class, and int area()... Even when I try this (proposed solution from James2250):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
  #include <iostream> 
using namespace std;
 
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;
      }
};
class Rectangle: public Shape{
   public:
      Rectangle( int a=0, int b=0)
      {
        Shape(a, b); 
      }
      int area ()
      { 
         cout << "Rectangle class area :" <<<(width * height)<endl;
         return (width * height); 
      }
};
class Triangle: public Shape{
   public:
      Triangle( int a=0, int b=0)
      {
        Shape(a, b); 
      }
      int area ()
      { 
         cout << "Triangle class area :" <<(width * height / 2)<< endl;
         return (width * height / 2); 
      }
};
// Main function for the program
int main( )
{
   Shape *shape;
   Rectangle rec(10,7);
   Triangle  tri(10,5);
   
   // store the address of Rectangle
   shape = &rec;
   // call rectangle area.
   shape->area();

   // store the address of Triangle
   shape = &tri;
   // call triangle area.
   shape->area();
   
   return 0;
}


it still doesn't work.......
"it doesn't work" is awfully vague. You've come to the non-telepath area of this forum, so you'll have to actually tell us what the problem is rather than expecting us to read your mind to find out. It's a burden, I know, but in this modern day and age, you're expected to make the effort to deal with us poor, disabled non-telepaths.

I can see a problem with your code. At lines 23 and 35 - that's not how you pass constructor arguments through to base class constructors. Your derived classes are actually initialising their base classes using the default values a=0 and b=0. I'd recommend you go back to your textbook and look up the correct way to do this.

Thank you, MikeyBoy. I'll look up the correct way.
Sorry about the way I asked this question, I'll try do it better next time. I just used the example from the other tutorial (http://www.tutorialspoint.com/cplusplus/cpp_polymorphism.htm). So I just played with that code and tried to get the result.... I just supposed that they should initialize constructors in a correct way. Seems like, not entirely correct.
Topic archived. No new replies allowed.