Reputation: 155
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 to be displayed in another function.
Below is the example, hope it makes more sense.
I've tried using references, but clearly Im doing it wrong as Im unable to get it to work.
Main.cpp:
#include <iostream>
#include "Text.h"
int main(){
Text text1;
text1.SetText("Arial", 12, "Black", "This is a sample text"); //String, int, string, string
text1.PrintText();
return 0;
}
Text.h:
#ifndef TEXT_H
#define TEXT_H
class Text{
public:
Text();
void SetText(std::string font, int size, std::string color, std::string data);
void GetParameters(); /*This is the problem area. I have setText that will set it, but how do I return these values to use in PrintText if it has different variable types?*/
void PrintText();
private:
std::string font;
int size;
std::string color;
std::string data;
};
#endif // TEXT_H
Text.cpp:
#include <iostream>
#include "Text.h"
Text::Text(){
}
void Text::SetText(std::string font, int size, std::string color, std::string data){
font = font;
size = size;
color = color;
data = data;
}
void Text::GetParameters (){//Any pointers in this would be much appreciated.
}
void Text::PrintText(){
cout <<"Text parameters are: " <<GetParameters() <<endl;//this is where Im trying to simply display the font, size, color and data values.
}
Im sorry if its a bit lengthy, I wasnt sure how much to include to properly illustrate the issue Im having.
The result Im trying to achieve is something very basic:
Text parameters are:
Font = Arial
Size = 12
Color = Black
Data = This is a sample text
Worth mentioning that I cant use structs in this assignment.
Upvotes: 2
Views: 80
Reputation: 16876
The std::string Text::GetParameters()
function is already covered by the other answers, but I'd like to point out a bug with your void Text::SetText
implementation:
void Text::SetText(std::string font, int size, std::string color, std::string data){
font = font;
size = size;
color = color;
data = data;
}
This doesn't do anything because font = font;
just sets the font
parameter to what it already is, and likewise for the other parameters. Instead, try this:
void Text::SetText(std::string font, int size, std::string color, std::string data) {
this->font = font;
this->size = size;
this->color = color;
this->data = data;
}
By saying this->font
you are telling it that you don't mean the font
parameter, but the font
field of the object. Now it's correctly setting the fields to the respective parameters.
Upvotes: 2
Reputation: 1667
It looks like you only need to return a string as the output from the function GetParameters
. Note that currently your return type is void
(returns nothing).
Example code:
std::string Text::GetParameters() {
std::string stringToReturn = "";
stringToReturn += "Font = " + font + "\n";
stringToReturn += "Size = " + std::to_string(size) + "\n";
// leave it to you to figure out the rest of the function
return stringToReturn;
}
Upvotes: 3
Reputation: 51832
Just print out the data members:
void Text::PrintText()
{
cout << "font: " << font << ", size: " << size << ", color: " << color
<< ", data: " << data << "\n";
}
If you want to access these members from non-member functions, then add public getter member functions:
class Text {
public:
// ...
const std::string& getFont() const { return font; }
int getSize() const { return size; }
const std::string& getColor() const { return color; }
const std::string& getData() const { return data; }
};
I recommend renaming the private member variables to clearly distinguish them from public members. A popular way is to just append an underscore to their name:
class Text {
// ...
private:
std::string font_;
int size_;
std::string color_;
std::string data_;
};
Upvotes: 2