#include <iostream>
using namespace std;
//定义类
struct Person {
//成员变量(属性)
int age;
//成员函数(方法)
void run() {
cout << "Person::run() - " << age << endl;
}
};
int main() {
//使用类来创建对象
Person person; //Person person = new Person(); java是这样
person.age = 10;
preson.run();
getchar();
return 0;
}
struct Person {
private: //下面的都生效
//成员变量(属性)
int age;
//成员函数(方法)
void run() {
cout << "Person::run() - " << age << endl;
}
};
class Person {
public: //下面的都生效
//成员变量(属性)
int age;
//成员函数(方法)
void run() {
cout << "Person::run() - " << age << endl;
}
};
void test() {
Person person;
person.m_age = 20;
person.run();
Person *P = &person;
p->m_age = 30;
p->run();
}
int main() {
test();
}
原文:https://www.cnblogs.com/sec875/p/12264797.html