c++ - 员工测试驱动数据结构
问题描述
我将使用什么来定义公共和私有之外的这些变量?枚举?
本节的说明如下:
默认构造函数。请记住,Address 的默认构造函数具有以下初始值:Address to "99999 Sunset Boulevard"、"Beverly Hills"、"CA"、"99999"
4 个私有字符串实例变量,用于:Street、City、State、Zip 具有 4 个参数的构造函数:一个用于街道,一个用于城市,一个用于州,一个用于 zip。
printAddress():它打印街道、城市、州和邮编
这是我到目前为止得到的
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;
}
编辑:
解释一些变化。
查看类声明的私有部分。您将成员声明为
int
虽然它们应该是字符串。我纠正了这一点。在构造函数中,您应该使用初始化列表来初始化成员。
在您的构造函数参数列表中,您使用了单词
City
,和State
,就好像它们是类型一样。他们不是。这些参数的类型是 std::string。或者在我的情况下,我选择是因为如果两者都可行,我更喜欢通过引用传递而不是通过值传递。Street
Zip
const std::string&
如果方法不改变对象,您可以声明它们
const
,以便它们也可以在类的 const 实例或 const 引用上调用。在此示例中,我在声明 getter 时这样做了。打印后我没有看到需要向调用者返回任何内容,因此我将返回类型更改为
void
.我添加了一个 main 函数来测试这个类。
关于差异可能还有很多话要说,但我认为今晚就足够了。
干杯!
推荐阅读
- django - 如何检查Django Views列表中的对象
- java - 打印 AVL 树的顶部“m”个元素
- google-bigquery - 使用 Google bigquery 将查询结果导出到云存储中的输出文件
- php - 尝试使用 php 访问 json 数组数据
- python - 在 Flask 中提供图像后删除图像
- excel - 求解器的 VBA 循环
- unity3d - 如何使用 Unity3D 中的字符控制器通过按钮 UI 跳转
- c# - 当我尝试使用以管理员 C# 运行的特定位置终止进程时访问被拒绝
- sdk - Yocto 将自己的 Header 和 Lib 文件添加到 sdk
- php - PHP Mail 函数
在我的消息链接中间添加一个