继承之初试1

    xiaoxiao2021-12-14  17

    这个是最简单的继承。但是千万不要小写。。。。。。

    #include<iostream> using namespace std; //--------------student class Student { public: Student(); void display(); private: int num; char sex; int score; } ; void Student::display() { cout<<num<<endl<<sex<<endl<<score<<endl; } Student::Student() { num=1; sex='M'; score=99; } //-------------------------- class Student1:public Student//记住这个要大写的Student,尴尬 { public: Student1(); void display1(); private: int adress; }; void Student1::display1() { display(); cout<<adress<<endl; } Student1::Student1() { adress=516; } //-------------------------------- int main() { Student1 a; a.display1(); }带有构造函数的继承。

    需要注意的一点是要是把构造函数声明在外部,注意他的写法。

    //简单派生类的构造函数 #include<iostream> using namespace std; //--------------student class Student { public: Student(int x,char y,int z); void display(); private: int num; char sex; int score; } ; void Student::display() { cout<<num<<endl<<sex<<endl<<score<<endl; } Student::Student(int x,char y,int z) { num=x; sex=y; score=z; } //-------------------------- class Student1:public Student//记住这个要大写的Student,尴尬 { public: Student1(int x,char y,int z,int w); void display1(); private: int adress; }; void Student1::display1() { display(); cout<<adress<<endl; } Student1::Student1(int x,char y,int z,int w):Student(x,y,z) { adress=w; } //-------------------------------- int main() { Student1 a(25,'M',100,516); a.display1(); }

    转载请注明原文地址: https://ju.6miu.com/read-970993.html

    最新回复(0)