• c++派生类中构造函数和析构函数执行顺序、判断对象类型、抽象类、虚函数


    一、

     代码:

     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类构造函数
    ");
    17 }
    18 A::A(int x,int y)
    19 {
    20     a=x;
    21     b=y;
    22     printf("调用A类构造函数
    ");
    23 }
    24 A::~A()
    25 {
    26     printf("调用A类析构函数
    ");
    27 }
    28 class B:public A
    29 {
    30 public:
    31     int aa,bb;
    32     B()
    33     {
    34         printf("调用B类构造函数
    ");
    35     }
    36     B(int x,int y)
    37     {
    38         aa=x;
    39         bb=y;
    40         printf("调用B类构造函数
    ");
    41     }
    42     ~B()
    43     {
    44         printf("调用B类析构函数
    ");
    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类构造函数
    ");
    59     }
    60     ~C()
    61     {
    62         printf("调用C类析构函数
    ");
    63     }
    64 };
    65 int main()
    66 {
    67     C first(0,0);
    68     printf("%d %d %d %d
    ",first.aaa,first.bbb,first.one.aa,first.one.bb);
    69     return 0;
    70 }
    View Code

    二、

      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 }
    View Code
  • 相关阅读:
    raid讲解 转
    更换EMC VNX系列存储故障硬盘的检查步骤
    dwarf是怎样处理的栈帧?
    栈帧啊栈帧
    perf record -c
    perf使用的问题,再看perf record,perf record 设置的采样频率,采样频率是如何体现在
    perf中branch-filter到底是干嘛的?
    perf打印调用栈的过程
    再看内核的frace架构, tracepoint宏扩展(二)
    再看内核的frace架构, tracepoint宏扩展
  • 原文地址:https://www.cnblogs.com/kongbursi-2292702937/p/11775093.html
Copyright © 2020-2023  润新知