Overloading +=
Oct 25, 2017 at 3:07pm UTC
Edit: added
display()
definition
Say I have a class,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
class Contact {
char name[20];
long long * m_pNumber;
short amtNumbers;
public :
Contact();
Contact(const char * tempName, const long long * tempNumber, const short amount);
~Contact();
bool isEmpty() const ;
void display() const ;
void setEmpty();
bool isValidPhonenumber(const long long phone);
Contact(const Contact &other);
Contact& Contact::operator =(const Contact& other);
Contact& Contact::operator +=(long long phone);
};
And my
operator +=
is overloaded as,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Contact& Contact::operator +=(long long phone)
{
if (isValidPhonenumber(phone)) {
long long * tmp = new long long [this ->amtNumbers + 1];
for (int i = 0; i < this ->amtNumbers; i++)
{
tmp[i] = this ->m_pNumber[i];
}
tmp[amtNumbers + 1] = phone; //if I add this line, I run into a Debug Error
delete [] this ->m_pNumber;
this ->m_pNumber = tmp;
this ->amtNumbers++;
return *this ;
}
}
When I do something in main(), such as,
1 2 3 4 5 6 7 8 9
int main() {
sict::Contact theContact("John Doe" , nullptr , 0); // sict:: intentional
theContact.display();
theContact += 14161234567LL;
theContact += 14162345678LL;
theContact += 14163456789LL;
theContact += 114164567890LL;
theContact.display();
. . . . . . .
I want the += to work as it should. But when I take it to
display()
, it doesn't seem to print. My
isValidPhonenumber()
is defined as,
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
bool Contact::isValidPhonenumber(const long long phone)
{
short first, second, third, fourth;
if (0 > phone || 1000000000000 <= phone || 10000000000 > phone) {
return false ;
}
if (phone >= 100000000000) {
first = phone / 100000000000;
second = (phone / 10000000000) % 10;
third = (phone / 1000000000) % 10;
fourth = (phone / 1000000) % 10;
if (first == 0 || second == 0 || third == 0 || fourth == 0) {
return false ;
}
}
if (phone >= 10000000000) {
first = phone / 10000000000;
third = (phone / 100000000) % 10;
fourth = (phone / 100000) % 10;
if (first == 0 || third == 0 || fourth == 0) {
return false ;
}
}
return true ;
}
My display function is defined as,
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
void Contact::display() const {
if (isEmpty()) {
cout << "Empty contact!" << endl;
}
else {
int sizeName = strlen(name);
for (int i = 0; i < sizeName; i++) {
cout << name[i];
}
cout << endl;
for (int i = 0; i < amtNumbers; i++) {
if (m_pNumber[i] >= 100000000000) {
short first = m_pNumber[i] / 100000000000;
short second = (m_pNumber[i] / 10000000000) % 10;
short third = (m_pNumber[i] / 1000000000) % 10;
cout << "(+" << first << second
<< ") " << third << (m_pNumber[i] / 100000000) % 10 << (m_pNumber[i] / 10000000) % 10
<< " " << (m_pNumber[i] / 1000000) % 10 << (m_pNumber[i] / 100000) % 10 << (m_pNumber[i] / 10000) % 10
<< "-" << (m_pNumber[i] / 1000) % 10 << (m_pNumber[i] / 100) % 10 << (m_pNumber[i] / 10) % 10 << (m_pNumber[i] / 1) % 10
<< endl;
}
else if (m_pNumber[i] >= 10000000000) {
short first = m_pNumber[i] / 10000000000;
short second = (m_pNumber[i] / 1000000000) % 10;
cout << "(+" << first
<< ") " << second << (m_pNumber[i] / 100000000) % 10 << (m_pNumber[i] / 10000000) % 10
<< " " << (m_pNumber[i] / 1000000) % 10 << (m_pNumber[i] / 100000) % 10 << (m_pNumber[i] / 10000) % 10
<< "-" << (m_pNumber[i] / 1000) % 10 << (m_pNumber[i] / 100) % 10 << (m_pNumber[i] / 10) % 10 << (m_pNumber[i] / 1) % 10
<< endl;
}
}
}
}
and my output is,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
John Doe
John Doe
Testing! Please wait:
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
John Doe
John Doe
When it should be,
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
John Doe
John Doe
(+1) 416 123-4567
(+1) 416 234-5678
(+1) 416 345-6789
(+11) 416 456-7890
Testing! Please wait:
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
..................................................
John Doe
(+1) 416 123-4567
(+1) 416 234-5678
(+1) 416 345-6789
(+11) 416 456-7890
Empty contact!
Im not looking to change up the code too much. I just want to fix the logical or syntactical errors as they are (this is for an assignment). TIA
Last edited on Oct 25, 2017 at 3:37pm UTC
Oct 25, 2017 at 4:05pm UTC
tmp[amtNumbers] = phone;
last index is one less than what you new'd above the for loop.
it worked for me at
http://cpp.sh/4e66p
Last edited on Oct 25, 2017 at 4:06pm UTC
Oct 25, 2017 at 4:12pm UTC
Jaybob66 is right, you're passing the phone as an argument to operator+= but you're only checking if it's correct and never actually add it to m_pNumber.
Oct 25, 2017 at 4:22pm UTC
Thank you both, I erroneously implemented that.
tmp[amtNumbers] = phone;
works as expected. But now Im running into an issue with re-assigning values.
1 2 3 4 5 6 7 8
int main() {
sict::Contact theContact("John Doe" , nullptr , 0); // sict:: intentional
theContact.display();
theContact += 14161234567LL;
theContact += 14162345678LL;
theContact += 14163456789LL;
theContact += 114164567890LL;
theContact.display();
This bit of code works. But when I do,
1 2 3
theContact = Contact("" , nullptr , 0);
theContact += 14161230002LL;
theContact.display();
It prints
John Doe
when it should print
Empty contact!
My constructor:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
Contact::Contact(const char * tempName, const long long * tempNumber, const short amount) {
setEmpty();
if ('\0' != tempName || nullptr != tempName) {
for (int i = 0; i < 19; i++) {
name[i] = tempName[i];
}
}
if (nullptr != tempNumber) {
for (int i = 0; i < amount; i++) {
if (isValidPhonenumber(tempNumber[i])) {
amtNumbers++;
}
}
m_pNumber = new long long [amtNumbers];
int count = 0;
for (int i = 0; i < amount; i++) {
if (isValidPhonenumber(tempNumber[i])) {
m_pNumber[count] = tempNumber[i];
count++;
}
}
}
}
and my setEmpty(),
1 2 3 4 5
void Contact::setEmpty() {
name[0] = '\0' ;
m_pNumber = nullptr ;
amtNumbers = 0;
}
For some reason, it doesnt recognize
theContact = Contact("" , nullptr , 0);
as it should.
Expected output:
Actual output:
1 2
John Doe
(+1) 416 123-0002
Oct 25, 2017 at 4:34pm UTC
How does your assignment operator look like?
Oct 25, 2017 at 4:36pm UTC
Ihatov,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Contact& Contact::operator =(const Contact& other)
{
if (this != &other) {
delete [] this ->m_pNumber;
this ->amtNumbers = other.amtNumbers;
this ->m_pNumber = new long long [amtNumbers];
for (int i = 0; i < this ->amtNumbers; ++i) {
this ->m_pNumber[i] = other.m_pNumber[i];
}
}
else {
return *this ;
}
}
Last edited on Oct 25, 2017 at 4:36pm UTC
Oct 25, 2017 at 4:46pm UTC
You aren't setting the name anywhere in your assignment operator hence it stays "John Doe".
You need to put this somewhere:
strcpy(this ->name, other.name);
Or this for loop if you want to do it manually:
for (int i = 0; i < 19; i++) name[i] = other.name[i];
Last edited on Oct 25, 2017 at 4:47pm UTC
Oct 25, 2017 at 4:55pm UTC
Thank you very much
Topic archived. No new replies allowed.