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.

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
======= Integer.h ========
#ifndef INTEGER
#define INTEGER

#include <iostream>
#include <sstream>
#include <string>

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 <iostream>
#include <sstream>
#include <string>
#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.
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! :-)

Hints:
Integer.h:
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
// 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);
    bool operator == (const Integer& i) const;
    bool operator!=(const Integer& i) const;
};

#endif 


Integer.cpp:
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
#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 != '.')
        {
            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:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include "Integer.h"

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

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!

Topic archived. No new replies allowed.