C++简单继承

来源:互联网 发布:绿蔓的读音知乎 编辑:程序博客网 时间:2024/06/06 00:37
#include<iostream>using namespace std;class People{private:    string name;    int id;protected:    int age;    void hehe(){        cout << "hehe" << endl;    }public:    People(){name="NULL";id=-1;}    People(string nm,int i):name(nm),id(i){}    void shout(){        cout << "Aiyooooo... I'm " << name <<'\t' << endl;        hehe();    }    void setId(int i){        id=i;    }    void setname(string n){name=n;}    string getname(){return name;}};class Student:public People{private:    int sid;   // int score;public:    Student(){        People();        sid=999;    }    Student(string nm,int i,int s)    :People (nm,i),sid(s){}    string getname(){return People::getname();}    void learn(){        cout <<sid<<endl;    }    void shout(){        cout << "Haha...I'm" <<getname() << endl;    }};int main(){   // People zs("Z",1001);    Student ls("Lisi",1001,999);    ls.learn();    ls.shout();    return 0;}
原创粉丝点击