c++继承
来源:互联网 发布:淘宝比较好的灯饰店 编辑:程序博客网 时间:2024/05/18 01:50
继承分好多种,先记录点:
一、简单的
公有继承:
class Base{private:int a_number;public://Base(){}Base(int i) : a_number(i) { }int get_number() { return a_number;}void print(){ cout << "a is: "<<a_number << endl;}};class Derived : public Base{private:int d_number;public:// constructor, initializer used to initialize the base part of a Derived object.Derived(int i, int j) : Base(i), d_number(j) { };// a new member function that overrides the print( ) function in Basevoid print(){cout << "b is: " << get_number() << " ";// access number through get_number( )cout << d_number<< endl;}};int main(){Base a(2);a.print(); Derived b(3, 4);// print( ) in Baseb.print(); // print( ) in Derivedcout << "base part of b is: ";b.Base::print(); // print( ) in Basereturn 0;}
2.virtual函数的应用
class A{private:int a;public:A(int i) : a(i) { }virtual void print() {cout << a << endl;}int get_a() {//cout << "h" << endl;return a;}};class B{private:int b;public:B(int j) : b(j) { }void print() { cout << b << endl; }int get_b() { return b;}};class C : public A, public B{int c;public:C(int i, int j, int k) : A(i), B(j), c(k) { }void print() {cout << "f" << endl;A::print(); B::print();}// use print( ) with scope resolutionvoid get_ab() { cout << get_a() << " " << get_b() << endl;}// use get_a( ) and get_b( ) without scope resolution};int main(){C x(5, 8, 10);A* ap = &x; cout << "调用C:"<<endl;// use C::print( );ap->print(); B* bp = &x; cout << "调用B:"<<endl; // use B::print( );bp->print();cout << "////"<<endl;x.A::print(); // use A::print( );x.get_ab(); //use C:return 0;}
阅读全文
0 0
- c继承
- C++----------------继承
- 【c#】继承
- C++:继承
- C++::继承
- [C++]继承
- 【C++】继承
- 【C#】继承
- 【c++】继承
- 【C++】继承
- c#-继承
- 【C++】 继承
- 【C#】继承
- C++|继承
- 【C++】继承
- C/C++--私有继承
- [C/C++]继承
- c++:私有继承,公有继承,保护继承
- openstack虚拟机迁移操作
- 作为技术人员的死对头,其实我也很苦逼
- throw exception和 try catch的区别
- Python Os模块--路径、文件、系统命令等操作
- C++中含有无符号类型的表达式——有符号数与无符号数相加
- c++继承
- C++Lambda表达式用法
- python安装numpy和scipy
- 《Android开发艺术探索》读书笔记之Activity的生命周期与启动模式
- 7个习惯让你成为高级程序员
- 20二维数组
- 如何把OpenSSH公钥转换成OpenSSL格式
- 日期计算
- Zookeeper(五)集群管理