鱼C论坛

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

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

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

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

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

x
如何在派生类里的show函数里访问基类的show函数
最佳答案
2021-3-30 15:25:18
#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()
{

    CTime::Show();
    cout<<year<<"-"<<month<<"-"<<day<<" " << 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;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-3-30 11:19:46 | 显示全部楼层
发代码
想知道小甲鱼最近在做啥?请访问 -> 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;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-30 15:25:18 | 显示全部楼层    本楼为最佳答案   
#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()
{

    CTime::Show();
    cout<<year<<"-"<<month<<"-"<<day<<" " << 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;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-11-14 17:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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