02.类、类指针和别名、类与结构体区别

来源:互联网 发布:linux localdns查看 编辑:程序博客网 时间:2024/05/19 13:15

Test2.cpp文件:


#include<iostream>#include<string>#include<vector>using namespace std;//1.C++允许使用struct定义一个类class Student {public:    void say() {        cout << name << age << endl;    }    int getAge();//类中声明,类外定义。private:    int age;    string name;};int Student::getAge(){    return age;}int main() {    //1.正常访问    Student stu;    stu.say();    int age = stu.getAge();    //2.通过指针访问    Student* p = &stu;    p->say();    p->getAge();    cout << p->getAge() << endl;    //3.别名访问    Student &s = stu;    s.say();    s.getAge();    return 0;}
0 0
原创粉丝点击