员工测试驱动程序数据结构

Employee Test Driver Data Structures

本文关键字:数据结构 测试驱动程序      更新时间:2023-10-16

我将使用什么来定义公共和私有之外的这些变量?恩努姆?

本节的说明如下:

默认构造函数。请记住,Address 的默认构造函数具有以下初始值: 地址到"99999日落大道" , "比佛利山庄", "CA", "99999">

4 个私有字符串实例变量:街道、城市、州、邮编 具有 4 个参数的构造函数:一个用于街道,一个用于城市,一个用于州,一个用于 zip。

打印地址((:它打印街道,城市,州和邮政编码

这是我到目前为止得到的

class Address
{
public:
Address();
Address(City defaultCity, State defaultState, Street defaultStreet, Zip, defaultZip);
int getCity();
int getState();
int getStreet();
int getZip();
int printAddress();
private:
int Street, City, State, Zip;
};
Address :: Address(City defaultCity, State defaultState, Street defaultStreet, Zip, defaultZip);
{
defaultCity = City
defaultState = State
defaultStreet = Street
defaultZip = Zip
}
Address::Address()
{
Street = "99999 Sunset Boulevard,";
City = "Beverly Hills,";
State = "CA,"; 
Zip = "99999";
}
int Address::getCity()
{
return City;
}
int Address::getState()
{
return State;
}
int Address::getStreet()
{
return Street;
}
int Address::getZip()
{
return Zip;
}
int Address::printAddress()
{
return Street, City, State, Zip;
};

另外,当他说"打印"时,我认为他的意思是显示对吗?

谢谢

我会这样做:

#include <iostream>
#include <string>
/********************
*  File: Main.cpp  *
********************/
/*
* For simplicity I put it all in one file.
*/
class Address
{
public:
explicit Address();
explicit Address(const std::string& city, const std::string& state,
const std::string& street, const std::string& zip);
const std::string& getCity() const;
const std::string& getState() const;
const std::string&  getStreet() const;
const std::string&  getZip() const;
void printAddress() const;
private:
std::string street;
std::string city;
std::string state;
std::string zip;
};
// Default Constructor
Address::Address() :
city("Beverly Hills"),
state("CA"),
street("99999 Sunset Boulevard"),
zip("99999")
{ }
Address::Address(const std::string& city, const std::string& state,
const std::string& street, const std::string& zip) :
city(city), state(state), street(street), zip(zip)
{ }
const std::string& Address::getCity() const
{
return city;
}
const std::string& Address::getState() const
{
return state;
}
const std::string& Address::getStreet() const
{
return street;
}
const std::string& Address::getZip() const
{
return zip;
}
void Address::printAddress() const
{
std::cout << street << ", " << city << ", "
<< state << ", " << zip << std::endl;
};
int main(int argc, char* argv[]) {
Address defaultAddress;
defaultAddress.printAddress();
Address customAddress("Cologne", "NRW", "Domplatz 1", "D-50668");
customAddress.printAddress();
return 0;
}

编辑:

解释一些变化。

  1. 查看类声明的私有部分。您将成员声明为int尽管它们应该是字符串。我纠正了这一点。

  2. 在构造函数中,应使用初始化列表来初始化成员。

  3. 在构造函数参数列表中,您使用了单词CityStateStreetZip,就好像它们是类型一样。他们不是。这些参数的类型为 std::string。或者就我而言,我选择了const std::string&因为如果两者都可行,我更喜欢通过引用传递而不是按值传递。

  4. 如果方法不更改对象,则可以const声明它们,以便也可以在类或 const 引用的 const 实例上调用它们。在这个例子中,我在声明getter时就是这样做的。

  5. 我认为打印后不需要向调用方返回任何内容,因此我将返回类型更改为void

  6. 我添加了一个 main 函数来测试类。

关于这些差异可能还有很多话要说,但我认为今晚已经足够了。

干杯!