Inheritance problem !

What is wrong to use base class object to initialize the member variables and then using the values by accessing them via derived class objects?
I am trying following code
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
#include<iostream>
using namespace std;

class polygon
{

 protected:
 int height, width;

 public :
 void set_values(int a,int b)
 {
   height = a;
   width = b;
 }
};

class rectangle : public polygon
{
 public:
 int area()
 {
   return (height * width);
 }
};

int main()
{

polygon poly;

poly.set_values(10,10);
rectangle rect;

cout << "area of rectangle is\n"<<rect.area()<<endl;

return 0;
}


I get a garbage value in output

area of rectangle is
-240831680


However , both the below methods work

1) instead of set_value functions, initialize member variables in constructor
1
2
3
4
5
6
7
8
9
Class polygon
{

public:
 polygon()
 {
    height=10;
    width=10;
 }



area of rectangle is
100


OR

2) set values using derived class object.

1
2
rectangle rect;
rect.set_values(10,10)




area of rectangle is
100


Why can't I initialize member variables using a object of base class?
Thanks in advance :)
You never initialize the values. You just create rect and call area().
umm..still not clear. Doesn't poly.set_values(10,10) initialize height and width to 10?
Doesn't poly.set_values(10,10) initialize height and width to 10?
Yes it does. The 'poly' object but not the 'rect' object
Because poly != rect ... when you declare rect it has uninitialized new polygon.. you need to set the values of "rect" .

Why can't I initialize member variables using a object of base class?

The concept is that rectangle is not a polygon .. rectangle has a polygon ... although in math what i said won't make any sense. because rectangle is a polygon. Using inheritance you have to keep in mind that derived class has a base class.



Last edited on
poly and rect are different object .that is initializing one object will not have effect on the other object .
I'm interested too. Does this mean that the following would be what he wants?

1
2
3
4
5
6
7
8
9
int main()
{
    rectangle rect;
    rect.set_values(10,10);

    cout << "The area of the rectangle is " << rect.area();

    return 0;
}
Last edited on
Yes that is probably what he tried to do.
your origional code
see comments

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
#include<iostream>
using namespace std;

class polygon
{

 protected:
 int height, width;

 public :
 void set_values(int a,int b)
 {
   height = a;
   width = b;
 }
};

class rectangle : public polygon
{
 public:
 int area()
 {
   return (height * width);
 }
};

int main()
{

polygon poly;

poly.set_values(10,10);

// this is your origional code commented out, rect knows absolutely nothing about poly...
//rectangle rect;

//instead try something like
rectangle* pRect = new polygon( poly );

cout << "area of rectangle is\n"<<pRect.area()<<endl;

return 0;
}


what's the magic three things the compiler creates for you if you don't create them?

Copy constructor, = operator and virual destructor? I forget... help me out here

edit - forgot to change rect to pRect
Last edited on
Why does your polygon class only have "width" and "height"? That doesn't make any sense.
Topic archived. No new replies allowed.