Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

how to access prviate members of an instance in c++,


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Private members are meant to be inaccessible from outside the class. You could make username1 public and const:

#include <iostream>
#include <string>

class user1 {
    
public:
    const std::string username1;
    user1(std::string Myfirstname, std::string emailaddress, std::string); //constructor

private:
    std::string email;
    std::string mobile;

};

user1::user1(std::string Myfirstname, std::string emailaddress, std::string): username1(Myfirstname), email(emailaddress) {}

int main() {
    user1 firstman {"John" , "[email protected]" , "011000000"};
    
    std::cout << "Created " << firstman.username1 << " !
";
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...