Create a Painting class that holds the painting title, artist name, and value. A
ID: 3717152 • Letter: C
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. Save the file as Paintings.cpp.
Note: instead of displaying the 10 paintings to the console as requested by the exercise, output the 10 paintings to a file named Paintings.txt. Each data field output should be separated by a space.
Explanation / Answer
// Copy the following code, compile it and run it
Source code:
#include<iostream>
#include<fstream>
using namespace std;
class Painting
{
public:
string painting_title,artist_name;
float value;
Painting(){}
Painting(string title,string name)
{
painting_title=title;
artist_name=name;
value=400;
}
void display()
{
cout<<painting_title<<" "<<artist_name<<" "<<value<<endl;
}
};
class FamousPainting : public Painting
{
public:
FamousPainting(string title,string name):Painting(title,name)
{
value=25000;
}
};
int main()
{
fstream file("Paintings.txt",ios::out);
Painting arr[10];
for(int i=0;i<10;i++)
{
string title,name;
cout<<"Painting "<<i+1<<" :"<<endl;
cout<<"Painting title : ";
getline(cin,title);
cout<<"Artist name : ";
getline(cin,name);
cout<<endl;
if(name=="Degas" || name=="Monet" || name=="Picasso" || name=="Rembrandt" || name=="degas" || name=="monet" || name=="picasso" || name=="rembrandt" )
{
arr[i]=FamousPainting(title,name);
}
else
{
arr[i]=Painting(title,name);
}
}
for(int i=0;i<10;i++)
{
file<<arr[i].painting_title<<" ";
file<<arr[i].artist_name<<" ";
file<<arr[i].value<<endl;
}
file.close();
return 0;
}
* Please do let me know in the comments section if there is any clarification to be made.