Several questions

Hi all.I am a beginner in C++ and have following question regarding polymorphism.I want that getPolygon will act as Rectangle.But within the method getPolygon width and height is correct but outside of that method not.What is not correct in my example below?

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
62
  #include "stdafx.h"

// pointers to base class
#include <iostream>
using namespace std;

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

	void toString() {
		cout << "width:" << width << " height:" << height << endl;
	}

	virtual int area() = 0; // 'pure virtual function' -> makes class 'abstract base class'
};

class Rectangle : public Polygon {
public:
	Rectangle(int width, int height) {
		this->set_values(width, height);
	}

	int area()
	{
		//cout << "this is Rectangle area:";
		return width*height;
	}
};

class Triangle : public Polygon {
public:
	int area()
	{
		//cout << "this is Triangle area:";
		return width*height / 2;
	}
};

Polygon * getPolygon() {
	Polygon * result = & Rectangle(2, 3);
	result->toString();
	return result;
}

int main() {

	Triangle triangle;
	Polygon * ppoly = &triangle;
	ppoly->set_values(4, 5);
	cout << triangle.area() << '\n';

	Polygon * polygon = getPolygon();
	polygon->toString();

	return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Polygon * getPolygon() {
	Polygon * result = & Rectangle(2, 3); // rectangle is destructed here
	result->toString(); // accessing nonexisting object
	return result;
}

// is almost the same as
Polygon * getPolygon() {
	Rectangle fubar(2, 3)
	Polygon * result = & fubar;
	result->toString();
	return result;
	// fubar is destructed here
}

In both versions above the getPolygon returns a memory address. That address has no valid object after the end of the function call. In your version it did not have one already when you did call the toString.

In Polygon * result = & Rectangle(2, 3); you do create an unnamed temporary Rectangle object, whose lifetime does not extend beyond this one statement.

You should use dynamically allocated memory, if you want the object to outlive a function. Then you have to manage the memory too (read: deallocate appropriately). You should look up std::unique_ptr and std::shared_ptr, for they help in the management.



Another note. Constructors are convenient:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Polygon {
public:
	Rectangle() = default;
	Rectangle( int width, int height )
	 : width(width), height(height)
	{
	}

	void set_values(int a, int b);

	// code
};

class Rectangle : public Polygon {
public:
	Rectangle(int width, int height)
	 : Polygon(width, height)
	{
	}
Last edited on
Thank you for explanation. Could you please give me a short example for dynamically allocated memory? (If possible based on my program above)

Thank you again.
You are returning a pointer to a local variable that doesn't exists outside the called function. You need to use new (and delete later):
1
2
3
4
5
6
7
8
9
10
11
12
Polygon * getPolygon() {
	Polygon * result = new Rectangle(2, 3); // Note
	result->toString();
	return result;
}

...

	Polygon * polygon = getPolygon();
	polygon->toString();
	delete polygon; // Note
...


unique_ptr would be a better altenative here:

http://www.cplusplus.com/reference/memory/unique_ptr/?kw=unique_ptr
Topic archived. No new replies allowed.