我是从事学校编码项目的新手程序员。
问题域如下:
编写与时钟相关的C++程序
我们在类里面做了很多例子,我们可以使用单独的get \ set函数,但这是我们第一次尝试使用单个get函数和指针来做到这一点。坦率地说,我迷路了
这是我的头文件的内容:
// default class definition
#ifndef CLOCK1_H
#define CLOCK1_H
#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;
class Clock
{
private:
int hours;
int minutes;
int seconds;
public:
int Clock::getInitialTime();
int Clock::setClockTime();
Clock::Clock(); // default constructor
Clock::~Clock(); // default destructor
};
#endif
这是我的源代码文件的内容:
// function declarations \ main program
//
#include "stdafx.h"
#include "Clock1.h"
#include <iostream>
#include <conio.h>
using namespace std;
int Clock::getInitialTime()
{
return hours, minutes, seconds;
}
//void Clock::setClockTime(int hr,min,sec)
//{
// hours=hr;
// minutes=min;
// seconds=sec;
//}
// default constructor
Clock::Clock()
{
hours=0;
minutes=0;
seconds=0;
}
// default destructor
Clock::~Clock()
{
cin.get();
cin.get();
}
int main()
{
Clock defaultObj;
defaultObj.getInitialTime();
cout << "The initial time is " << defaultObj.getInitialTime() << endl;
return 0;
}
我正在尝试以较小的步骤进行操作,第一个目标是能够输出小时,分钟,秒的初始值。完成此操作后,我可以添加带有其他参数的其他构造函数。
我的第一个猜测是我需要添加以下内容:
构造函数:
将适当的参数添加到默认构造函数
Clock::Clock(int *hourPrt, int *minutePtr, int *secondPrt)
创建指针
Clock *hourPtr;
Clock *minutePtr;
Clock *secondPtr;
将它们与对象的属性关联
hourPrt=defaultObj.hours
minutePtr=defaultObj.minutes
secondPrt=defaultObj.seconds
getInitialTime函数
修改方式
主函数调用
修改方式
谁能帮我这个?
谢谢
最佳答案
构造函数不是您需要更改的功能之一。首先编写void Clock::setClockTime(int hours, int minutes, int seconds)
,这是最简单的。
还请注意,在类内部时,不要在成员函数前添加类名。
class Clock
{
public:
Clock::Clock(); // WRONG
Clock(); // RIGHT way to declare constructor
};