Class Assignment help

Hello,

This is my first time learning about classes and pointers

I have some questions.

I wrote all the functions except one which is the show function because I didn't understand what I should do!!

 
Write a main function that creates an Address object and calls the show member function for that object.


cause my codes do pretty much what show function does I am guessing

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  class Address{
public:
	// ctor
	Address(unsigned number, string street, string suffix, string city, string state, unsigned zip);
	
	// accessors
	unsigned getNumber() const;
	string getStreet() const;
	string getSuffix() const;
	string getCity() const;
	string getState() const;
	unsigned getZip() const;
	
	// mutators
	Address & setNumber(unsigned number);
	Address & setStreet(const string & street);
	Address & setSuffix(const string & suffix);
	Address & setCity(const string & city);
	Address & setState(const string & state);
	Address & setZip(unsigned zip);

	// other: display the information in the invoking objects on 2 lines,
	//     as we would expect to see on an envelope
	const Address & show() const;
private:
	unsigned myNumber;	
	string myStreet;		
	string mySuffix;		
	string myCity;		
	string myState;		
	unsigned myZip;		
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
int main() 
{
	Address a( 16000, "gfgfg", "Street", "fgdggd", "dg", 91344) ;
	cout << a.getNumber() << " " << a.getStreet() << " " << a.getSuffix() <<
 endl << a.getCity() << " " << a.getState() << " " << a.getZip() << endl;
	cout<<endl;

	Address loc[] = {
			{ 6401, "gdgdg", "dgd", "Woodland Hills", "CA", 91371 },
			{ 4792, "sffs", "sfsf", "sfsf", "CA", 91371 
},
			{ 2001, "dgdg", "dgdg", "dgd", "CA", 91371 },
			{ 1234, "dgd", "dgdg", "dg", "CA", 91375 },
			{ 992, "dgd", "dg", "dgdg", "CA", 91375 }
	};
	for (unsigned i = 0; i < 5;i++)
	{
		cout << loc[i].getNumber() << " " << loc[i].getStreet() << " " << loc[i].getSuffix() << endl << loc[i].getCity() << " " << loc[i].getState() << " " << loc[i].getZip() << endl;
		cout << "-------------------" << endl;
	}


}

// member function definition //ctor
Address::Address(unsigned number, string street, string suffix, string city, string state, unsigned zip)
	:myNumber(number), myStreet(street), mySuffix(suffix), myCity(city), myState(state), myZip(zip)                
	if (!myNumber||myNumber<1 || myNumber>199999999)	die("Invalid Number");
	if (myStreet == " ") die("Invalid Street");														                                                                    
	if (mySuffix==" ") die("Invalid Suffix");  
	if (myCity == " ") die("Invalid City");
	if (myState == " ") die("Invalid State");
	if (!zip)			die("Invalid Zip");
}

// Class member function definition // accessors
unsigned Address::getNumber() const
{
	return this->myNumber;                           
}
string Address::getStreet() const 
{
	return myStreet;
}
string Address::getSuffix() const 
{
	return mySuffix;
}
string Address::getCity()const 
{
	return myCity;
}
string Address::getState()const 
{
	return myState;
}
unsigned Address::getZip() const 
{
	return myZip;
}

// Class member function definition // mutators
Address & Address::setNumber(unsigned number) 
{
	this->myNumber = number;
	return *this;
}
Address & Address::setStreet(const string & street)
{
	this->myStreet = street;
	return *this;
}
Address & Address::setSuffix(const string & suffix)
{
	this->mySuffix = suffix;
	return *this;
}
Address & Address::setCity(const string & city)
{
	this->myCity = city;
	return *this;
}
Address & Address::setState(const string & state)
{
	this->myState = state;
	return *this;
}
Address & Address::setZip(unsigned zip)
{
	this->myZip = zip;
	return *this;
}

/*const Address & Address::show() const
{

}*/

bool die(const string & msg)
{
	cerr << endl << "FATAL ERROR: " << msg << endl;
	exit(EXIT_FAILURE);
Last edited on
If you look at the comment above the prototype for the show method, it explains what it wants. You're to print out the information in the Address object like you would for printing on an envelope.
but I don't think there is difference between what I already did in main function and show function
You were supposed to write:
1
2
3
4
5
int main() {
  Address foo( ... );
  foo.show();
  return 0;
}

It does not matter whether you can write the printout differently; the specification of the program says how it has to be done.

Your program could have a need to print addresses many times in different parts of the code. Would you really like to write the long list of get*() every time?
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <string>
#include <iostream>

using namespace std;

class Address
{
public:
    // ctor
    Address(unsigned number, string street, string suffix, string city, string state, unsigned zip);

    // accessors
    unsigned getNumber() const;
    string getStreet() const;
    string getSuffix() const;
    string getCity() const;
    string getState() const;
    unsigned getZip() const;

    // mutators
    Address & setNumber(unsigned number);
    Address & setStreet(const string & street);
    Address & setSuffix(const string & suffix);
    Address & setCity(const string & city);
    Address & setState(const string & state);
    Address & setZip(unsigned zip);

    // other: display the information in the invoking objects on 2 lines,
    //     as we would expect to see on an envelope
    const Address & show() const;
private:
    unsigned myNumber;
    string myStreet;
    string mySuffix;
    string myCity;
    string myState;
    unsigned myZip;
};

int main()
{
    Address a( 16000, "gfgfg", "Street", "fgdggd", "dg", 91344) ;
    cout << a.getNumber() << " " << a.getStreet() << " " << a.getSuffix() <<
         endl << a.getCity() << " " << a.getState() << " " << a.getZip() << endl;
    cout<<endl;

    Address loc[] =
    {
        { 6401, "gdgdg", "dgd", "Woodland Hills", "CA", 91371 },
        { 4792, "sffs", "sfsf", "sfsf", "CA", 91371 },
        { 2001, "dgdg", "dgdg", "dgd", "CA", 91371 },
        { 1234, "dgd", "dgdg", "dg", "CA", 91375 },
        { 992, "dgd", "dg", "dgdg", "CA", 91375 }
    };

    for (unsigned i = 0; i < 5; i++)
    {
        cout << loc[i].getNumber() << " " << loc[i].getStreet() << " " << loc[i].getSuffix() << endl << loc[i].getCity() << " " << loc[i].getState() << " " << loc[i].getZip() << endl;
        cout << "-------------------" << endl;
    }
}

// member function definition //ctor
Address::Address(unsigned number, string street, string suffix, string city, string state, unsigned zip)
{
    myNumber = number;
    myStreet = street;
    mySuffix = suffix;
    myCity = city;
    myState = state;
    myZip = zip;

//    if (!myNumber||myNumber<1 || myNumber>199999999)	die("Invalid Number");
//    if (myStreet == " ") die("Invalid Street");
//    if (mySuffix==" ") die("Invalid Suffix");
//    if (myCity == " ") die("Invalid City");
//    if (myState == " ") die("Invalid State");
//    if (!zip)			die("Invalid Zip");
}

// Class member function definition // accessors
unsigned Address::getNumber() const
{
    return this->myNumber;
}

string Address::getStreet() const
{
    return myStreet;
}
string Address::getSuffix() const
{
    return mySuffix;
}
string Address::getCity()const
{
    return myCity;
}
string Address::getState()const
{
    return myState;
}
unsigned Address::getZip() const
{
    return myZip;
}

// Class member function definition // mutators
Address & Address::setNumber(unsigned number)
{
    this->myNumber = number;
    return *this;
}
Address & Address::setStreet(const string & street)
{
    this->myStreet = street;
    return *this;
}
Address & Address::setSuffix(const string & suffix)
{
    this->mySuffix = suffix;
    return *this;
}
Address & Address::setCity(const string & city)
{
    this->myCity = city;
    return *this;
}
Address & Address::setState(const string & state)
{
    this->myState = state;
    return *this;
}
Address & Address::setZip(unsigned zip)
{
    this->myZip = zip;
    return *this;
}

/*const Address & Address::show() const
{

}*/

bool die(const string & msg)
{
    cerr << endl << "FATAL ERROR: " << msg << endl;
    //exit(EXIT_FAILURE);
}


Unfortunately your constructor is a mess. Have a close look at this code and where I have commented out the rubbish. Give it a whirl and see if you are getting closer. Your main problem is you haven't understood how the constructor works.
thanks #kemort for the correction I made huge mistake
So do you think I can do this first check for error then initialize.
1
2
3
4
5
6
7
8
9
10
11
12
13

if (!number || number <= 0 && number >199999) die("Invalid Number");
		else myNumber = number;
		if (street == " ") die("Invalid Street");
		else myStreet = street;
		if (mySuffix == " ") die("Invalid Suffix");
		else mySuffix = suffix;
		if (myCity == " ") die("Invalid City");
		else myCity = city;
		if (myState == " ") die("Invalid State");
		else myState = state;
		if (!zip || zip <= 0 && zip >199999)	die("Invalid Zip");
		else 	myZip = zip;
closed account (48T7M4Gy)
The short answer is try it!

My opinion is nested if's are almost impossible to debug easily. A cascade of separate if's is better IMHO.

Always make changes in short steps.
show function

1
2
3
4
5
6
7
const Address & Address::show() const
{
cout << myNumber << " " << myStreet << " " << mySuffix << endl 
<< myCity << " " << myState << " " << myZip << endl;
cout << "-------------------" << endl;
return *this;
}


what does the colon represent here

:myNumber(number), myStreet(street), mySuffix(suffix), myCity(city), myState(state), myZip(zip)

what does this represent in the return statement
*this or ->this
Last edited on
closed account (48T7M4Gy)
what does the colon represent here

I assume you mean in the constructor. I don't know, you put it there, not me :-)

this etc
ASgain, I'm surprised. You wrote it! Check out a tutorial or your teacher. :-)

Good luck ...

I know, but I am looking for some explanation about what does it do
closed account (48T7M4Gy)
Where did you get the code from?
I asked and now I know that the colon mean initialization list

and *this mean pointer to the object being worked with
Topic archived. No new replies allowed.