返回不同类型的变量

时间:2019-05-01 09:07:46

标签: c++ oop inheritance

很抱歉,如果我的标题有点误导,不知道如何总结我在这一点上遇到的问题。

基本上,我的任务是使用继承。 但是我目前的问题是我不确定如何在同一函数中返回int和字符串以在另一个函数中显示。

下面是示例,希望它更有意义。

我尝试使用引用,但是由于Im无法使其正常工作,我显然做错了。

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.
}

很抱歉,如果它有点冗长,我不确定要包含多少内容来正确说明我所遇到的问题。

我试图达到的结果是非常基本的:

Text parameters are:
Font = Arial
Size = 12
Color = Black
Data = This is a sample text

值得一提的是,我不能在此作业中使用结构。

3 个答案:

答案 0 :(得分:3)

看起来您只需要返回一个字符串作为函数GetParameters的输出。请注意,当前您的返回类型为void(不返回任何内容)。

示例代码:

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;
}

答案 1 :(得分:2)

只需打印出数据成员:

void Text::PrintText()
{
    cout << "font: " << font << ", size: " << size << ", color: " << color
         << ", data: " << data << "\n";
}

如果要从非成员函数访问这些成员,请添加公共getter成员函数:

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; }
};

我建议重命名私有成员变量,以清楚地区分它们与公共成员。一种流行的方法是在其名称后添加下划线:

class Text {
    // ...

private:
    std::string font_;
    int size_;
    std::string color_;
    std::string data_;
};

答案 2 :(得分:2)

其他答案已经涵盖了std::string Text::GetParameters()函数,但是我想指出您的void Text::SetText实现的一个错误:

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

这没有任何作用,因为font = font;仅将font参数设置为已经存在的参数,其他参数也是如此。相反,请尝试以下操作:

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;
}

通过说this->font来告诉您,它不是指font参数,而是对象的font字段。现在,可以将字段正确设置为相应的参数。