Putting strings in an object

Hi everyone. See the comments in the code. How do I take three strings and put it inside an object, and return that object to main? This is only a part of the whole program.

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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;


class threeStrings
{
private:
   string string1, string2, string3;
public:
   threeStrings();
   bool setString1(string str1);
   bool setString2(string str2);
   bool setString3(string str3);
   string getString1();
   string getString2();
   string getString3();
};

threeStrings conjure();
string generateString();


int main()
{
   
   
}

threeStrings::threeStrings()
{
   string1 = "";
   string2 = "";
   string3 = "";
}

threeStrings conjure()
{
   threeStrings randomizedString; // declare object

   // conjure() calls generateString() three times for three different strings. 
   // How to put all this inside the object randomizedString? And return this object to main()?
   return ??;
}

string generateString()
{
   // This method generates and returns a SINGLE string to the conjure()
}

bool threeStrings::setString1(string str1)
{
   
   string1 = generateString();
   return true;

}

bool threeStrings::setString2(string str1)
{
   
   string2 = generateString();
   return true;

}
bool threeStrings::setString3(string str1)
{
   
   string3 = generateString();
   return true;

}
string threeStrings::getString1()
{
   return string1;
}
string threeStrings::getString1()
{
   return string2;
}
string threeStrings::getString1()
{
   return string3;
}
Last edited on
I'd do it like this:
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
class threeStrings
{
private:
   string string1, string2, string3;
   string generateString(); // Making this a member function
   
public:
   threeStrings();
   bool setString1(string str1);
   bool setString2(string str2);
   bool setString3(string str3);
   string getString1();
   string getString2();
   string getString3();
   
   threeStrings& conjure(); // Making this a member function
};

threeStrings& threeStrings::conjure()
{
	setString1( generateString() );
	setString2( generateString() );
	setString3( generateString() );
	return *this; // Returning the object itself.
}

string threeStrings::generateString()
{
	switch(rand()%3) // Randomly returns a pre-defined string
	{
	case 0: return "Snowy weather is cold";
	case 1: return "I like tomato sauce";
	case 2: return "King Sombre has captured the crystal empire!";
	}
}
Topic archived. No new replies allowed.