异质链表部分编译不通过,该如何处理
异质链表部分编译不通过
#include<iostream>
using namespace std;
class salary_base
{private:
int work_hour; //加班时间
public:
salary_base(){work_hour=0;}
int get_hour(){return work_hour;}
void set_hour(int hour){work_hour=hour;}
virtual int get_salary()=0;
};
class SeniorEngineer:public salary_base
{public:
int get_salary();
};
int SeniorEngineer::get_salary()
{return (8000+80*get_hour());}
class LowEngineer:public salary_base
{public:
int get_salary();
};
int LowEngineer::get_salary()
{return (5000+50*get_hour());}
class Engineer:public salary_base
{public:
int get_salary();
};
int Engineer::get_salary()
{return (4000+40*get_hour());}
typedef struct salary_node
{salary_base *p;
salary_node *next;
}salary_node;
int main()
{int a,b,c;
cin>>a>>b>>c;
SeniorEngineer obj1;
LowEngineer obj2;
Engineer obj3;
obj1.set_hour(a);
obj2.set_hour(b);
obj3.set_hour(c);
cout<<obj1.get_salary()<<endl;
cout<<obj2.get_salary()<<endl;
cout<<obj3.get_salary()<<endl;
salary_node *head=NULL;
salary_node *pr,*s=NULL;
s->p=new SeniorEngineer();
s->p->set_hour(a);
head=s;
pr=head;
s->p=new LowEngineer();
s->p->set_hour(b);
pr->next=s;
pr=s;
s->p=new Engineer();
s->p->set_hour(c);
pr->next=s;
pr=s;
pr->next=NULL;
int sum=0;
int i=0;
while(head)
{sum+=head->p->get_salary();
head=head->next;
i++;
}
cout<<sum/i<<endl;
}
------解决方案--------------------
链表的各个节点都没有分配空间。
------解决方案--------------------
楼主,你想实现什么?
------解决方案--------------------
#include<iostream>
using namespace std;
class salary_base
{private:
int work_hour; //加班时间
public:
salary_base(){work_hour=0;}
int get_hour(){return work_hour;}
void set_hour(int hour){work_hour=hour;}
virtual int get_salary()=0;
};
class SeniorEngineer:public salary_base
{public:
int get_salary();
};
int SeniorEngineer::get_salary()
{return (8000+80*get_hour());}
class LowEngineer:public salary_base
{public:
int get_salary();
};
int LowEngineer::get_salary()
{return (5000+50*get_hour());}
class Engineer:public salary_base
{public:
int get_salary();
};
int Engineer::get_salary()
{return (4000+40*get_hour());}
typedef struct salary_node
{salary_base *p;
salary_node *next;
}salary_node;
int main()
{int a,b,c;
cin>>a>>b>>c;
SeniorEngineer obj1;
LowEngineer obj2;
Engineer obj3;
obj1.set_hour(a);
obj2.set_hour(b);
obj3.set_hour(c);
cout<<obj1.get_salary()<<endl;
cout<<obj2.get_salary()<<endl;
cout<<obj3.get_salary()<<endl;
salary_node *head=NULL;
salary_node *pr,*s=NULL;
s->p=new SeniorEngineer();
s->p->set_hour(a);
head=s;
pr=head;
s->p=new LowEngineer();
s->p->set_hour(b);
pr->next=s;
pr=s;
s->p=new Engineer();
s->p->set_hour(c);
pr->next=s;
pr=s;
pr->next=NULL;
int sum=0;
int i=0;
while(head)
{sum+=head->p->get_salary();
head=head->next;
i++;
}
cout<<sum/i<<endl;
}
------解决方案--------------------
链表的各个节点都没有分配空间。
------解决方案--------------------
楼主,你想实现什么?
------解决方案--------------------
- C/C++ code
class salary_base { private: int work_hour; //加班时间 public: salary_base(){work_hour=0;} int get_hour(){return work_hour;} void set_hour(int hour){work_hour=hour;} virtual int get_salary()=0; }; class SeniorEngineer:public salary_base { public: int get_salary(); }; int SeniorEngineer::get_salary() { return (8000+80*get_hour()); } class LowEngineer:public salary_base { public: int get_salary(); }; int LowEngineer::get_salary() { return (5000+50*get_hour()); } class Engineer:public salary_base { public: int get_salary(); }; int Engineer::get_salary() { return (4000+40*get_hour()); } typedef struct salary_node { salary_base *p; salary_node *next; }salary_node; int main() { int a,b,c; cin>>a>>b>>c; SeniorEngineer obj1; LowEngineer obj2; Engineer obj3; obj1.set_hour(a); obj2.set_hour(b); obj3.set_hour(c); cout<<obj1.get_salary()<<endl; cout<<obj2.get_salary()<<endl; cout<<obj3.get_salary()<<endl; salary_node *head=NULL; salary_node *pr,*s=NULL; s = new salary_node; s->p=new SeniorEngineer(); s->p->set_hour(a); head=s; pr=head; s = new salary_node; s->p=new LowEngineer(); s->p->set_hour(b); pr->next=s; pr=s; s = new salary_node; s->p=new Engineer(); s->p->set_hour(c); pr->next=s; pr=s; pr->next=NULL; int sum=0; int i=0; while(head) { sum+=head->p->get_salary(); head=head->next; i++; } cout<<sum/i<<endl; return 0; }