#include <iostream>
usingnamespace std;
class Shape {
protected:
int width, height;
public:
Shape( int a=0, int b=0)
{
width = a;
height = b;
}
virtualint 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?
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);
#include <iostream>
usingnamespace std;
class Shape {
protected:
int width, height;
public:
Shape(int a, int b)
{
width = a;
height = b;
}
virtualvoid 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;
}
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):
#include <iostream>
usingnamespace std;
class Shape {
protected:
int width, height;
public:
Shape( int a=0, int b=0)
{
width = a;
height = b;
}
virtualint 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 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.