Tiancl 发表于 2021-3-30 10:55:23

c++的继承派生问题

如何在派生类里的show函数里访问基类的show函数

人造人 发表于 2021-3-30 11:19:46

发代码

Tiancl 发表于 2021-3-30 11:58:55

人造人 发表于 2021-3-30 11:19
发代码

#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;
}

人造人 发表于 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;
}
页: [1]
查看完整版本: c++的继承派生问题