首页 > 解决方案 > 员工测试驱动数据结构

问题描述

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

本节的说明如下:

默认构造函数。请记住,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;
};

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

谢谢

标签: c++data-structures

解决方案


我会这样做:

#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. 在您的构造函数参数列表中,您使用了单词City,和State,就好像它们是类型一样。他们不是。这些参数的类型是 std::string。或者在我的情况下,我选择是因为如果两者都可行,我更喜欢通过引用传递而不是通过值传递。StreetZipconst std::string&

  4. 如果方法不改变对象,您可以声明它们const,以便它们也可以在类的 const 实例或 const 引用上调用。在此示例中,我在声明 getter 时这样做了。

  5. 打印后我没有看到需要向调用者返回任何内容,因此我将返回类型更改为void.

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

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

干杯!


推荐阅读