1

I'm currently working on an object oriented project for C++ programming. I have created a few objects:

int main()
{
    Address address1 = Address("7732","cambie street","vancouver","BC","v5p3r3");
    Author author1 = Author("Joe","Howe","joehow@hotmail.ca");
    Publisher publisher1("candy",address1);
    Book book1 = Book("sprite",author1,publisher1,"1995");
 return 0;
}

and here are my 4 object constructors

Address::Address(string number, string name, string c, string crty, string pCode){
    streetNumber = number;
    streetName = name;
    city = c;
    country = crty;
    postalCode = pCode;
}
Book::Book(string newTitle, Author author1,Publisher pub, string y){
    ISBN++;
    bookISBN = ISBN;
    title = newTitle;
    a = author1;
    authorCount = 1;
    d = pub;
    year = y;
}
Author::Author(string fN, string lN, string e){

    firstName = fN;
    lastName = lN;
    email = e;
}
Publisher::Publisher(string n, Address x){
    name = n;
    a = x;
}

when i call these functions in the main

cout << book1.getPublisher().getAddress().getCity() << endl;
book1.getPublisher().getAddress().set("washington",Address::CITY);
cout << book1.getPublisher().getAddress().getCity() << endl;

not sure if these are allowed in c++ i've done them in java,

i've also tried separating them and it would give me the desired results,

so my question becomes is this format allowed in c++ and if so what am i doing wrong.

my result displays

vancouver
vancouver

but my desired result would be

vancouver
washington

these are my get/set functions

Address Publisher::getAddress(){
    return a;
}


Publisher Book::getPublisher(){
    return d;
}
void Address::set(string value , int number){
switch(number)
{
    case STREET_NUMBER:
        streetNumber = value;break;
    case STREET_NAME:
        streetName = value;break;
    case CITY:
        city = value;break;
    case COUNTRY:
        country = value;break;
    case POSTAL_CODE:
        postalCode = value;break;


  }
}

Thanks for the help

j doe
  • 83
  • 1
  • 7
  • 1
    Please get a book on C++. Your problem is that you are using things learned from Java in C++. Since these two languages have a radically different object model, your code fails. Nikita explained some of it (copying returnvalues), but there's more to it, hence the suggestion to read a good book. – Ulrich Eckhardt Oct 05 '16 at 10:28
  • Agree with @UlrichEckhardt; You should read about pointers and references and understand what they are and how to use them... as this is much closer to what other languages work with. – UKMonkey Oct 05 '16 at 10:40

3 Answers3

3

getAddress() returns a copy of Address object, so you change copy, not Address field in Publisher.

To fix the problem getAddress() should return reference:

Address& Publisher::getAddress()
{
    return a;
}

Probably you need to redesign your object hierarchy, because returning references (pointers) to internals is bad practice.

Community
  • 1
  • 1
Nikita
  • 6,270
  • 2
  • 24
  • 37
1

You'll have to create the method

Book::setAddress(Address ads)

or either use pointer/reference to modify it's value.
Otherwise you'll just get a copy of it

Treycos
  • 7,373
  • 3
  • 24
  • 47
0

In the Java everything is reference. In C++ is not.

There are so many copies of your objects, that you can't easily do what you want without using pointers.

KIIV
  • 3,534
  • 2
  • 18
  • 23