C++11 如何通过只返回对象的getfunction返回字符串

C++11 如何通过只返回对象的getfunction返回字符串,c++11,visual-c++,C++11,Visual C++,1你没有问,但对学习OOP非常重要。这是一个非常糟糕的主意-将类与setter和getter一起使用 2您可以使用此示例将人员输出到控制台和任何流: class Name { public: Name(); ~Name(); Name(string CustomerName, string CustomerlastName); string setFirstName(string CustomerName); string setLastName(str

1你没有问,但对学习OOP非常重要。这是一个非常糟糕的主意-将类与setter和getter一起使用

2您可以使用此示例将人员输出到控制台和任何流:

class Name {
public:
    Name();
    ~Name();
    Name(string CustomerName, string CustomerlastName);

    string setFirstName(string CustomerName);
    string setLastName(string CustomerlastName);
    string getFirstName() const;
    string getLastName() const;

private:
    string firstName;
    string lastName;
};

class Person {  //Class declaration
public:         //Public Members
    ~Person();  //destructor 
    Person();   //default constructor
    Person(Name cName, Adress cAdress, string cPersonnummer, int cSkonummer); //Constructor call with values 

    void setCusmoterNamn(Name const & cName);     //Set function Customer name
    void setCustomerAdress(Adress const & cAdress); //set function for Adress
    void setCustomerPersonNummer(string customerpersonNummer);         //set function for Customer perosnal number
    void setCustomerSkoNummer(int customerskoNummer); //set function for Customer Shoe number

    Name getCusmoterNamn() const;  //get function for customer Name
    Adress getCusmoterAdress() const; //Get function for customer family name
    string getCustomerPersonNummer();  //get function for Personal Number
    int getCustomerSkoNummer();     //get funktion for Sko nummer


private:  //Private class memebers
    Name namn;      // Object of type Name
    Adress adress; // Object of type Adress
    string persNr;  // variable of type String 
    int skoNr;   //Variable of type int
};

void PrintPersonObject(vector <Person> &Personer){ //This is used for printing complete class Person

    for (std::vector<Person>::iterator it = Personer.begin(); it != Personer.end(); ++it) {

        cout << "Name           :" //<< it->getCusmoterNamn();// CustomerName() is returning a object i.e Name, I dont knw how to return a string to be able to print.

    }
}

试着对字符串进行向下转换 例如
String_var=String您的_对象。getObject

函数PrintPersonObject正在接收包含对象person的向量。Class Person具有memebrs类名称和类地址。我需要将Person信息打印到屏幕上,但是类Person的get函数返回Name对象,并且不接受任何参数。我不知道如何返回类名memebrs字符串名。
#include <iostream>
#include <string>

using std::string;

class Name {
public:
    Name(string CustomerName, string CustomerlastName) 
    : firstName(CustomerName), lastName (CustomerlastName)
    {}

    friend std::ostream& operator << (std::ostream& stream, const Name& x) 
    { return stream << x.firstName << " " << x.lastName;}
private:
    string firstName;
    string lastName;
};


class Person {  //Class declaration
public:         //Public Members
    Person(const Name& cName, int cSkonummer) 
    : name(cName), skoNr(cSkonummer) {}; //Constructor call with values 

    friend std::ostream& operator << (std::ostream& stream, const Person& x) 
    { return stream << "ID: " << x.skoNr<< std::endl << "Name: " << x.name;}

private:  //Private class memebers
    Name name;      // Object of type Name
    int skoNr;   //Variable of type int
};

int main() 
{
    Name name("A", "B");
    Person person(name, 123);
    std::cout << person << std::endl;
}