班级组成及施工顺序
Class composition and construction order?
我有两个类,Time和Date,Date由一个Time对象组成。我对时间对象有意见。测试程序(不允许我更改)创建一个时间对象,然后创建一个Date对象并将时间对象作为参数传递。当创建第一个时间对象时,它会正确初始化,但当调用Date对象构造函数时,它不会使用原始时间对象的参数。相反,它创建了一个新的Time对象,并使用我的默认参数(0,0,0),它需要包含在我的Time构造函数和Time类定义中。
我想做的是将提供给Time对象的参数传递给Date构造函数的Time参数,这样Time构造函数就不会使用默认的(0,0,0)参数。我尝试使用对时间对象的引用,但无法使其工作。
这是我的两个类头和测试代码。请注意,测试代码是不允许更改的。我想在创建日期对象时将参数添加到该对象中,但不允许更改代码。
测试代码:
#include <iostream>
using std::cout;
using std::endl;
#include "Time.h" // include Time class definition
#include "Date.h" // include Date class definition
const int MAX_TICKS = 20;
int main()
{
Time t(23, 59, 58);// create a time object
Date d(12, 31, 2014, t); // create date object
// output Time object t's values
for ( int ticks = 1; ticks < MAX_TICKS; ++ticks )
{
d.print(); // invokes print
cout << endl;
d.tick(); // invokes function tick
} // end for
d.~Date();// call Date destructor
system("PAUSE");
return 0;
} // end main
日期标题:
#ifndef DATE_H
#define DATE_H
#include "Time.h"
class Date
{
public:
static const unsigned int monthsPerYear = 12; // months in a year
Date( int = 1, int = 1, int = 1900, Time = Time()); // default constructor
~Date(); // provided to confirm destruction order
void print() const; // print date in month/day/year format
void tick(); // function that increments seconds by 1.
void increaseADay(); // increases the day by one
private:
unsigned int month; // 1-12 (January-December)
unsigned int day; // 1-31 based on month
unsigned int year; // any year
Time time; // private Time object - class composition
// utility function to check if day is proper for month and year
unsigned int checkDay( int );
}; // end class Date
#endif
时间头:
#ifndef TIME_H
#define TIME_H
// Time class definition
class Time
{
public:
explicit Time( int = 0, int = 0, int = 0 ); // default constructor
~Time(); // destructor
// set functions
void setTime( int, int, int ); // set hour, minute, second
void setHour( int ); // set hour (after validation)
void setMinute( int ); // set minute (after validation)
void setSecond( int ); // set second (after validation)
// get functions
unsigned int getHour() const; // return hour
unsigned int getMinute() const; // return minute
unsigned int getSecond() const; // return second
void printUniversal() const; // output time in universal-time format
void printStandard() const; // output time in standard-time format
private:
unsigned int hour; // 0 - 23 (24-hour clock format)
unsigned int minute; // 0 - 59
unsigned int second; // 0 - 59
}; // end class Time
#endif
日期类实现文件:
// Date class member-function definitions.
#include <array>
#include <iostream>
#include <stdexcept>
#include "Date.h" // include Date class definition
#include "Time.h" // include the Time class definition
using namespace std;
// constructor confirms proper value for month; calls
// utility function checkDay to confirm proper value for day
Date::Date( int mn, int dy, int yr, Time time)
{
if ( mn > 0 && mn <= monthsPerYear ) // validate the month
month = mn;
else
throw invalid_argument( "month must be 1-12" );
year = yr; // could validate yr
day = checkDay( dy ); // validate the day
// output Date object to show when its constructor is called
cout << "Date object constructor for date ";
print();
cout << endl;
} // end Date constructor
// print Date object in form month/day/year
void Date::print() const
{
cout << month << '/' << day << '/' << year;
cout << "t";
time.printStandard();
cout << "t";
time.printUniversal();
} // end function print
// output Date object to show when its destructor is called
Date::~Date()
{
cout << "Date object destructor for date ";
print();
cout << endl;
} // end ~Date destructor
// function to increment Time by one second
void Date::tick()
{
int tempSecond = time.getSecond();
if (tempSecond < 59)
{
tempSecond++;
time.setSecond(tempSecond);
}
else if (tempSecond >= 59)
{
time.setSecond(0);
int tempMin = time.getMinute();
if (tempMin < 59)
{
tempMin++;
time.setMinute(tempMin);
}
else if (tempMin >= 59)
{
time.setMinute(0);
int tempHour = time.getHour();
if (tempHour < 23)
{
tempHour++;
time.setHour(tempHour);
}
else if (tempHour >= 23)
{
time.setHour(0);
increaseADay();
}
}
}
}
void Date::increaseADay()
{
checkDay(++day);
}
// utility function to confirm proper day value based on
// month and year; handles leap years, too
unsigned int Date::checkDay( int testDay )
{
static const array< int, monthsPerYear + 1 > daysPerMonth =
{ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
// determine whether testDay is valid for specified month
if ( testDay > 0 && testDay <= daysPerMonth[ month ] )
return testDay;
// February 29 check for leap year
if ( month == 2 && testDay == 29 && ( year % 400 == 0 ||
( year % 4 == 0 && year % 100 != 0 ) ) )
return testDay;
if (testDay > daysPerMonth[month])
{
month++;
day = 1;
if (month > monthsPerYear)
{
year++;
month = 1;
day = 1;
return 0;
}
}
throw invalid_argument( "Invalid day for current month and year" );
} // end function checkDay
时间类实现文件
#include <iostream>
#include <iomanip>
#include <stdexcept>
#include "Time.h" // include definition of class Time from Time.h
using namespace std;
// Time constructor initializes each data member
Time::Time( int hour, int minute, int second )
{
cout << "Time object constructor is called ";
setTime( hour, minute, second ); // validate and set time
cout << "t";
printStandard(); // call the print standard function
cout << "t";
printUniversal(); // call the print universal function.
cout << endl;
} // end Time constructor
// Time destructor - calls printStandard and printUniversal
Time::~Time()
{
// print a message indicating that we are destructing
cout << "Time object destructor called.";
// call printStandard and printUniversal functions
printStandard();
printUniversal();
} // end Time destructor
// set new Time value using universal time
void Time::setTime( int h, int m, int s )
{
setHour( h ); // set private field hour
setMinute( m ); // set private field minute
setSecond( s ); // set private field second
} // end function setTime
// set hour value
void Time::setHour( int h )
{
if ( h >= 0 && h < 24 ) // validate the input
hour = h;
else // throw an error if hour is not valid.
throw invalid_argument( "hour must be 0-23" );
} // end function setHour
// set minute value
void Time::setMinute( int m )
{
if ( m >= 0 && m < 60 ) // validate the input
minute = m;
else // throw an error if hour is not valid.
throw invalid_argument( "minute must be 0-59" );
} // end function setMinute
// set second value
void Time::setSecond( int s )
{
if ( s >= 0 && s < 60 )
second = s;
else
throw invalid_argument( "second must be 0-59" );
} // end function setSecond
// return hour value
unsigned int Time::getHour() const
{
return hour;
} // end function getHour
// return minute value
unsigned int Time::getMinute() const
{
return minute;
} // end function getMinute
// return second value
unsigned int Time::getSecond() const
{
return second;
} // end function getSecond
// print Time in universal-time format (HH:MM:SS)
void Time::printUniversal() const
{
// set field widths and fill characters to print format "00:00:00"
// setfill is sticky, setw is not
cout << setfill( '0' ) << setw( 2 ) << getHour() << ":"
<< setw( 2 ) << getMinute() << ":" << setw( 2 ) << getSecond();
} // end function printUniversal
// print Time in standard-time format (HH:MM:SS AM or PM)
void Time::printStandard() const
{
// set field widths and fill characters to print format "00:00:00"
// use the conditional operator to check if time is equal to 0 or 12
// if so, mod the hour by 12 to put it in 12 hour clock format.
cout << ( ( getHour() == 0 || getHour() == 12 ) ? 12 : getHour() % 12 )
<< ":" << setfill( '0' ) << setw( 2 ) << getMinute()
<< ":" << setw( 2 ) << getSecond() << ( hour < 12 ? " AM" : " PM" );
} // end function printStandard
如果运行此程序并查看控制台输出的前两行,它将显示打印设置时间的时间构造函数。第二个打印显示如何再次调用时间构造函数,将数据成员设置为00:00:00。这就是我试图避免的。我不明白为什么第二次调用Time构造函数。。。
我正在查看您的Date
构造函数,有些东西让我印象深刻:
- 它以
Date::Date( int mn, int dy, int yr, Time time) {
开始,不使用:
,这意味着您的Data::time
字段将使用Time
的默认构造函数初始化 - 您没有使用构造函数的
time
参数,特别是没有将其分配给Data::time
我想这解释了你的问题
您必须在Date
构造函数中设置this->time = time;
。这就是你所缺少的。
相关文章:
- CMake-按正确顺序将项目与C运行时对象文件链接
- 函数调用中参数的顺序重要吗
- 为什么不;名字在地图上是按顺序排列的吗
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 数到第n个楼梯的路(顺序无关紧要)
- 优先顺序:智能指针和类析构函数
- 在循环中按顺序遍历成员变量
- 独立读取-修改-写入顺序
- QML按钮点击功能执行顺序
- C++中数据类型修饰符的顺序
- 当比特(而不是字节)的顺序至关重要时的持久性
- C++从其他 constexpr 创建 lambda 不能按顺序执行 Constexpr
- 通过选项卡的文本设置QTabWidget顺序
- c++11评估顺序(未定义的行为)
- 如何在C++中递归地按相反顺序打印集合
- 给定顺序中的事件处理
- 具有包含其他对象的类的对象创建顺序
- 如何通过替换顺序代码的while循环来添加OpenMP for循环
- 遍历顺序由 std::文件系统directory_iterator给出
- 班级组成及施工顺序