-1

What would I use to define these Variables outside the Public and Private? Enum?

The Instructions for this section are this:

A default constructor. Remember that the default constructor for Address has the following initial values: Address to "99999 Sunset Boulevard" , "Beverly Hills", "CA", "99999"

4 private string instance variables for :Street, City, State, Zip A constructor with 4 parameters: one for street, one for city, one for state, one for zip.

A printAddress(): it prints the Street, City, State, and Zip

Here is what I got so far

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;
};

Also when he says "print" I assume he means display right?

Thank you

Jtech
  • 11
  • 5

1 Answers1

1

I would do it like this:

#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;
}

EDIT:

Explaining some changes.

  1. Look at the private section of the class declaration. You declared members as int although they should be strings. I corrected that.

  2. In the constructors you should use initialization lists to initialize members.

  3. In your constructor parameter list you used the words City, State, Street and Zip as if they were types. Which they are not. The type of these parameters is std::string. Or in my case I chose const std::string& because I prefer passing by reference over passing by value if both are viable.

  4. If methods do not change the object you may declare them const so they can be called also on const instances of the class or const references. I did so in this example when declaring the getters.

  5. I did not see a need to return anything to the caller after printing so I changed the return type to void.

  6. I added a main function to test the class.

There is probably a lot more to say about the differences, but I think this is enough for tonight.

Cheers!

David
  • 1,204
  • 12
  • 16
  • A good solution. I recommend highlighting what changes you made and explaining why you made them. Otherwise you have a code-only answer, and they have an annoying tendency to produce [Cargo Cult Programmers](https://en.wikipedia.org/wiki/Cargo_cult_programming). – user4581301 Oct 09 '18 at 20:56
  • Please do, this is 1 section of 4 for this project. I am a fresh CS major and my adviser recommended taking this Data Structures class while I am in Programming 1 and I feel like I am WAY over my head – Jtech Oct 09 '18 at 21:00
  • @Jtech That's a rough road, but you should be able to manage it. That said, I had to learn Pascal while doing Algorithms and Pascal is a much more forgiving language than C++. I recommend delaying the Algorithms and Data structures work a week or so while you give yourself a crash course in the basics of C++. Definitely get yourself a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) because you're going to have to start running hard. Are you already familiar with any other programming languages? – user4581301 Oct 09 '18 at 21:10
  • Pay special attention to how David is using `std::string`. It is your best friend right now. Your next best friends will be `std::vector` and whatever debugging software came with your development environment. Nothing beats a debugger for ferreting out what code is really doing. – user4581301 Oct 09 '18 at 21:13
  • I took Intro into Programming which was Perl but that's it. I am also learning C# right now as well so I have 3 programming classes and I am struggling but really trying my best – Jtech Oct 09 '18 at 21:14
  • If you're not already using it, consider getting a free edition of Visual Studio. It comes with the one of the best and easiest to use debuggers out there. I'm pushing the debugger pretty hard, but that's because it's one of the biggest time-savers in software engineering. – user4581301 Oct 09 '18 at 21:17
  • The other biggie is write little bits of code at a time. Write a few lines. Compile and test. do not proceed until everything works correctly, and then only write a few more lines of code. If you've been testing as you go, any bugs are most likely in the stuff you just added, reducing the surface area you have to search. Plus if you get something wrong, you find out early and don't have to fix the dozens of times you may have repeated it. – user4581301 Oct 09 '18 at 21:21
  • Groovy. Here's decent write-up on how to use the debugger. It's pretty much the same for C# and C++: https://learn.microsoft.com/en-us/visualstudio/debugger/debugger-feature-tour?view=vs-2017 – user4581301 Oct 09 '18 at 21:23
  • @user4581301 is giving nice advice. I think practicing coding is very important to understand it. I learn it best with practicing not just reading. Then I stumble over all the details. Start with the very small examples. Jtech keep it up, you can do it! – David Oct 09 '18 at 21:25
  • One last bit: Don't be afraid to throw out code. Sometime you'll write something that'll take longer to debug into shape than scrapping it and starting over. But keep a backup in case you're wrong. – user4581301 Oct 09 '18 at 21:27
  • Forgot the most important bit: Never write code without a plan. If you know what you're going to write, why, and what the results should look like when it executes, you're writing code to soon. Before long you'll have to write a linked list, and this is a mother ing killer unless you plan everything out ahead of time. Draw pictures to help you visualize what needs to happen and when. You'll log in in a month or so and every second question is going to be about linked lists. – user4581301 Oct 09 '18 at 21:29
  • Here is another question. In the 3rd section I am suppose to use the printAddress function and display it in another file. I included the file using #include but I don't quite know the command to call it. void Employee::printEmployee() const { std::cout << printName << printAddress << SSN < – Jtech Oct 09 '18 at 21:43
  • fine I did. At least I am learning by doing this and have great code to reference throughout this class – Jtech Oct 09 '18 at 22:22