鱼C论坛

 找回密码
 立即注册
查看: 1702|回复: 3

[已解决]c++的继承派生问题

[复制链接]
发表于 2021-3-30 10:55:23 | 显示全部楼层 |阅读模式

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

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

x
如何在派生类里的show函数里访问基类的show函数
最佳答案
2021-3-30 15:25:18
  1. #include<iostream>
  2. using namespace std;
  3. class CTime
  4. {
  5.     private:
  6.         int hour,minute,second;
  7.     public:
  8.         CTime();
  9.         CTime(int th,int tm,int ts);
  10.         void Show();
  11. };
  12. CTime::CTime()
  13. {
  14.     hour=0;
  15.     minute=0;
  16.     second=0;
  17.     cout<<"Function #1 is called!"<<endl;
  18. }
  19. CTime::CTime(int th,int tm,int ts)
  20. {
  21.     hour=th;
  22.     minute=tm;
  23.     second=ts;
  24.     cout<<"Function #2 is called!"<<endl;
  25. }
  26. void CTime::Show()
  27. {
  28.     cout<<hour<<":"<<minute<<":"<<second<<endl;
  29.     cout<<"Function #3 is called!"<<endl;
  30. }
  31. class CDate:public CTime
  32. {
  33.     private:
  34.         int year,month,day;
  35.     public:
  36.         CDate();
  37.         CDate(int dy,int dm,int dd);
  38.         CDate(int dy,int dm,int dd,int th,int tm,int ts);
  39.         CDate(int dy,int dm,int dd,CTime t);
  40.         CDate(CTime t);
  41.         void Show();
  42. };
  43. CDate::CDate()
  44. {
  45.     cout<<"Function #4 is called!"<<endl;
  46.     year=2015;
  47.     month=5;
  48.     day=25;
  49. }
  50. CDate::CDate(int dy,int dm,int dd)
  51. {
  52.     cout<<"Function #5 is called!"<<endl;
  53.     year=dy;
  54.     month=dm;
  55.     day=dd;
  56. }
  57. CDate::CDate(int dy,int dm,int dd,int th,int tm,int ts)
  58. {
  59.       cout<<"Function #6 is called!"<<endl;
  60.       year=dy;
  61.       month=dm;
  62.        day=dd;
  63.       CTime(th,tm,ts);
  64. }
  65. CDate::CDate(int dy,int dm,int dd,CTime t):CTime(t)
  66. {
  67.      cout<<"Function #7 is called!"<<endl;
  68.       year=dy;
  69.       month=dm;
  70.        day=dd;

  71. }
  72. CDate::CDate(CTime t):CTime(t)
  73. {
  74.      cout<<"Function #8 is called!"<<endl;
  75.      year=2015;
  76.     month=5;
  77.     day=25;
  78. }
  79. void CDate::Show()
  80. {

  81.     CTime::Show();
  82.     cout<<year<<"-"<<month<<"-"<<day<<" " << endl;
  83.      cout<<"Function #9 is called!"<<endl;
  84. }
  85. int main()
  86. {
  87.     int dy,dm,dd,th,tm,ts,cas=0;
  88.     CTime t1;
  89.     CDate d1;
  90.     cout<<"T1:";    t1.Show();
  91.     cout<<"D1:";    d1.Show();
  92.     while(cin>>dy>>dm>>dd>>th>>tm>>ts)
  93.     {
  94.         cas++;
  95.         cout<<"Case #"<<cas<<":"<<endl;
  96.         CTime t2(th,tm,ts);
  97.         CDate d2(dy,dm,dd);
  98.         CDate d3(dy,dm,dd,th,tm,ts);
  99.         CDate d4(dy,dm,dd,t2);
  100.         CDate d5(t2);
  101.         cout<<"T2:";    t2.Show();
  102.         cout<<"D2:";    d2.Show();
  103.         cout<<"D3:";    d3.Show();
  104.         cout<<"D4:";    d4.Show();
  105.         cout<<"D5:";    d5.Show();
  106.     }
  107.     return 0;
  108. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-3-30 11:19:46 | 显示全部楼层
发代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-30 11:58:55 | 显示全部楼层

#include<iostream>
using namespace std;
class CTime
{
    private:
        int hour,minute,second;
    public:
        CTime();
        CTime(int th,int tm,int ts);
        void Show();
};
CTime::CTime()
{
    hour=0;
    minute=0;
    second=0;
    cout<<"Function #1 is called!"<<endl;
}
CTime::CTime(int th,int tm,int ts)
{
    hour=th;
    minute=tm;
    second=ts;
    cout<<"Function #2 is called!"<<endl;
}
void CTime::Show()
{
    cout<<hour<<":"<<minute<<":"<<second<<endl;
    cout<<"Function #3 is called!"<<endl;
}
class CDate:public CTime
{
    private:
        int year,month,day;
    public:
        CDate();
        CDate(int dy,int dm,int dd);
        CDate(int dy,int dm,int dd,int th,int tm,int ts);
        CDate(int dy,int dm,int dd,CTime t);
        CDate(CTime t);
        void Show();
};
CDate::CDate()
{
    cout<<"Function #4 is called!"<<endl;
    year=2015;
    month=5;
    day=25;
}
CDate::CDate(int dy,int dm,int dd)
{
    cout<<"Function #5 is called!"<<endl;
    year=dy;
    month=dm;
    day=dd;
}
CDate::CDate(int dy,int dm,int dd,int th,int tm,int ts)
{
      cout<<"Function #6 is called!"<<endl;
      year=dy;
      month=dm;
       day=dd;
      CTime(th,tm,ts);
}
CDate::CDate(int dy,int dm,int dd,CTime t):CTime(t)
{
     cout<<"Function #7 is called!"<<endl;
      year=dy;
      month=dm;
       day=dd;

}
CDate::CDate(CTime t):CTime(t)
{
     cout<<"Function #8 is called!"<<endl;
     year=2015;
    month=5;
    day=25;
}
void CDate::Show()
{

    cout<<year<<"-"<<month<<"-"<<day<<" "<<void CTime::Show()<<endl;//就这里!!!!!
     cout<<"Function #9 is called!"<<endl;
}
int main()
{
    int dy,dm,dd,th,tm,ts,cas=0;
    CTime t1;
    CDate d1;
    cout<<"T1:";    t1.Show();
    cout<<"D1:";    d1.Show();
    while(cin>>dy>>dm>>dd>>th>>tm>>ts)
    {
        cas++;
        cout<<"Case #"<<cas<<":"<<endl;
        CTime t2(th,tm,ts);
        CDate d2(dy,dm,dd);
        CDate d3(dy,dm,dd,th,tm,ts);
        CDate d4(dy,dm,dd,t2);
        CDate d5(t2);
        cout<<"T2:";    t2.Show();
        cout<<"D2:";    d2.Show();
        cout<<"D3:";    d3.Show();
        cout<<"D4:";    d4.Show();
        cout<<"D5:";    d5.Show();
    }
    return 0;
}
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-30 15:25:18 | 显示全部楼层    本楼为最佳答案   
  1. #include<iostream>
  2. using namespace std;
  3. class CTime
  4. {
  5.     private:
  6.         int hour,minute,second;
  7.     public:
  8.         CTime();
  9.         CTime(int th,int tm,int ts);
  10.         void Show();
  11. };
  12. CTime::CTime()
  13. {
  14.     hour=0;
  15.     minute=0;
  16.     second=0;
  17.     cout<<"Function #1 is called!"<<endl;
  18. }
  19. CTime::CTime(int th,int tm,int ts)
  20. {
  21.     hour=th;
  22.     minute=tm;
  23.     second=ts;
  24.     cout<<"Function #2 is called!"<<endl;
  25. }
  26. void CTime::Show()
  27. {
  28.     cout<<hour<<":"<<minute<<":"<<second<<endl;
  29.     cout<<"Function #3 is called!"<<endl;
  30. }
  31. class CDate:public CTime
  32. {
  33.     private:
  34.         int year,month,day;
  35.     public:
  36.         CDate();
  37.         CDate(int dy,int dm,int dd);
  38.         CDate(int dy,int dm,int dd,int th,int tm,int ts);
  39.         CDate(int dy,int dm,int dd,CTime t);
  40.         CDate(CTime t);
  41.         void Show();
  42. };
  43. CDate::CDate()
  44. {
  45.     cout<<"Function #4 is called!"<<endl;
  46.     year=2015;
  47.     month=5;
  48.     day=25;
  49. }
  50. CDate::CDate(int dy,int dm,int dd)
  51. {
  52.     cout<<"Function #5 is called!"<<endl;
  53.     year=dy;
  54.     month=dm;
  55.     day=dd;
  56. }
  57. CDate::CDate(int dy,int dm,int dd,int th,int tm,int ts)
  58. {
  59.       cout<<"Function #6 is called!"<<endl;
  60.       year=dy;
  61.       month=dm;
  62.        day=dd;
  63.       CTime(th,tm,ts);
  64. }
  65. CDate::CDate(int dy,int dm,int dd,CTime t):CTime(t)
  66. {
  67.      cout<<"Function #7 is called!"<<endl;
  68.       year=dy;
  69.       month=dm;
  70.        day=dd;

  71. }
  72. CDate::CDate(CTime t):CTime(t)
  73. {
  74.      cout<<"Function #8 is called!"<<endl;
  75.      year=2015;
  76.     month=5;
  77.     day=25;
  78. }
  79. void CDate::Show()
  80. {

  81.     CTime::Show();
  82.     cout<<year<<"-"<<month<<"-"<<day<<" " << endl;
  83.      cout<<"Function #9 is called!"<<endl;
  84. }
  85. int main()
  86. {
  87.     int dy,dm,dd,th,tm,ts,cas=0;
  88.     CTime t1;
  89.     CDate d1;
  90.     cout<<"T1:";    t1.Show();
  91.     cout<<"D1:";    d1.Show();
  92.     while(cin>>dy>>dm>>dd>>th>>tm>>ts)
  93.     {
  94.         cas++;
  95.         cout<<"Case #"<<cas<<":"<<endl;
  96.         CTime t2(th,tm,ts);
  97.         CDate d2(dy,dm,dd);
  98.         CDate d3(dy,dm,dd,th,tm,ts);
  99.         CDate d4(dy,dm,dd,t2);
  100.         CDate d5(t2);
  101.         cout<<"T2:";    t2.Show();
  102.         cout<<"D2:";    d2.Show();
  103.         cout<<"D3:";    d3.Show();
  104.         cout<<"D4:";    d4.Show();
  105.         cout<<"D5:";    d5.Show();
  106.     }
  107.     return 0;
  108. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-7-1 17:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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