设计一个媒体资源类MediaAsset和它的派生类歌曲类Song、照片类Photo,要求:
(1)MediaAsset类中有构造函数、虚析构函数及纯虚函数void Show();
(2)歌曲类Song继承于MediaAsset类,新增string类型数据成员artist和title,并调用其构造函数实现初始化,覆盖基类Show函数并输出其新增数据成员;
(3)照片类Photo继承于MediaAsset类,新增string类型数据成员date、location和subject,并调用其构造函数实现初始化,覆盖基类Show函数并输出其新增数据成员。
在main函数中,分别通过shared_ptr和new两种方式构建不同派生类对象,然后依次调用所有对象的Show函数实现输出,最后输出Photo类所有对象的Show函数实现输出,比较两种动态分配方式处理对象的差异性。
main函数参考测试代码如下:
int main()
{
string type;
cin >> type;
if (type=="shared_ptr")
{
vector> assets
{
make_shared("Himesh Reshammiya", "Tera Surroor"),
make_shared("Penaz Masani", "Tu Dil De De"),
make_shared("2011-04-06", "Redmond, WA", "Soccer field at a park."),
make_shared("Bob Dylan", "The Times They Are A Changing"),
make_shared("Aretha Franklin", "Bridge Over Troubled Water"),
make_shared("Thala", "Entre El Mar y Una Estrella"),
make_shared("2021-12-06", "Xian, China", "Snowing at bell tower.")
};
vector> photos;
for (const auto& p : assets)
{
p->Show();
shared_ptr temp = dynamic_pointer_cast(p);
if (temp.get()!=nullptr)
{
photos.push_back(p);
}
}
for (const auto& p : photos)
{
p->Show();
}
}
else if (type=="new")
{
vector assets
{
new Song("Himesh Reshammiya", "Tera Surroor"),
new Song("Penaz Masani", "Tu Dil De De"),
new Photo("2011-04-06", "Redmond, WA", "Soccer field at a park."),
new Song("Bob Dylan", "The Times They Are A Changing"),
new Song("Aretha Franklin", "Bridge Over Troubled Water"),
new Song("Thala", "Entre El Mar y Una Estrella"),
new Photo("2021-12-06", "Xian, China", "Snowing at bell tower.")
};
vector photos;
for (const auto& p : assets)
{
p->Show();
Photo *temp = dynamic_cast(p);
if (temp!=nullptr)
{
photos.push_back(p);
}
}
for (const auto& p : photos)
{
p->Show();
}
}
else
cout << "Input error!" << endl;
return 0;
}
新建对象方式选择,或者选择shared_ptr,或者选择new
构造函数输出信息
所有对象Show函数调用输出结果
Photo类对象Show函数调用输出结果
析构函数输出信息
Sample Input 1
shared_ptr
Constructor media asset
Constructor song
Constructor media asset
Constructor song
Constructor media asset
Constructor photo
Constructor media asset
Constructor song
Constructor media asset
Constructor song
Constructor media asset
Constructor song
Constructor media asset
Constructor photo
Himesh Reshammiya, Tera Surroor
Penaz Masani, Tu Dil De De
2011-04-06, Redmond, WA, Soccer field at a park.
Bob Dylan, The Times They Are A Changing
Aretha Franklin, Bridge Over Troubled Water
Thala, Entre El Mar y Una Estrella
2021-12-06, Xian, China, Snowing at bell tower.
2011-04-06, Redmond, WA, Soccer field at a park.
2021-12-06, Xian, China, Snowing at bell tower.
Destructor song
Destructor media asset
Destructor song
Destructor media asset
Destructor photo
Destructor media asset
Destructor song
Destructor media asset
Destructor song
Destructor media asset
Destructor song
Destructor media asset
Destructor photo
Destructor media asset
new
Constructor media asset
Constructor song
Constructor media asset
Constructor song
Constructor media asset
Constructor photo
Constructor media asset
Constructor song
Constructor media asset
Constructor song
Constructor media asset
Constructor song
Constructor media asset
Constructor photo
Himesh Reshammiya, Tera Surroor
Penaz Masani, Tu Dil De De
2011-04-06, Redmond, WA, Soccer field at a park.
Bob Dylan, The Times They Are A Changing
Aretha Franklin, Bridge Over Troubled Water
Thala, Entre El Mar y Una Estrella
2021-12-06, Xian, China, Snowing at bell tower.
2011-04-06, Redmond, WA, Soccer field at a park.
2021-12-06, Xian, China, Snowing at bell tower.
#include
#include
#include
#include
using namespace std;
class MediaAsset
{
public:
MediaAsset()
{
cout << "Constructor media asset" << endl;
}
virtual ~MediaAsset()
{
cout << "Destructor media asset" << endl;
}
virtual void Show() = 0;
};
class Song :public MediaAsset
{
private:
string artist, title;
public:
Song(const string& artist, const string& title) :artist(artist), title(title)
{
cout << "Constructor song" << endl;
}
void Show()
{
cout << artist << ", " << title << endl;
}
~Song()
{
cout << "Destructor song" << endl;
}
};
class Photo :public MediaAsset
{
private:
string date, location,subject;
public:
Photo(const string& date, const string& location,const string& subject) :date(date),location(location),subject(subject)
{
cout << "Constructor photo" << endl;
}
void Show()
{
cout << date << ", " << location << ", " << subject << endl;
}
~Photo()
{
cout << "Destructor photo" << endl;
}
};
int main()
{
string type;
cin >> type;
if (type == "shared_ptr")
{
vector> assets
{
make_shared("Himesh Reshammiya", "Tera Surroor"),
make_shared("Penaz Masani", "Tu Dil De De"),
make_shared("2011-04-06", "Redmond, WA", "Soccer field at a park."),
make_shared("Bob Dylan", "The Times They Are A Changing"),
make_shared("Aretha Franklin", "Bridge Over Troubled Water"),
make_shared("Thala", "Entre El Mar y Una Estrella"),
make_shared("2021-12-06", "Xian, China", "Snowing at bell tower.")
};
vector> photos;
for (const auto& p : assets)
{
p->Show();
shared_ptr temp = dynamic_pointer_cast(p);
if (temp.get() != nullptr)
{
photos.push_back(p);
}
}
for (const auto& p : photos)
{
p->Show();
}
}
else if (type == "new")
{
vector assets
{
new Song("Himesh Reshammiya", "Tera Surroor"),
new Song("Penaz Masani", "Tu Dil De De"),
new Photo("2011-04-06", "Redmond, WA", "Soccer field at a park."),
new Song("Bob Dylan", "The Times They Are A Changing"),
new Song("Aretha Franklin", "Bridge Over Troubled Water"),
new Song("Thala", "Entre El Mar y Una Estrella"),
new Photo("2021-12-06", "Xian, China", "Snowing at bell tower.")
};
vector photos;
for (const auto& p : assets)
{
p->Show();
Photo* temp = dynamic_cast(p);
if (temp != nullptr)
{
photos.push_back(p);
}
}
for (const auto& p : photos)
{
p->Show();
}
}
else
cout << "Input error!" << endl;
return 0;
}