Hi all,
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.
The assignment is done, but my teacher has, maarked the work and essentially Ive done it wrong, it needs to use inheritance rather than composition. So Im a bit stuck at the moment as Im unsure as to whats the easiest way to change this now.
Heres my code:
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 27 28
|
#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
cout <<"Parameters are: " <<endl;
text1.PrintText();
cout << "\n";
Box box1;
box1.SetBox(15, 15, "Red");
cout << "Box parameters: " <<endl;
box1.PrintBox();
cout << "\n";
cout <<"Text and box put together:" <<endl;
TextBox example;
piemers.SetTextBox(text1, box1);
piemers.PrintTextBox();
return 0;
}
|
Text.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
|
#ifndef TEXT_H
#define TEXT_H
using namespace std;
class Text{
public:
Text();
void SetText(string font, int size, string color, string data);
string GetFont();
string GetColor();
string GetData();
int GetSize();
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 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
#include <iostream>
#include "Text.h"
#include "Box.h"
#include "TextBox.h"
using namespace std;
Text::Text(){
}
void Text::SetText(string font, int size, string color, string data){
this->font = font;
this->size = size;
this->color = color;
this->data = data;
}
string Text::GetFont(){
return font;
}
string Text::GetColor(){
return color;
}
string Text::GetData(){
return data;
}
int Text::GetSize(){
return size;
}
void Text::PrintText(){
cout << "Font: " <<GetFont() <<endl;
cout << "Size: " <<GetSize() <<endl;
cout << "Color: " <<GetColor() <<endl;
cout << "Data: " <<GetData() <<endl;
}
|
Box.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
|
#ifndef BOX_H
#define BOX_H
using namespace std;
class Box{
public:
Box();
void SetBox(int width, int height, string box_color);
int GetWidth();
int GetHeight();
string GetBorderColor();
void PrintBox();
private:
int width;
int height;
string border_color;
};
#endif // BOX_H
|
box.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
|
#include <iostream>
#include "Text.h"
#include "Box.h"
#include "TextBox.h"
using namespace std;
Box::Box(){
}
void Box::SetBox(int width, int height, string box_color){
this->width = width;
this->height = height;
this->border_color = box_color;
}
int Box::GetWidth(){
return width;
}
int Box::GetHeight(){
return height;
}
string Box::GetBorderColor(){
return border_color;
}
void Box::PrintBox(){
cout << "Width: " <<GetWidth() <<endl;
cout << "Height: " <<GetHeight() <<endl;
cout << "Box color: " <<GetBorderColor() <<endl;
}
|
TextBox.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
#ifndef TEXTBOX_H
#define TEXTBOX_H
using namespace std;
class TextBox : public Text, public Box{ /*This class would inherit everything from the other two classes and use
their PrintText and PrintBox functions to be displayed in PrintTextBox.*/
public:
TextBox();
void SetTextBox(const Text&, const Box&);/*Also, been told that this is incorrect if no copy constructor is described.
No idea what that means though???*/
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
|
#include <iostream>
#include "Text.h"
#include "Box.h"
#include "TextBox.h"
using namespace std;
TextBox::TextBox(){
}
void TextBox::SetTextBox(const Text& text, const Box& box){
this->text = text;
this->box = box;
}
void TextBox::PrintTextBox(){//At the moment, this does not work, it just displayes random numbers rather than the actual data
PrintText();
PrintBox();
}
|
I realise this is really lengthy and I apologise for that.
If anyone can point me in the right direction as to how to change this from compositions to inheritance based code, it would be greatly appreciated.