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.
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.
I’ve added some comment.
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! :-)
// 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 <string>
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);
booloperator == (const Integer& i) const;
booloperator!=(const Integer& i) const;
};
#endif
#include <cstdlib> // <-- atoi()
#include <iostream>
#include <sstream>
#include <string>
#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 != '.')
{
returntrue;
}
}
returnfalse;
}
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) { returntrue; }
returnfalse;
}
bool Integer::operator!=(const Integer& i) const { return !operator==(i); }
I appreciate your help and thank you for your time. I will follow your advice next time I post my work. Greatly appreciated!
Regarding the code, I think that I'm very close to the goal because of your help. This helped me get out of what I was stuck on. Hope to see you in my future posts!