VC驿站

 找回密码
 加入驿站

QQ登录

只需一步,快速开始

搜索
查看: 580|回复: 3

[已解决]一道静态联编和动态联编的题

[复制链接]
41_avatar_middle
最佳答案
0 
在线会员 发表于 2020-4-21 15:39:09 | 显示全部楼层 |阅读模式
6驿站币
【问题描述】某学校对教师每个月工资的计算规定如下:固定工资+课时补贴;教授的固定工资为5000元,每个课时补贴50;副教授的固定工资为3000,每个课时补贴30元。教师类作为基类已经定义好,你需要编写教师类的派生类教授和副教授,以实现主函数中的输出。
【样例输入】zheng 20 duan 30
【样例输出】
Title: Pro. Name: zheng Salary: 6000
Title: A.P. Name: duan Salary: 3900
Title: A.P. Name: duan Salary: 3900
【评分标准】5个测试用例,每个占20%

//第8次作业,虚函数
#include  <iostream>
#include  <string.h>
using  namespace  std;

class  Teacher
{
public:
        virtual  int  Salary()=0;
        virtual  void  Print(int)=0;
};













int  main()
{
        Teacher  *t;
        int  money=0;
        char  pro_name[128];   
        char  ap_name[128];
        int  pro_lessons,  ap_lessons;
        cin  >>  pro_name  >>  pro_lessons  >>  ap_name  >>  ap_lessons;
         
        Professor  p(pro_name,  pro_lessons);
        t  =  &p;
        money  =  t->Salary();
        t->Print(money);
   
        AssociateProfessor  aprof(ap_name,  ap_lessons);
        //  静态联编
        money  =  aprof.Salary();
        aprof.Print(money);
        //  动态联编
          t  =  &aprof;
        money  =  t->Salary();
        t->Print(money);
   
        t  =  NULL;
        system("pause");
        return  0;
}
请问我写的时候报错说函数调用缺少参数列表;请使用“&Professor::Salary”创建指向成员的指针以及其他,那么到底怎么去实现这个派生类呢?
最佳答案
57_avatar_small
2020-4-21 15:39:10
QQQin 发表于 2020-4-21 16:36
还有一个问题,受时间限制不能发,在这里想请您帮忙看一下

要求输出格式是

这个比较简单。楼主自己试试应该可以写出来吧
  1. int main()
  2. {
  3.         Student stu1(0);                        //Constructor
  4.         cout << "stu1" << endl;                //stu1
  5.         stu1.print();                                //0
  6.         stu1.print(stu1);                        //Copy Function                        0                        Destructor

  7.         cout << "stu2" << endl;                                //stu2
  8.         Student *stu2 = new Student(1);                //Constructor
  9.         stu2->print();                                                //1
  10.         delete stu2;                                                //Destructor


  11.         Student stu3(stu1);                                        //Copy Function
  12.         cout << "stu3" << endl;                                //stu3
  13.         stu3.print();                                                //0
  14.         stu3.print(stu3);                                        //Copy Function                        0                        Destructor

  15.         return 0;                                                //运行到此处再步进调试,则可以看到最后俩个Destructor。依次析构stu3和stu1。此处析构顺序牵扯到栈,自行查阅知识点
  16. }
复制代码

最佳答案

查看完整内容

这个比较简单。楼主自己试试应该可以写出来吧




上一篇:关于运算符重载的问题
下一篇:这道析构函数调用次数的题究竟哪里出了问题?
57_avatar_middle
最佳答案
25 
在线会员 发表于 2020-4-21 15:39:10 | 显示全部楼层    本楼为最佳答案   
bestAnswer
QQQin 发表于 2020-4-21 16:36
还有一个问题,受时间限制不能发,在这里想请您帮忙看一下

要求输出格式是

这个比较简单。楼主自己试试应该可以写出来吧
  1. int main()
  2. {
  3.         Student stu1(0);                        //Constructor
  4.         cout << "stu1" << endl;                //stu1
  5.         stu1.print();                                //0
  6.         stu1.print(stu1);                        //Copy Function                        0                        Destructor

  7.         cout << "stu2" << endl;                                //stu2
  8.         Student *stu2 = new Student(1);                //Constructor
  9.         stu2->print();                                                //1
  10.         delete stu2;                                                //Destructor


  11.         Student stu3(stu1);                                        //Copy Function
  12.         cout << "stu3" << endl;                                //stu3
  13.         stu3.print();                                                //0
  14.         stu3.print(stu3);                                        //Copy Function                        0                        Destructor

  15.         return 0;                                                //运行到此处再步进调试,则可以看到最后俩个Destructor。依次析构stu3和stu1。此处析构顺序牵扯到栈,自行查阅知识点
  16. }
复制代码
57_avatar_middle
最佳答案
25 
在线会员 发表于 2020-4-21 16:31:21 | 显示全部楼层
思路:Professor 和AssociateProfessor继承 Teacher,完成构造和析构函数,然后重写其纯虚函数。
代码如下:
  1. #include  <iostream>
  2. #include  <string.h>
  3. using  namespace  std;

  4. class  Teacher
  5. {
  6. public:
  7.         virtual  int  Salary() = 0;
  8.         virtual  void  Print(int) = 0;
  9. };

  10. class Professor :public Teacher {
  11. public:
  12.         Professor(char *pName, int nlessons)
  13.                 : pName(pName)
  14.                 , nLessons(nlessons)
  15.         {
  16.                 if (pName)
  17.                 {
  18.                         int nLen = strlen(pName);
  19.                         this->pName = new char[nLen+1];
  20.                         strcpy(this->pName, pName);
  21.                 }
  22.         }

  23.         ~Professor() {
  24.                 if (pName)
  25.                 {
  26.                         delete pName;
  27.                 }
  28.         }

  29.         int  Salary() override{
  30.                 int nMoney = 5000 + nLessons * 50;
  31.                 return nMoney;
  32.         }

  33.         void  Print(int money) override {
  34.                 if (pName)
  35.                 {
  36.                         cout << "Title: Pro.Name :" << pName <<  " Salary :" << money << endl;
  37.                 }
  38.         }

  39. private:
  40.         char *pName;
  41.         int nLessons;
  42. };

  43. class AssociateProfessor :public Teacher {
  44. public:
  45.         AssociateProfessor(char *pName, int nlessons)
  46.                 : pName(pName)
  47.                 , nLessons(nlessons)
  48.         {
  49.                 if (pName)
  50.                 {
  51.                         int nLen = strlen(pName);
  52.                         this->pName = new char[nLen + 1];
  53.                         strcpy(this->pName, pName);
  54.                 }
  55.         }

  56.         ~AssociateProfessor() {
  57.                 if (pName)
  58.                 {
  59.                         delete pName;
  60.                 }
  61.         }

  62.         int  Salary() override {
  63.                 int nMoney = 3000 + nLessons * 30;
  64.                 return nMoney;
  65.         }

  66.         void  Print(int money) override {
  67.                 if (pName)
  68.                 {
  69.                         cout << "Title: A.P.Name :" << pName << " Salary :" << money << endl;
  70.                 }
  71.         }

  72. private:
  73.         char *pName;
  74.         int nLessons;
  75. };

  76. int  main()
  77. {
  78.         Teacher  *t;
  79.         int  money = 0;
  80.         char  pro_name[128];
  81.         char  ap_name[128];
  82.         int  pro_lessons, ap_lessons;
  83.         cin >> pro_name >> pro_lessons >> ap_name >> ap_lessons;

  84.         Professor  p(pro_name, pro_lessons);
  85.         t = &p;
  86.         money = t->Salary();
  87.         t->Print(money);

  88.         AssociateProfessor  aprof(ap_name, ap_lessons);
  89.         //  静态联编
  90.         money = aprof.Salary();
  91.         aprof.Print(money);
  92.         //  动态联编
  93.         t = &aprof;
  94.         money = t->Salary();
  95.         t->Print(money);

  96.         t = NULL;
  97.         system("pause");
  98.         return  0;
  99. }
复制代码

评分

参与人数 1驿站币 +2 热心值 +2 收起 理由
77_avatar_small Health + 2 + 2 很给力!

查看全部评分

41_avatar_middle
最佳答案
0 
ico_lz  楼主| 发表于 2020-4-21 16:36:11 | 显示全部楼层
JLDawson 发表于 2020-4-21 16:31
思路:Professor 和AssociateProfessor继承 Teacher,完成构造和析构函数,然后重写其纯虚函数。
代码如下 ...

还有一个问题,受时间限制不能发,在这里想请您帮忙看一下
  1. #include  <iostream>
  2.    
  3. using  namespace  std;
  4. class  Student
  5. {
  6. private:
  7.         int  id;
  8.    
  9. public:
  10.         Student(const  int  a)  {   
  11.                 id  =  a;   
  12.                 cout<<"Constructor"<<endl;
  13.         }
  14.                                                                                                                                  
  15.         Student(const  Student  &  stu)  {
  16.                 id  =  stu.id;   
  17.                 cout<<"Copy  Function"<<endl;
  18.         }   

  19.         ~Student()  {   
  20.                 cout<<"Destructor"<<endl;   
  21.         }   
  22.    
  23.         void  print(Student  stu){                                                                     
  24.                 cout<<stu.id<<endl;
  25.         }
  26.         void  print(){
  27.                 cout<<id<<endl;
  28.         }
  29.         friend  void  print1(Student  stu);   
  30. };

  31. void  print1(Student  stu){     
  32.         cout<<stu.id<<endl;
  33. }
复制代码

要求输出格式是
  1. Constructor
  2. stu1
  3. 0
  4. Copy Function
  5. 0
  6. Destructor
  7. stu2
  8. Constructor
  9. 1
  10. Destructor
  11. Copy Function
  12. stu3
  13. 0
  14. Copy Function
  15. 0
  16. Destructor
  17. Destructor
  18. Destructor
复制代码

编写主函数输出的时候我输出的有四个Destructor,请问是因为什么?怎么去写这个主函数呢?
您需要登录后才可以回帖 登录 | 加入驿站 qq_login

本版积分规则

×【发帖 友情提示】
1、请回复有意义的内容,请勿恶意灌水;
2、纯数字、字母、表情等无意义的内容系统将自动删除;
3、若正常回复后帖子被自动删除,为系统误删的情况,请重新回复其他正常内容或等待管理员审核通过后会自动发布;
4、感谢您对VC驿站一如既往的支持,谢谢合作!

关闭

站长提醒上一条 /2 下一条

QQ|小黑屋|手机版|VC驿站 ( 辽ICP备09019393号-4 )|网站地图wx_jqr

GMT+8, 2023-9-29 10:56

Powered by CcTry.CoM

© 2009-2021 cctry.com

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