C++ OOP returning several different values

Hi all,
Sorry if my title is a bit misleading, not sure how to summarize the issue Im having at this point.

Basically my assignment is to use Inheritances.
But my issue at the moment is that Im not sure how to return int and strings in the same function.

Below is the example, hope it makes more sense.

Main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include "Text.h"
#include "Box.h"
#include "Text.h"

using namespace std;

int main(){

    Text text1;
    text1.SetText("Arial", 12, "Black", "This is a sample text"); //String, int, string, string
    text.PrintText();


    return 0;
}


Text.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef TEXT_H
#define TEXT_H
using namespace std;

class Text{

    public:
        Text();
        void SetText(string font, int size,  string color, string data);
        void GetParameters(); //Obviously I cant use VOID cause I want to return values, but not sure what to use as it has strings and integers.
        void PrintText();

    private:
        string font;
        int size;
        string color;
        string data;
};

#endif // TEXT_H 


Text.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include "Text.h"
#include "Box.h"
#include "Text.h"

using namespace std;

Text::Text(){
}

void Text::SetText(string font, int size,  string color, string data){
    font = font;
    size = size;
    color = color;
    data = data;
}

void Text::GetParameters (){ //How would I return these values? SO i can use them in PrintText as seen below.
}

void Text::PrintText(){
    cout <<"Text parameters are: "  <<GetParameters() <<endl;
}


Any pointers are greatly appreciated at this point.


Heres the assignment that was assigned to give some context:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Realize class Text with parameters font, size, color, data.
The following classes of Text methods should be implemented:
constructor
SetText
GetKatrsParameter
PrintText // Output Values Output

Must implement Box with parameters width, height, border_color.
The following classroom methods should be implemented:
constructor
SetBox
GetKatrsParameter
PrintBox // parameter value output

TextBox should be derived from the Text and Box classes.
The following TextBox methods should be implemented:
constructor
SetTextBox
PrintTextBox (parameter value output)

Write a program that illustrates the work of the acquired structure. Display the GetKatrsParameter function calls from the TextBox class.
Last edited on
there are 2 simple ways.
1
void foo (int &x, string &s) //passed by reference
{
s = "doh"; //whatever was passed in for them was changed. its 'returned' to the caller this way.
x = 3;
}

2
a user defined type.
myclass foo(inputs){returns a myclass object which contains the stuff you wanted}

you can also use class variables for your specific example. if you set a class member variable in text::getparams, it will be changed and readable down in text::print -- they behave much like global variables so long as you are in the text:: scope. This type of data feels like it should be private without any external access; its data you only use inside the class and the user does not need to know about it (? guessing).

Last edited on
May be use references:
void GetParameters( std::string& font, int& size, std::string& colour, std::string& data);
Usually in such a case it is a good idea to pack things in structs like so:
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
class Text{
struct Parameter
{
        string font;
        int size;
        string color;
};
  Parameter Param;

    public:
        Text();
        void SetText(string font, int size,  string color, string data);
        const Parameter &GetParameters() const
{
  return Param;
}
        void PrintText();

    private:
        string data;
};

...

int main()
{
  Text t{...};

...

  Text::Parameter param = t.GetParameters(); // A copy of the parameter
}
Topic archived. No new replies allowed.