鱼C论坛

 找回密码
 立即注册
查看: 3310|回复: 1

虚函数的问题,为什么编译运行都没错,就是运行不了

[复制链接]
发表于 2013-1-7 15:39:47 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<iostream.h>
#include<string.h>
class player
{
public:
        player(char *name1,int p,float t,float x1,float x2,float y1,float y2,float z1,float z2,float k);//带形参的构造函数
        friend void get_FG(player&);                        //此处共定义三个友元函数
        friend void get_Px(player&);
        friend void get_FT(player&);
        ~player();                                        //析构函数
        void show();
        void total();
        player (const player& p){FGM=2*p.FGM;cout<<"两分球得分为"<<FGM<<endl;}   //拷贝构造函数
        void display();
       
       

private:
        char *name;
        float time;
    int number;
        float FGM;
        float FGA;
    float PM;
        float PA;
        float FTM;
        float FTA;
        float score;
        float total_score;
        static int count;//定义静态数据成员,统计上场球员数
};




















#include<iostream.h>(虚函数)
class nba
{       
public:
        virtual void print(){cout<<"比赛"<<endl;}
};
class team:public nba
{
public:
        void print(){cout<<"miami"<<endl;}

};










#include"player.h"
player::player(char *name1,int p,float t,float x1,float x2,float y1,float y2,float z1,float z2,float k)
{
        name=new char[strlen(name1)+1];
        strcpy(name,name1);
        number=p;
        time=t;
        FGM=x1;
        FGA=x2;
    PM=y1;
        PA=y2;
        FTM=z1;
        FTA=z2;
        score=k;
        total_score=(x1-y1)*2+y1*3+z1;
        ++count;
}

void player::show()
{
  cout<<number<<"号"<<"\t"<<"球员:"<<"\t"<<name<<endl;
  cout<<"出场时间:   "<<time<<"分钟"<<endl;
  cout<<"总出手次数"<<"\t"<<FGM<<"\t"<<"命中"<<"\t"<<FGA<<endl;
  cout<<"3分出手次数"<<"\t"<<PM<<"\t"<<"命中"<<"\t"<<PA<<endl;
  cout<<"罚球次数"<<"\t"<<FTM<<"\t"<<"命中"<<"\t"<<FTA<<endl;
  cout<<"本场得分"<<"\t"<<total_score<<endl;
  cout<<"梦幻积分"<<"\t"<<score<<endl;
}

void get_FG(player& c)
        {
        if(c.FGA==0)
        cout<<"未获得出手机会"<<endl;
        else
        cout<<"投篮命中率:"<<(c.FGM/c.FGA)*100<<"%"<<endl;
        }


void get_Px(player& a)
{
        if(a.PA==0)
        cout<<"未获得出手机会"<<endl;
        else
    cout<<"三分命中率:"<<(a.PM/a.PA)*100<<"%"<<endl;
}

void get_FT(player& b)
{
        if(b.FTA==0)
        cout<<"未获得罚球机会"<<endl<<endl;
        else
    cout<<"罚球命中率:"<<(b.FTM/b.FTA)*100<<"%"<<endl<<endl;
}



void player::display()
{
    cout<<"热火本场上场球员人数:"<<count<<endl;
}
int player::count=0;



player::~player()
{
    delete []name;
        --count;
}













#include"player.h"
#include"nba.h"
template <typename T>   //定义函数模板
T max(T d1,T d2)

{return (d1>d2) ? d1:d2;}
       
void main()
{

        //nba *pc;(此处不能运行,去掉就可以)
//        nba g;
    //team f;
//        pc->print();
        //pc=&g;
//        pc->print();
//        pc=&f;


      int t1=96,t2=89;
        float s1=101.5,s2=98.7;
       

  player player1("Udonis Haslem",40,20,2,3,0,0,0,0,7);
  player1.show();
  player p1(player1);
  get_FG(player1);
  get_Px(player1);
  get_FT(player1);




  player player2("LeBron James",6,41,8,14,1,5,13,16,29.5);
  player2.show();
  player p2(player2);
  get_FG(player2);
  get_Px(player2);
  get_FT(player2);


  player player3("Chris Bosh",1,38,5,12,0,2,4,4,17);
  player3.show();
  player p3(player3);
  get_FG(player3);
  get_Px(player3);
  get_FT(player3);


  player player4("Dwyane Wade",3,33,7,11,1,1,7,9,23.5);
  player4.show();
  player p4(player4);
  get_FG(player4);
  get_Px(player4);
  get_FT(player4);
  

  player player5("Mario Chalmers",15,25,2,6,1,3,0,0,5.5);
  player5.show();
  player p5(player5);
  get_FG(player5);
  get_Px(player5);
  get_FT(player5);
  

  player player6("Joel Anthony",50,7,0,1,0,0,0,0,-3.5);
  player6.show();
  player p6(player6);
  get_FG(player6);
  get_Px(player6);
  get_FT(player6);
  

  player player7("Mike Miller",13,10,0,1,0,1,0,0,-3.5);
  player7.show();
  player p7(player7);
  get_FG(player7);
  get_Px(player7);
  get_FT(player7);
  

  player player8("Shane Battier",31,26,1,6,1,5,0,0,1);
  player8.show();
  player p8(player8);
  get_FG(player8);
  get_Px(player8);
  get_FT(player8);
  

  player player9("Ray Allen",34,23,2,3,1,2,0,0,5.5);
  player9.show();
  player p9(player9);
  get_FG(player9);
  get_Px(player9);
  get_FT(player9);
  


  player player10("Norris Cole",30,16,3,8,0,1,0,0,-0.5);
  player10.show();
  player p10(player10);
  get_FG(player10);
  get_Px(player10);
  get_FT(player10);
  player10.display();


  cout<<"本场比赛最高分为:"<<"\t"<<max(t1,t2)<<endl;
  cout<<"本场比赛进攻效率最高为:"<<"\t"<<max(s1,s2)<<endl;
}


小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-1-9 12:03:46 | 显示全部楼层
nba *pc = new nba;
把你说的改成这个。关键是 你定义的是指针。继承因为存在着多态性。你必须告诉他指针指向的值是什么类型。否则pc既可以只想nba又可以指向team。
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-8-9 12:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表