Inheritance&Polymorphism

I'm kinda new to Inheritance & Polymorphism concepts. But here's one thing I am confused to solve it.

The question:

Create a Painting class that holds the painting title, artist name, and value. All Paintings are valued at $400 unless they are FamousPaintings. Include a display function that displays all fields. The FamousPainting subclass overrides the Painting value and sets each Painting’s value to $25,000. Write a main() function that declares an array of 10 Painting objects. Prompt the user to enter the title and artist for each of the 10 Paintings. Consider the Painting to be a FamousPainting if the artist is one of the following: Degas, Monet, Picasso, or Rembrandt. Display the 10 Paintings.

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <string>
using namespace std;

///////////////////
// Base Class
///////////////////
class Painting
{
protected:
	string title, artist;
	int value;
public:
	Painting();
	void setTitle(string);
	void setArtist(string);
	void display() const;
};

Painting::Painting()
{
	this->value = 400;
}

void Painting::setTitle(string title)
{
	this->title = title;
}

void Painting::setArtist(string artist)
{
	this->artist = artist;
}

void Painting::display() const
{
	cout << "Title: " << this->title << endl;
	cout << "Artist name: " << this->artist << endl;
	cout << "Value: $" << this->value << endl;
}

/////////////////////
// Derived Class
/////////////////////
class FamousPainting : Painting
{
public:
	FamousPainting();
};

FamousPainting::FamousPainting()
{
	this->value = 25000;
}

//////////////////////
// Driver program
//////////////////////
int main()
{
	string pTitle, pArtist;
	Painting paintings[10];

	for (int i = 0; i <= 9; i++)
	{
		cout << "Enter title of painting: " << endl;
		cin >> pTitle;
		paintings[i].setTitle(pTitle);
		cout << "Enter artist: " << endl;
		cin >> pArtist;
		paintings[i].setArtist(pArtist);
	}

	system("pause");
	return 0;
}


I'm stuck in the main function. How am supposed to turn some of the Painting objects into FamousPainting objects?
In short: you cannot.
However, if you declare array of pointers to Painting, you can assign a pointer to derived object to pointer to base:
1
2
3
4
5
6
7
8
9
10
11
12
13
Painting* painting;

std::string artist;
std::cin >> artist;
if (artist == "Picasso")
    painting = new FamousPainting;
else
    painting = new Painting;
painting.setArtist(artist);

//...

delete painting;


However this is example of misusing inheritance. You should create subclasses if behavior changes, not some default values.
Last edited on
I see. Would you mind explain what 'new' and 'delete' is and how it works? I haven't learn these two before. Thank you.
It is a backbone of any C++ program (however in future you should learn to not use raw new and delete, prefering safe wrappers instead)
http://www.cplusplus.com/doc/tutorial/dynamic/
http://www.learncpp.com/cpp-tutorial/69-dynamic-memory-allocation-with-new-and-delete/
Thank you.
I think line 9 of MiiNiPaa's code should be

painting->setArtist(artist);

Andy
Topic archived. No new replies allowed.