在继承中,构造链中,先构造的后析构
见以下代码示例:
#include<iostream>
using namespace std;class animal1 {
public:animal1() {cout << "animal1 构造" << endl;}~animal1() {cout << "animal1 析构" << endl;}
};
class cat1 : public animal1 {
public:cat1() {cout << "cat1 构造" << endl;}~cat1() {cout << "cat1 析构" << endl;}
};void test1() {cat1 c;
}
int main() {test1();return 0;
}
、
继承中的同名属性访问的话,他其实都是独立的,并且内存地址也是不一样的,可见以下代码例子:
#include<iostream>
using namespace std;class animal1 {
public:animal1() {data = 1;}int data;
};
class cat1 : public animal1 {
public:cat1() {data = 2;}int data;
};void test1() {cat1 c;cout << c.data << endl;cout << c.animal1::data << endl;cout << &(c.data) << endl;cout << &(c.animal1::data) << endl;
}
int main() {test1();return 0;
}
同名函数访问机制,也是相互独立的,见以下代码示例:
#include<iostream>
using namespace std;class animal1 {
public:animal1() {}void eat() {cout << "动物" << endl;}
};
class cat1 : public animal1 {
public:cat1() {}void eat() {animal1::eat();cout << "咪咪" << endl;}
};int main() {cat1 c;c.eat();return 0;
}