C++中继承类的问题

2024-11-15 05:42:59
推荐回答(2个)
回答1:

编译时两个地方有错,功能部分未实现
# include
# include

using namespace std;

class clock
{
private:
int hour;
int minute;
int second;
public:
clock(int hour = 0,int minute = 0,int second = 0);
int return_hour();
void output();
};

class Newclock:public clock
{
private:
char a[10];
public:
Newclock(int hour = 0,int minute = 0,int second = 0,char a = NULL);
void judge_AM_PM();
void output();
};

clock::clock(int hour, int minute, int second)
{
this -> hour = hour;
this -> minute = minute;
this -> second = second;
}

int clock::return_hour()
{
return hour;
}

void clock::output()
{
cout<<"The time is :"< //
// 实现将输入的一个时间(比如:13:12:14)改成(1:12:14 PM),也就是hour
//如果大于12,就减12
//
if (hour > 12)
hour -= 12;
cout<}

Newclock::Newclock(int hour,int minute,int second,char a):clock(hour,minute,second)
//上面已经声明过了!这里必须定义
{}

void Newclock::judge_AM_PM()
{
if (clock::return_hour() > 12) // 加上函数参数列表,也就是()
{
strcpy(a,"AM");
}

else
{
strcpy(a,"PM");
}
}

void Newclock::output()
{
clock::output();
cout<}

void main()
{
Newclock time(16,12,13);

time.judge_AM_PM();
time.output();

}

回答2:

#include
#include "string.h"

using namespace std;
class clock
{
private:
int hour;
int minute;
int second;
public:
clock(int hour = 0,int minute = 0,int second = 0);
int return_hour();
void output();
};

class Newclock:public clock
{
private:
char a[10];
public:
Newclock(int hour = 0,int minute = 0,int second = 0,char a = NULL);
void judge_AM_PM();
void output();
};

clock::clock(int hour, int minute, int second)
{
this -> hour = hour;
this -> minute = minute;
this -> second = second;
}

int clock::return_hour()
{
return hour;
}

void clock::output()
{
cout<<"The time is :"< cout<}

Newclock::Newclock(int hour,int minute,int second,char a):clock(hour,minute,second){};//
//必须要有函数体

void Newclock::judge_AM_PM()
{
if (clock::return_hour() > 12)//函数调用要有()
{
strcpy(a,"AM");
}

else
{
strcpy(a,"PM");
}
}

void Newclock::output()
{
clock::output();
cout<}

int main()
{
Newclock time(8,12,13);

time.judge_AM_PM();
time.output();

}