c++派生类中构造函数和析构函数执行顺序、判断对象类型、抽象类、虚函数
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c++派生类中构造函数和析构函数执行顺序、判断对象类型、抽象类、虚函数,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3623字,纯文字阅读大概需要6分钟。
内容图文
一、
代码:
1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 #include<iostream> 5usingnamespace std; 6class A 7{ 8public: 9int a,b; 10 A(); 11 A(int x,int y); 12 ~A(); 13}; 14A::A() 15{ 16 printf("调用A类构造函数\n"); 17} 18 A::A(int x,int y) 19{ 20 a=x; 21 b=y; 22 printf("调用A类构造函数\n"); 23} 24 A::~A() 25{ 26 printf("调用A类析构函数\n"); 27} 28class B:public A 29{ 30public: 31int aa,bb; 32 B() 33 { 34 printf("调用B类构造函数\n"); 35 } 36 B(int x,int y) 37 { 38 aa=x; 39 bb=y; 40 printf("调用B类构造函数\n"); 41 } 42 ~B() 43 { 44 printf("调用B类析构函数\n"); 45 } 46}; 47class C:public B 48{ 49public: 50int aaa,bbb; 51 B one; 52 C(int x,int y) 53 { 54 aaa=x; 55 bbb=y; 56 one.aa=x; 57 one.bb=y; 58 printf("调用C类构造函数\n"); 59 } 60 ~C() 61 { 62 printf("调用C类析构函数\n"); 63 } 64}; 65int main() 66{ 67 C first(0,0); 68 printf("%d %d %d %d\n",first.aaa,first.bbb,first.one.aa,first.one.bb); 69return0; 70 }
二、
1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 #include<iostream> 5usingnamespace std; 6class Base 7{ 8public: 9 Base(int x,int y) 10 { 11 a=x; 12 b=y; 13 } 14virtualvoid show() //声明了虚函数,那么指针指向哪个对象就用那个对象的函数 15 { //否则的话,声明指针是什么类型就会用那个类型的函数 16 cout<<"基类内容show打印"<<endl; 17 cout<<"a:"<<a<<"b:"<<b<<endl; 18 } 19void prinf() 20 { 21 cout<<"基类内容prinf打印"<<endl; 22 cout<<"a:"<<a<<"b:"<<b<<endl; 23 } 24private: 25int a,b; 26 27}; 28class One:public Base 29{ 30public: 31 32 One(int x,int y):Base(x,y) 33 { 34 aa=x; 35 bb=y; 36 } 37void show() 38 { 39 cout<<"派生类内容show打印"<<endl; 40 cout<<"aa:"<<aa<<"bb:"<<bb<<endl; 41 } 42void prinf() 43 { 44 cout<<"派生类内容prinf打印"<<endl; 45 cout<<"aa:"<<aa<<"bb:"<<bb<<endl; 46 } 47private: 48int aa,bb; 49}; 50int main() 51{ 52 Base mb(100,200),*pc; 53 One mc(200,300); 54 pc=&mc; 55 pc->show(); 56 pc->prinf(); 57 pc=&mb; 58 pc->show(); 59 pc->prinf(); 60return0; 61} 62 63//2、 64 #include<stdio.h> 65 #include<string.h> 66 #include<algorithm> 67 #include<iostream> 68usingnamespace std; 69class Shape //抽象类定义是只要里面有一个纯虚函数那他就是抽象类, 70 { //抽象类不需要什么关键字来定义,且它的派生类必须覆盖重写他的纯虚方法 71public : 72double a; 73 Shape(double x) 74 { 75 a=x; 76 } 77virtualvoid area()=0; 78virtualvoid show()=0; 79}; 80class Circle:public Shape 81{ 82public: 83 Circle(double x):Shape(x) 84 { 85 86 } 87void area() 88 { 89 cout<<"the circle area is :"<<endl; 90 cout<<3.14*a*a<<endl; 91 } 92void show() 93 { 94 cout<<"a:"<<a<<endl; 95 } 96}; 97int main() 98{ 99 Shape *ptr; 100 Circle r(6); 101 ptr=&r; 102 ptr->area(); 103 ptr->show(); 104return0; 105} 106107//3、108 #include<stdio.h> 109 #include<string.h> 110 #include<algorithm> 111 #include<iostream> 112 #include<typeinfo> 113usingnamespace std; 114class Sqare 115{ 116public : 117double a; 118 Sqare(double x) 119 { 120 a=x; 121 } 122}; 123class Circle 124{ 125public : 126double a; 127 Circle(double x) 128 { 129 a=x; 130 } 131}; 132int main() 133{ 134 Sqare one(1); 135 Circle two(2); 136if(typeid(one)==typeid(Sqare)) //判断对象类型137 { 138 cout<<"它是Sqare对象"<<endl; 139 } 140else141 { 142 cout<<"它是Circle对象"<<endl; 143 } 144145if(typeid(two)==typeid(Sqare)) 146 { 147 cout<<"它是Sqare对象"<<endl; 148 } 149else150 { 151 cout<<"它是Circle对象"<<endl; 152 } 153return0; 154 }
原文:https://www.cnblogs.com/kongbursi-2292702937/p/11775093.html
内容总结
以上是互联网集市为您收集整理的c++派生类中构造函数和析构函数执行顺序、判断对象类型、抽象类、虚函数全部内容,希望文章能够帮你解决c++派生类中构造函数和析构函数执行顺序、判断对象类型、抽象类、虚函数所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。