13 Replies - 385 Views - Last Post: 03 September 2013 - 09:20 PM Rate Topic: -----

#1 xconwing  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9096
  • View blog
  • Posts: 34,155
  • 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?
Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1335
  • View blog
  • Posts: 4,572
  • 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

Was This Post Helpful? 1
  • +
  • -

#5 xconwing  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1335
  • View blog
  • Posts: 4,572
  • 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.
Was This Post Helpful? 0
  • +
  • -

#7 xconwing  Icon User is offline

  • New D.I.C Head

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

Re: learning polymorphism

Posted 03 September 2013 - 01:49 PM

View Post#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

Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9096
  • View blog
  • Posts: 34,155
  • 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.
Was This Post Helpful? 0
  • +
  • -

#9 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1335
  • View blog
  • Posts: 4,572
  • Joined: 19-February 09

Re: learning polymorphism

Posted 03 September 2013 - 02:24 PM

View Postxconwing, 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.

Please post your updated code.
Was This Post Helpful? 0
  • +
  • -

#10 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 770
  • View blog
  • Posts: 2,248
  • Joined: 20-March 10

Re: learning polymorphism

Posted 03 September 2013 - 03:12 PM

#define has gave you very good advice,

if you follow his advice to the letter you will arrive at the correct answer.

Regards

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#11 xconwing  Icon User is offline

  • New D.I.C Head

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

Re: learning polymorphism

Posted 03 September 2013 - 04:57 PM

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

View Postxconwing, 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.

Please post your updated code.


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


Was This Post Helpful? 0
  • +
  • -

#12 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1335
  • View blog
  • Posts: 4,572
  • 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.
Was This Post Helpful? 0
  • +
  • -

#13 xconwing  Icon User is offline

  • New D.I.C Head

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

Re: learning polymorphism

Posted 03 September 2013 - 08:06 PM

View Post#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?
Was This Post Helpful? 0
  • +
  • -

#14 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1335
  • View blog
  • Posts: 4,572
  • Joined: 19-February 09

Re: learning polymorphism

Posted 03 September 2013 - 09:20 PM

View Postxconwing, 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1