大佬教程收集整理的这篇文章主要介绍了C++类中的继承实例详解,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
C++类中的继承实例详解@H_696_3@
#include<iostream> #include<String> using namespace std; class Person { public: Person(const char* name = "abc") :_name(Name) { cout << "Person()" << endl; } Person(const Person& pp) :_name(pp._Name) { cout << "Person(const Person&)" << endl; } Person& operator=(const Person& pp) { cout << "Person& operator=(const Person&)" << endl; if (this != &pp) { _name = pp._name; } return *this; } ~Person() { cout << "~Person()" << endl; } protected: String _name; }; class student :public Person { public: student(const char* name,int num) :Person(Name),_num(num) { cout << "student()" << endl; } student(const student& A) :Person(A),_num(A._num) { cout << "student(const student& A)" << endl; } student& operator=(const student& A) { cout << "student& operator=(const student& A)" << endl; if (this != &A) { Person::operator=(A); _num = A._num; } return *this; } ~student() { cout << "~student()" << endl; } private: int _num; }; void test() { student a1("Peter",20); student a2(a1); a2 = a1; } int main() { test(); return 0; }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!@H_696_3@
以上是大佬教程为你收集整理的C++类中的继承实例详解全部内容,希望文章能够帮你解决C++类中的继承实例详解所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。