Skip to content

Instantly share code, notes, and snippets.

@zbjxb
Last active January 2, 2016 08:59
Show Gist options
  • Save zbjxb/8280488 to your computer and use it in GitHub Desktop.
Save zbjxb/8280488 to your computer and use it in GitHub Desktop.
给胖子演示一下c++类成员函数中能访问同类对象的私有数据成员。
#include <iostream>
using namespace std;
class Car {
public:
Car(int num, const string& engineer) : m_nWheel(num), m_strEngineer(engineer) {}
void display(Car& car);
private:
void joke(){}
private:
int m_nWheel;
string m_strEngineer;
};
void Car::display(Car& car) {
car.joke();
cout << "Car's made up:\n";
cout << "Wheel number: " << car.m_nWheel << endl;
cout << "Engineer name: " << car.m_strEngineer << endl;
}
int main(int argc, char** argv) {
Car A(4, "V12"),B(6, "BMW");
A.display(B);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment