【发布时间】:2015-10-30 09:09:35
【问题描述】:
您好,我有一个名为 Date 的 c++ 类。代码如下。如何为班级创建测试运行?我对 C++ 非常陌生。我进行了很多搜索,但找不到任何有效/有用的网站。请问有人可以帮忙吗?提前致谢。
date.h
#ifndef H_date
#define H_date
#include <iostream>
using namespace std;
class Date{
friend ostream &operator << (ostream & os, const Date &);
friend istream &operator >> (istream & is, Date &);
public:
Date();
Date(int day, int month, int year);
void setday(int day);
void setmonth(int month);
void setyear(int year);
void setDate(int day, int month, int year);
int getday() const;
int getmonth() const;
int getyear() const;
void print() const;
protected:
int day;
int month;
int year;
};
#endif
date.cpp
#include <iostream>
#include "date.h"
using namespace std;
Date::Date()
{
day = 0;
month = 0;
year = 0;
}
Date::Date(int newDay, int newMonth, int newYear)
{
day = newDay;
month = newMonth;
year = newYear;
}
void Date::setday(int newDay)
{
day =newDay;
}
void Date::setmonth(int newMonth)
{
month = newMonth;
}
void Date::setyear(int newYear)
{
year = newYear;
}
void Date::setDate(int newDay, int newMonth, int newYear)
{
day =newDay;
month = newMonth;
year = newYear;
}
int Date::getday()const
{
return day;
}
int Date::getmonth()const
{
return month;
}
int Date::getyear()const
{
return year;
}
void Date::print()const
{
cout << day << ":" << month<< ":" << year <<endl;
}
ostream& operator<< (ostream& osObject, const Date& date1)
{
osObject << date1.day
<< ":" <<date1.month
<< ":" << date1.year;
return osObject;
}
istream& operator>> (istream& isObject, Date& date1)
{
isObject>>date1.day>>date1.month>>date1.year;
return isObject;
}
【问题讨论】:
-
如果您使用特定值调用
setday,getday是否返回正确的值?测试一个类真的很简单:使用 setter 函数来设置一个特定的值(在你的例子中是数据)并检查 getter 函数是否返回正确的值。对于输入/输出函数,使用std::istringstream和std::ostringstream。 -
“创建测试运行”是指“单元测试”?
-
你无法想象我多么想免费为其他人编写测试规范。
-
Yes Nayana.. 单元测试
标签: c++ visual-studio visual-studio-2010 oop