一、

代码:

 1 #include<stdio.h>
2 #include<string.h>
3 #include<algorithm>
4 #include<iostream>
5 using namespace std;
6 class A
7 {
8 public:
9 int a,b;
10 A();
11 A(int x,int y);
12 ~A();
13 };
14 A::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 }
28 class B:public A
29 {
30 public:
31 int 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 };
47 class C:public B
48 {
49 public:
50 int 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 };
65 int main()
66 {
67 C first(0,0);
68 printf("%d %d %d %d\n",first.aaa,first.bbb,first.one.aa,first.one.bb);
69 return 0;
70 }

二、

  1 #include<stdio.h>
2 #include<string.h>
3 #include<algorithm>
4 #include<iostream>
5 using namespace std;
6 class Base
7 {
8 public:
9 Base(int x,int y)
10 {
11 a=x;
12 b=y;
13 }
14 virtual void show() //声明了虚函数,那么指针指向哪个对象就用那个对象的函数
15 { //否则的话,声明指针是什么类型就会用那个类型的函数
16 cout<<"基类内容show打印"<<endl;
17 cout<<"a:"<<a<<"b:"<<b<<endl;
18 }
19 void prinf()
20 {
21 cout<<"基类内容prinf打印"<<endl;
22 cout<<"a:"<<a<<"b:"<<b<<endl;
23 }
24 private:
25 int a,b;
26
27 };
28 class One:public Base
29 {
30 public:
31
32 One(int x,int y):Base(x,y)
33 {
34 aa=x;
35 bb=y;
36 }
37 void show()
38 {
39 cout<<"派生类内容show打印"<<endl;
40 cout<<"aa:"<<aa<<"bb:"<<bb<<endl;
41 }
42 void prinf()
43 {
44 cout<<"派生类内容prinf打印"<<endl;
45 cout<<"aa:"<<aa<<"bb:"<<bb<<endl;
46 }
47 private:
48 int aa,bb;
49 };
50 int 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();
60 return 0;
61 }
62
63 //2、
64 #include<stdio.h>
65 #include<string.h>
66 #include<algorithm>
67 #include<iostream>
68 using namespace std;
69 class Shape //抽象类定义是只要里面有一个纯虚函数那他就是抽象类,
70 { //抽象类不需要什么关键字来定义,且它的派生类必须覆盖重写他的纯虚方法
71 public :
72 double a;
73 Shape(double x)
74 {
75 a=x;
76 }
77 virtual void area()=0;
78 virtual void show()=0;
79 };
80 class Circle:public Shape
81 {
82 public:
83 Circle(double x):Shape(x)
84 {
85
86 }
87 void area()
88 {
89 cout<<"the circle area is :"<<endl;
90 cout<<3.14*a*a<<endl;
91 }
92 void show()
93 {
94 cout<<"a:"<<a<<endl;
95 }
96 };
97 int main()
98 {
99 Shape *ptr;
100 Circle r(6);
101 ptr=&r;
102 ptr->area();
103 ptr->show();
104 return 0;
105 }
106
107 //3、
108 #include<stdio.h>
109 #include<string.h>
110 #include<algorithm>
111 #include<iostream>
112 #include<typeinfo>
113 using namespace std;
114 class Sqare
115 {
116 public :
117 double a;
118 Sqare(double x)
119 {
120 a=x;
121 }
122 };
123 class Circle
124 {
125 public :
126 double a;
127 Circle(double x)
128 {
129 a=x;
130 }
131 };
132 int main()
133 {
134 Sqare one(1);
135 Circle two(2);
136 if(typeid(one)==typeid(Sqare)) //判断对象类型
137 {
138 cout<<"它是Sqare对象"<<endl;
139 }
140 else
141 {
142 cout<<"它是Circle对象"<<endl;
143 }
144
145 if(typeid(two)==typeid(Sqare))
146 {
147 cout<<"它是Sqare对象"<<endl;
148 }
149 else
150 {
151 cout<<"它是Circle对象"<<endl;
152 }
153 return 0;
154 }

c++派生类中构造函数和析构函数执行顺序、判断对象类型、抽象类、虚函数的更多相关文章

  1. C++学习笔记(6)----基类和派生类的构造函数和析构函数的执行顺序

    基类和派生类:构造函数和析构函数的执行顺序 在Visual Studio中,新建控制台工程,构造类如下: #include<iostream> using namespace std; c ...

  2. C++:派生类的构造函数和析构函数

    4.2 派生类的构造函数和析构函数4.2.1 派生类构造函数和析构函数的执行顺序 通常情况下,当创建派生类对象时,首先执行基类的构造函数,随后再执行派生类的构造函数:当撤销派生类对象时,则先执行派生类 ...

  3. C++学习之路—继承与派生(二):派生类的构造函数与析构函数

    (根据<C++程序设计>(谭浩强)整理,整理者:华科小涛,@http://www.cnblogs.com/hust-ghtao转载请注明) 由于基类的构造函数和析构函数是不能被继承的,所以 ...

  4. C++:派生类的构造函数和析构函数的调用顺序

    一.派生类 在C++编程中,我们在编写一个基类的派生类时,大致可以分为四步: • 吸收基类的成员:不论是数据成员还是函数成员,派生类吸收除基类的构造函数和析构函数之外的全部成员. • 改造基类函数:在 ...

  5. 不可或缺 Windows Native (21) - C++: 继承, 组合, 派生类的构造函数和析构函数, 基类与派生类的转换, 子对象的实例化, 基类成员的隐藏(派生类成员覆盖基类成员)

    [源码下载] 不可或缺 Windows Native (21) - C++: 继承, 组合, 派生类的构造函数和析构函数, 基类与派生类的转换, 子对象的实例化, 基类成员的隐藏(派生类成员覆盖基类成 ...

  6. cc28c_demo.cpp,派生类的构造函数和析构函数-代码示范3

    cc28c_demo.cpp,派生类的构造函数和析构函数-代码示范3 //派生类的构造函数和析构函数//派生类的构造函数(执行步骤)//--执行基类的构造函数//--执行成员对象的构造函数//--执行 ...

  7. c++, 派生类的构造函数和析构函数 , [ 以及operator=不能被继承 or Not的探讨]

    说明:文章中关于operator=实现的示例,从语法上是对的,但逻辑和习惯上都是错误的. 参见另一篇专门探究operator=的文章:<c++,operator=>http://www.c ...

  8. c++学习笔记4,派生类的构造函数与析构函数的调用顺序(一)

    測试源代码: //測试派生类的构造函数的调用顺序何时调用 //Fedora20 gcc version=4.8.2 #include <iostream> using namespace ...

  9. C++-理解构造函数、析构函数执行顺序

    先初始化序列中的函数调用,如果基类构造函数为非引用传递,则引起参数的拷贝构造 再: 先类内的成员构造函数(拷贝/默认),再类的构造函数:先基类,再派生类: 本文主要说明对象创建时构造函数的执行顺序,对 ...

随机推荐

  1. 技术基础 | Cassandra RBAC助你打击“虚拟海盗”,让他们对数据“战利品”望而不得

    现如今,我们称虚拟世界里的海盗们为"黑客",他们所追寻的战利品就是在你数据库某处的数据.   而我们能够保证你的数据安全的工具之一,就是"Cassandra基于角色的访问 ...

  2. 【高级排序算法】2、归并排序法的实现-Merge Sort

    简单记录 - bobo老师的玩转算法系列–玩转算法 -高级排序算法 Merge Sort 归并排序 Java实现归并排序 SortTestHelper 排序测试辅助类 package algo; im ...

  3. 一文读懂 Kubernetes APIServer 原理

    前言 整个Kubernetes技术体系由声明式API以及Controller构成,而kube-apiserver是Kubernetes的声明式api server,并为其它组件交互提供了桥梁.因此加深 ...

  4. C++ STL getline()函数

    getline() C++11 <string> 函数原型 //(1) istream& getline (istream& is, string& str, ch ...

  5. Angular入门到精通系列教程(11)- 模块(NgModule),延迟加载模块

    1. 摘要 2. NgModule举例.说明 3. Angular CLI生成模块 4. 延迟加载模块 5. 总结 环境: Angular CLI: 11.0.6 Angular: 11.0.7 No ...

  6. CISCO 如何重置3850交换机密码

    SUMMARY STEPS: Connect a terminal or PC to the switch. Set the line speed on the emulation software ...

  7. (Oracle)看懂Oracle执行计划(转载)

    最近一直在跟Oracle打交道,从最初的一脸懵逼到现在的略有所知,也来总结一下自己最近所学,不定时更新ing- 一:什么是Oracle执行计划? 执行计划是一条查询语句在Oracle中的执行过程或访问 ...

  8. TCMalloc源码学习(四)(小内存块释放)

    pagemap_和pagemap_cache_ PageHeap有两个map,pagemap_记录某一内存页对应哪一个span,显然可能多页对应一个span,pagemap_cache_记录某一内存页 ...

  9. collections,random

    collections模块 在内置数据类型(dict.list.set.tuple)的基础上,collections模块还提供了几个额外的数据类型:Counter.deque.defaultdict. ...

  10. 日记 + sb错误

    置顶消息cpdd 1.29 完了,文化课没了 我是废物 1.28 更新了自己的副标题 前副标题:Future never has to do with past time,but present ti ...