65,060
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class Highlight
{
public:
virtual string type() const=0;
virtual string star() const=0;
virtual string commentary() const=0;
};
class PassingPlay: public Highlight
{
private:
string p_type;
string p_star;
string p_commentary;
string yards;
public:
PassingPlay(string s,int y);
virtual string type() const;
virtual string star() const;
virtual string commentary() const;
};
PassingPlay::PassingPlay(string s,int y):p_star(s)
{
stringstream ss;
ss << y;
ss >> yards;
p_type = "Passing Play";
}
string PassingPlay::type() const
{
return p_type;
}
string PassingPlay::star() const
{
return p_star;
}
string PassingPlay::commentary() const
{
return "complete for "+yards+" yards!";
}
class RunningPlay: public Highlight
{
private:
string r_type;
string r_star;
public:
RunningPlay(string s);
virtual string type() const;
virtual string star() const;
virtual string commentary() const;
};
RunningPlay::RunningPlay(string s):r_star(s)
{
r_type = "RunningPlay";
}
string RunningPlay::type() const
{
return r_type;
}
string RunningPlay::star() const
{
return r_star;
}
string RunningPlay::commentary() const
{
return "Commentary: that will keep the defense honest!";
}
class Interception: public Highlight
{
private:
string i_type;
string i_star;
bool flag;
public:
Interception(string s,bool b);
virtual string type() const;
virtual string star() const;
virtual string commentary() const;
};
Interception::Interception(string s,bool b)
{
i_type = "Interception";
i_star = s;
bool flag = b;
}
string Interception::type() const
{
return i_type;
}
string Interception::star() const
{
return i_star;
}
string Interception::commentary() const
{
if(flag)
return "Good for a score!";
else
return "That will change the game!";
}
void describeHighlight(const Highlight* f)
{
cout << f->type() << " starring "<< f->star();
cout << endl << "Commentary: "<<f->commentary();
cout << endl;
}
int main()
{
Highlight* highlights[4];
highlights[0] = new PassingPlay("Brett", 59);
highlights[1] = new RunningPlay("Paul");
highlights[2] = new Interception("Jordan", false);
highlights[3] = new Interception("Myles", true);
for (int k = 0; k < 4; k++)
describeHighlight(highlights[k]);
cout << "Cleaning up" << endl;
for (int n = 0; n < 4; n++)
delete highlights[n];
system("pause");
return 0;
}