# learning polymorphism

Page 1 of 1

## 13 Replies - 907 Views - Last Post: 03 September 2013 - 09:20 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=328246&amp;s=d9c6e6f6da573f1af70cdbb1dc286433&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 xconwing

Reputation: 1
• Posts: 39
• Joined: 22-May 13

# learning polymorphism

Posted 03 September 2013 - 10:11 AM

Hello all,

I came across this program when going over c++ from tutorialspoint.com
http://www.tutorials...olymorphism.htm

how can I get access to the calculated area with the least changes to the program?

```#include <iostream>
using namespace std;

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;
}
};
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 << "Rectangle class: " << 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();
// cout << rec.area() << endl;      <-- tried

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

return 8;
}

```

Thanks

Is This A Good Question/Topic? 0

## Replies To: learning polymorphism

### #2 jjl

• Engineer

Reputation: 1270
• Posts: 4,998
• Joined: 09-June 09

## Re: learning polymorphism

Posted 03 September 2013 - 10:15 AM

Access in what sense? You are calling your area functions, but you are not capturing the return values.

### #3 modi123_1

• Suitor #2

Reputation: 14039
• Posts: 56,179
• Joined: 12-June 08

## Re: learning polymorphism

Posted 03 September 2013 - 10:15 AM

Quote

how can I get access to the calculated area with the least changes to the program?

I see an area function being used in that code. What are you getting at?

### #4 #define

• Duke of Err

Reputation: 1853
• Posts: 6,671
• Joined: 19-February 09

## Re: learning polymorphism

Posted 03 September 2013 - 11:02 AM

Hi, you will need to

1) label as virtual the function you wish to use polymorphically.

2) output/print the value returned from the area functions.

3) add Shape construction to initialization lists of Rectangle and Triangle.

This just creates an anonymous temporary Shape object - of no use.

```        Rectangle(int a = 0, int b = 0) {
Shape(a, b );
}

```

This post has been edited by #define: 03 September 2013 - 11:02 AM

### #5 xconwing

Reputation: 1
• Posts: 39
• Joined: 22-May 13

## Re: learning polymorphism

Posted 03 September 2013 - 12:52 PM

By "access" I mean "cout" the area of the triangle and the rectangle. I tried to do that in line 47, but it didn't work.

This post has been edited by xconwing: 03 September 2013 - 12:55 PM

### #6 #define

• Duke of Err

Reputation: 1853
• Posts: 6,671
• Joined: 19-February 09

## Re: learning polymorphism

Posted 03 September 2013 - 01:24 PM

It looks like it should work, by returning the value 0, which the program is set to do.

### #7 xconwing

Reputation: 1
• Posts: 39
• Joined: 22-May 13

## Re: learning polymorphism

Posted 03 September 2013 - 01:49 PM

#define, on 03 September 2013 - 01:24 PM, said:

It looks like it should work, by returning the value 0, which the program is set to do.

ummm... I don't think the area of anything is 0.
By the way, anyone know why random character just inserted in my program when I tried to post the dang thing. Like the "/>/>" in line 20 and 30. Additionally, the letter b in both places suppose to be lower case not caps. I tried to repost the code again but it does the samething -,-''

This post has been edited by xconwing: 03 September 2013 - 01:50 PM

### #8 modi123_1

• Suitor #2

Reputation: 14039
• Posts: 56,179
• Joined: 12-June 08

## Re: learning polymorphism

Posted 03 September 2013 - 01:52 PM

Quote

By the way, anyone know why random character just inserted in my program when I tried to post the dang thing. Like the "/>/>" in line 20 and 30. Additionally, the letter b in both places suppose to be lower case not caps. I tried to repost the code again but it does the samething -,-''

Your text matches up with a known smiley... add a space or two to break up the pattern.

### #9 #define

• Duke of Err

Reputation: 1853
• Posts: 6,671
• Joined: 19-February 09

## Re: learning polymorphism

Posted 03 September 2013 - 02:24 PM

xconwing, on 03 September 2013 - 09:49 PM, said:

ummm... I don't think the area of anything is 0.

Your intention is to return a calculated value, but the program is not written to do that. In post #4 I itemised the steps necessary for you to output a proper value.

### #10 snoopy11

• Engineering ● Software

Reputation: 1460
• Posts: 4,726
• Joined: 20-March 10

## Re: learning polymorphism

Posted 03 September 2013 - 03:12 PM

#define has gave you very good advice,

Regards

Snoopy.

### #11 xconwing

Reputation: 1
• Posts: 39
• Joined: 22-May 13

## Re: learning polymorphism

Posted 03 September 2013 - 04:57 PM

#define, on 03 September 2013 - 02:24 PM, said:

xconwing, on 03 September 2013 - 09:49 PM, said:

ummm... I don't think the area of anything is 0.

Your intention is to return a calculated value, but the program is not written to do that. In post #4 I itemised the steps necessary for you to output a proper value.

I'm sorry, not sure how implement your advice but here is the intended code.

```#include <iostream>
using namespace std;

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;
}
};
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 << "Rectangle class: " << 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();
// cout << rec.area() << endl;      <-- tried

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

return 8;
}

```

### #12 #define

• Duke of Err

Reputation: 1853
• Posts: 6,671
• Joined: 19-February 09

## Re: learning polymorphism

Posted 03 September 2013 - 05:53 PM

You understand that when a derived class inherits from a base class, the data and function members are inherited (available).

So in the Rectangle class width and height are available (inherited).

If you look at the constructor for Rectangle :

```class Rectangle: public Shape {
public:
Rectangle(int a = 0, int b = 0) {
Shape( a, b );
}
// ...
};

```

The width and height are not set by the parameters a and b.

So you need to set the width and height in Rectangle.

### #13 xconwing

Reputation: 1
• Posts: 39
• Joined: 22-May 13

## Re: learning polymorphism

Posted 03 September 2013 - 08:06 PM

#define, on 03 September 2013 - 05:53 PM, said:

You understand that when a derived class inherits from a base class, the data and function members are inherited (available).

So in the Rectangle class width and height are available (inherited).

If you look at the constructor for Rectangle :

```class Rectangle: public Shape {
public:
Rectangle(int a = 0, int b = 0) {
Shape( a, b );
}
// ...
};

```

The width and height are not set by the parameters a and b.

So you need to set the width and height in Rectangle.

Cool, thanks. The edited code is below.

```#include <iostream>
using namespace std;

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;
}
};
class Rectangle: public Shape {
public:
Rectangle(int a = 0, int b = 0) {
// Shape( a,b );
width = a;
height = 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 );
width = a;
height = b;
}
int area() {
cout << "Rectangle class: " << 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();
cout << rec.area() << endl;      //<-- tried

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

return 8;
}

```

Now, can you think of any reason why the author would put line 20 & 32 there?

### #14 #define

• Duke of Err

Reputation: 1853
• Posts: 6,671
• Joined: 19-February 09

## Re: learning polymorphism

Posted 03 September 2013 - 09:20 PM

xconwing, on 04 September 2013 - 04:06 AM, said:

Now, can you think of any reason why the author would put line 20 & 32 there?

Not sure, perhaps they wished to hint at using the Shape constructor in the initialization list:

```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);
}
};

```

.

This post has been edited by #define: 03 September 2013 - 09:21 PM