如何通过仅返回对象的getfunction返回字符串

时间:2015-02-26 12:14:58

标签: c++11 visual-c++

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.

    }
}

2 个答案:

答案 0 :(得分:0)

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

2)您可以将此示例用于输出人员到控制台(以及任何流):

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

答案 1 :(得分:0)

尝试向下转换为字符串 例如 String_var =(string)your_object.getObject()