### Complex Types - Strings

Hello guys,

I am completely lost on numbers 3 and 4. As for number 2, I am not sure if I'm on the right path. Any insight on this would be greatly appreciated!

2. An overloaded equals function that takes a string as an argument

You need to add an equals(string s) function. So here you need to convert a string to an int and assign it to the data of the Integer object. atoi() function is a good choice, but you will need to get to the char array of the std::string.

3. An overloaded = operator that takes a string

Since you have a function called equals(string s), adding an = overload operator should be easy. !!! Code reuse.

4. An overloaded constructor that takes a string as an argument

Again, you have an equals(string s) function. Use it.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119`` ``````======= Integer.h ======== #ifndef INTEGER #define INTEGER #include #include #include using std::string; class Integer { private: int data; public: Integer(); Integer(const Integer &i); Integer(int i); string toString(); void equals(string s); bool isNaN(string s); void equals(int i); int Integer::getNum() const; const Integer& operator = (const string& equals); bool Integer::operator == (Integer i); bool Integer::operator!=(Integer i); int toInt() const; }; #endif ======= Integer.cpp ======== #include #include #include #include "Integer.h" using namespace std; Integer::Integer() :data(0) { } Integer::Integer(const Integer &i) { equals(i.toInt()); } Integer::Integer(int i) { equals(i); } void Integer::equals(int i) { data = i; } int Integer::getNum() const { return this->data; } const Integer& Integer::operator=(const string& s) { } bool Integer::operator==(Integer i) { if (this->data == i.data) return true; return false; } bool Integer::operator!=(Integer i) { return !operator==(i); } string Integer::toString() { stringstream ss; ss << data; string newString = ss.str(); cout << "The string equals " << newString << endl; return newString; } void Integer::equals(string s) { if (isNaN(s) == true) { cout << "Cannot assign a character to Class Integer" << endl; } else if (isNaN(s) == false) { cout << "Valid number" << endl; this->data = stoi(s); } } bool Integer::isNaN(string s) { string::iterator ro; for (ro = s.begin(); ro < s.end(); ro++) { if (!isdigit(*ro) && *ro != '.') { return true; } } return false; }``````
You can add some ‘const’ here and there to improve safety ;-)
I usually keep the prototypes in my header files and the definitions in my source files in the same order to find them faster - that’s personal, of course.

Please consider adding the code to make your classes compile and run without any extra effort next time: people here are willing to help, but writing trivial lines of code just to perform tests is incredibly boring. Help others help you! :-)

Hints:
Integer.h:
 ``123456789101112131415161718192021222324252627282930313233343536373839`` ``````// I am completely lost on numbers 3 and 4. As for number 2, I am not sure // if I'm on the right path. Any insight on this would be greatly appreciated! // 2. An overloaded equals function that takes a string as an argument // You need to add an equals(string s) function. So here you need to // convert a string to an int and assign it to the data of the Integer // object. atoi() function is a good choice, but you will need to get to // the char array of the std::string. // 3. An overloaded = operator that takes a string // Since you have a function called equals(string s), adding an = overload // operator should be easy. !!! Code reuse. // 4. An overloaded constructor that takes a string as an argument // Again, you have an equals(string s) function. Use it. #ifndef INTEGER #define INTEGER #include class Integer { private: int data; public: Integer(); Integer(const Integer &i); Integer(int i); Integer(std::string s); void equals(int i); void equals(const std::string& s); bool isNaN(const std::string& s) const; int getNum() const; std::string toString(); int toInt() const; Integer& operator = (const std::string& value); bool operator == (const Integer& i) const; bool operator!=(const Integer& i) const; }; #endif ``````

Integer.cpp:
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192`` ``````#include // <-- atoi() #include #include #include #include "Integer.h" Integer::Integer() : data(0) {} Integer::Integer(const Integer &i) { equals(i.toInt()); } Integer::Integer(int i) { equals(i); } Integer::Integer(std::string s) { // Transform string s into an integer and assign it to 'data'. // Your equals(std::string) does it } void Integer::equals(int i) { data = i; } // 2. An overloaded equals function that takes a string as an argument // You need to add an equals(string s) function. So here you need to // convert a string to an int and assign it to the data of the Integer // object. atoi() function is a good choice, but you will need to get to // the char array of the std::string. void Integer::equals(const std::string& s) { if (isNaN(s) == true) // <-- you can simplify this { std::cout << "Cannot assign a character to Class Integer\n"; } else // if (isNaN(s) == false) <-- or true or false, you don't need this { std::cout << "Valid number\n"; data = std::stoi(s); // ok, but your teacher wants std::atoi() in cstdlib // to obtain a const char* from a std::string you can use std::string::c_str() // data = ... ? } } bool Integer::isNaN(const std::string& s) const { // I can't believe you haven't done the range-for yet... // A std::string is a container for chars - you can simplify the // following code. std::string::const_iterator ro; for (ro = s.cbegin(); ro != s.cend(); ++ro) { if (!isdigit(*ro) && *ro != '.') { return true; } } return false; } int Integer::getNum() const { return data; } std::string Integer::toString() { // it also exists std::to_string() std::stringstream ss; ss << data; std::string newString = ss.str(); // you can avoid this ;-) return newString; } int Integer::toInt() const { // What do you want this function to do? // It seems identical to getNum() to me... return data; } // 3. An overloaded = operator that takes a string // Since you have a function called equals(string s), adding an = overload // operator should be easy. !!! Code reuse. Integer& Integer::operator=(const std::string& s) { // your property 'data' should be assigned the value of s once turned // into an int. // reuse equals(std::string) return *this; } bool Integer::operator==(const Integer& i) const { if (data == i.data) { return true; } return false; } bool Integer::operator!=(const Integer& i) const { return !operator==(i); }``````

main.cpp:
 ``12345678910`` ``````#include #include "Integer.h" int main() { Integer myint("13"); myint = 666; std::cout << "myint: " << myint.getNum() << '\n'; return 0; }``````