Passing object values to another class

Sorry if the title is misleading, having trouble giving a proper definition of my issue.

So my assignment is to create 3 classes: Text, Box and TextBox.
Text has several values (font, size, color and data).
Box has similar values (width, hieght, border_color).
And TextBox has only two functions, SetTextBox and PrintTextBox.

Im having trouble understandingi how to pass the values from Text and Box classes to the TextBox class in order to utilise the SetTextBox function.

Main.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
#include <iostream>
#include "Text.h"
#include "Box.h"
#include "TextBox.h"

using namespace std;

int main(){

    Text text1;
    text1.SetText("Arial", 12, "Black", "Test text"); //String, int, string, string
    text1.PrintText();
    cout << "\n";

    Box box1;
    box1.SetBox(15, 15, "Red");
    box1.PrintBox();
    cout << "\n";

    cout <<"Putting both together: :" <<endl;
    TextBox piemers;
    piemers.SetTextBox(text1, box1);//I thought I can simply pass text1 and box1 objects to the TextBox class
    piemers.PrintTextBox();

    return 0;
}


TextBox.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef TEXTBOX_H
#define TEXTBOX_H
using namespace std;

class TextBox : public Text, public Box{
    public:
        TextBox();
        void SetTextBox(const Text&, const Box&);
        void PrintTextBox();

    private:
        Text text;
        Box box;

};

#endif // TEXTBOX_H 


TextBox.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
#include <iostream>
#include "Text.h"
#include "Box.h"
#include "TextBox.h"

using namespace std;

TextBox::TextBox(){

}

void TextBox::SetTextBox(const Text&, const Box&){//Not sure how to set this up and what it should look like

}

void TextBox::PrintTextBox(){
    cout << "End result: " <<endl;
    PrintText();
    PrintBox();
/*I was just trying some things out in this part, thinking I can just display the 
original values from the other class, but from what I understand, the assignment 
is supposed to utilise the SetTextBox function to set the objects from the previous 
classes to something new in this class and print that out.*/
}


The end result should essentially look like this:
1
2
3
4
5
6
7
8
End result:
Font: Arial
Size: 12
Color: Black
Data: Test text
Width: 15
Height: 15
Color: Red


I apologise if this is messy.

Any pointers would be appreciated, and if you spot any other mistakes Im making please let me know.

I didnt include the other two classes as I dont think theyre relevant for the last class
Last edited on
You're inheriting from both Text and Box, but then also have Text and Box objects as members of the class. This is certainly not the best way to do this.

It looks like you're not really using inheritance for anything useful, so my suggestion would be to not use it.

Just have class TextBox { ... }; not class TextBox : public Text, public Box { ... };

What are PrintText and PrintBox? Are those member functions of Text and Box, respectively?

If so, do this instead:

1
2
3
4
5
6
7
8
9
10
void TextBox::SetTextBox(const Text& text, const Box& box) {
    this->text = text;
    this->box = box;
}

void TextBox::PrintTextBox() {
    cout << "End result: " <<endl;
    text.PrintText();
    box.PrintBox();
}

Last edited on
@Ganado Just notice, youre right, Im not using inheritance.. I added that right at the beginning thinking I would but its not even necessary looking at it now.

And yes, those are member functions of those classes.

Your solution worked! Thanks man.
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
#include <iostream>
#include <string>

struct text {

    text() = default ; // initialise using default member initialisers

    text( const std::string& fnt, int sz, const std::string& clr, const std::string& dat )
        : font(fnt), size(sz), colour(clr), data(dat) {}

    void print() const {

        std::cout << "Font: " << font << '\n'
                  << "Size: " << size << '\n'
                  << "Text Colour: " << colour << '\n'
                  << "Data: " << data << '\n' ;
    }

    private:
        std::string font = "Ariel" ;
        int size = 10 ;
        std::string colour = "Black";
        std::string data = "Sample text" ;
};

struct box {

    box() = default ; // initialise using default member initialisers

    box( int wdth, int ht, const std::string& clr )
        : width(wdth), height(ht), colour(clr) {}

    void print() const {

        std::cout << "Width: " << width << '\n'
                  << "Height: " << height << '\n'
                  << "Box Colour: " << colour << '\n' ;
    }

    private:
        int width = 25 ;
        int height = 20 ;
        std::string colour = "Red" ;
};

struct text_box : public text, public box {

        text_box() = default ; // default initialise base class sub-objects

        // initialise (copy construct) base class sub-objects
        text_box( const text& txt, const box& bx ) : text(txt), box(bx) {}

        text_box& set( const text& txt, const box& bx ) {

            // assign to base class sub-objects
            text::operator= (txt) ;
            box::operator= (bx) ;
            return *this ;
        }

        void print() const {

            std::cout << "----------\ntext_box\n---------\n" ; // print header

            text::print() ; // print first base class stuff
            box::print() ; // print second base class stuff

            std::cout << "-------------------\n\n" ; // print footer
        }
};

int main() {

    text_box tb ; // default initialise
    tb.print() ;

    tb.set( text( "Baskerville", 22, "Magenta", "Hello World!" ),
            box( 60, 30, "Blue" ) ) ;
    tb.print() ;
}

http://coliru.stacked-crooked.com/a/d0ebaf579d3fafd4

Note: this may be an exercise to understand multiple inheritance;
in real life C++, one would consider an alternative design using composition.
@JLBorges wow thanks!
I can see this uses structures and from what I gather this is a better method of approaching problems like this, but unfortunately we havent gotten to structures just yet so we have to stick to classes.

But I will save this for future to understand it more clearly once it comes to it

Thanks so much!
Structs are the same as classes, just different default access modifiers , so don't get bogged down by that.
1
2
3
4
class Foo {
  public:
   Foo();
};

is equivalent to
1
2
3
struct Foo {
   Foo();
};


What JLBorges' example is showing is an alternative design that uses inheritance instead of composition. Both are valid ways to design a program, though personally I tend to go with composition when I can.
Last edited on
C structs are not C++ structs.

In C a struct can only contain data members. By default access is public.

C++ loosened up the struct definition, structs can contain data members and function methods. Default access is private.

If you are compiling source code as C++ then the difference between a struct and a class is the keyword used and what the default access is.

Using the struct keyword when your data members and/or function methods are to be public simply means you don't have to type the access modifier.
Last edited on
Topic archived. No new replies allowed.