关于类继承的几点注意事项

时间:2022-12-07 05:31:22
  1 //  main.cpp
  2 //  Inherit
  3 
  4 #include <iostream>
  5 
  6 class Toy
  7 {
  8 public:
  9     Toy()
 10     {
 11         std::cout << "Construct Toy" << std::endl;
 12     }
 13     
 14     virtual ~Toy()
 15     {
 16         std::cout << "Destroy Toy" << std::endl;
 17     }
 18 };
 19 
 20 class Father
 21 {
 22 public:
 23     int f;
 24     int g;
 25     
 26     Father():f(1)
 27     {
 28         std::cout << "Construct Father" << std::endl;
 29     }
 30     
 31     virtual ~Father()
 32     {
 33         std::cout << "Destroy Father" << std::endl;
 34     }
 35     
 36     virtual void output()
 37     {
 38         std::cout << "Father's f:" << f << " Father's g:" << g << std::endl;
 39     }
 40 };
 41 
 42 class Child : public Father
 43 {
 44 public:
 45     int f;  //子类可以拥有和父类同名的成员数据
 46     int c;
 47     static int son; //类的静态成员数据的声明
 48     int grandson;
 49     double *d;
 50     Father father;
 51     Toy toy;
 52 
 53     Child():f(8), c(2)
 54     {
 55         std::cout << "Construct Child" << std::endl;
 56     }
 57     
 58     virtual ~Child()
 59     {
 60         std::cout << "Destroy Child" << std::endl;
 61     }
 62     
 63     virtual void output()
 64     {
 65         double x, *y;
 66         std::cout << "x:" << x << " y:" << y << std::endl;  //未初始化局部变量的值为废值
 67         std::cout << "Father's f:" << Father::f << " Father's g:"  << g << " Child's f:" << f << " Child's c:" << c << std::endl;   //父类成员数据f在子类中遭屏蔽,通过Father::再现
 68         std::cout << "Child's grandson:" << grandson << " Child's d:" << d << std::endl;
 69     }
 70     
 71     /*注:非静态成员函数可以访问非静态成员和静态成员,而静态成员函数只可以访问静态成员,不可以访问非静态成员*/
 72     static void outlet()
 73     {
 74         //std::cout << "Child's c:" << c << std::endl;  //error
 75         std::cout << "Child's son:" << son << std::endl;    //静态成员函数只可以访问静态成员数据
 76     }
 77 };
 78 
 79 int Child::son; //类的静态成员数据的定义
 80 float global;   //全局变量的定义
 81 
 82 int main(int argc, const char * argv[])
 83 {
 84     Father *father = new Father();
 85     father->output();   //非静态成员函数(包括虚函数)只能由对象来调用,不可由类名调用
 86     delete father;
 87     father = NULL;
 88 
 89     father = new Child();
 90     father->output();   //利用父类指针和virtual函数实现的多态机制
 91     delete father;
 92     father = NULL;
 93     
 94     Child::outlet();   //静态成员函数可用类名调用
 95     
 96     Child *child = new Child();
 97     child->outlet();   //静态成员函数也可用对象调用
 98     delete child;
 99     child = NULL;
100     
101     std::cout << "global:" << global << std::endl;  //全局变量由系统默认初始化为0
102     
103     return 0;
104 }

控制台输出结果如下:

Construct Father

Father's f:1 Father's g:32767

Destroy Father

Construct Father

Construct Father

Construct Toy

Construct Child

x:6.95322e-310 y:0x0

Father's f:1 Father's g:0 Child's f:8 Child's c:2

Child's grandson:0 Child's d:0x0

Destroy Child

Destroy Toy

Destroy Father

Destroy Father

Child's son:0

Construct Father

Construct Father

Construct Toy

Construct Child

Child's son:0

Destroy Child

Destroy Toy

Destroy Father

Destroy Father

global:0

Program ended with exit code: 0