C++ virtual问题

yingaijjgz 2014-07-24 03:00:06
#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)
{
string p_star = s;
stringstream ss;
ss << y;
ss >> yards;
string 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)
{
string i_type = "Interception";
string 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];


}




要求显示出这样的效果
Passing Play starring Brett
Commentary: complete for 59 yards!
Running Play starring Paul
Commentary: that will keep the defense honest!
Interception starring Jordan
Commentary: That will change the game!
Interception starring Myles
Commentary: Good for a score!


但实际上我的f->type(),f->star(),和f->commentary()都为零,怎么回事??
老师要求不能更改void describeHighlight(const Highlight* f)和main()函数
...全文
178 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
勤奋的小游侠 2014-07-24
  • 打赏
  • 举报
回复
引用 楼主 yingaijjgz 的回复:
#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) { string p_star = s; stringstream ss; ss << y; ss >> yards; string p_type = "Passing Play";//你给临时变量赋值,没有给类的p_type变量赋值 } 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) { string i_type = "Interception"; string 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]; } 要求显示出这样的效果 Passing Play starring Brett Commentary: complete for 59 yards! Running Play starring Paul Commentary: that will keep the defense honest! Interception starring Jordan Commentary: That will change the game! Interception starring Myles Commentary: Good for a score! 但实际上我的f->type(),f->star(),和f->commentary()都为零,怎么回事?? 老师要求不能更改void describeHighlight(const Highlight* f)和main()函数
你的代码写错了,楼上有正确的回复
pengzhixi 2014-07-24
  • 打赏
  • 举报
回复
#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; 
}
modyaj 2014-07-24
  • 打赏
  • 举报
回复
函数加上断点 一步一步的调试看看呗!看完了就差不多定位了为什么是0了

65,060

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧