c++之——派生类的同名成员和函数调用方式及构造析构顺序
2017-03-05 15:34
309 查看
1 #include<iostream> 2 using namespace std; 3 class Object { 4 public: 5 Object(int test) :a(1), b(2), c(3) { cout << "object 构造\n"; } 6 ~Object() 7 { 8 cout << "object 析构\n"; 9 } 10 int a; 11 int b; 12 int c; 13 void same_fuc() 14 { 15 cout << "object test...\n"; 16 } 17 void o_print() 18 { 19 cout << "a b c is" << a << " " << b << " " << c << endl; 20 } 21 22 }; 23 class Parent :public Object { 24 public: 25 Parent(int test):Object(1), a(100),b(200),c(300),obj1(1),obj2(2)//NOTE 26 { 27 cout << "parent 构造。。。\n"; 28 } 29 ~Parent() 30 { 31 cout << "Parent 析构。。。\n"; 32 } 33 int a; 34 int b; 35 int c; 36 void same_fuc() 37 { 38 cout << "Parent test...\n"; 39 } 40 void p_print() 41 { 42 cout << "a b c is" << a << " " << b << " " << c << endl; 43 } 44 Object obj1; 45 Object obj2; 46 }; 47 class Child :public Parent 48 { 49 public: 50 Child() :Parent(1), a(1000), b(2000), c(3000) { cout << "child 构造\n"; } 51 ~Child() 52 { 53 cout << "child 析构,,,\n"; 54 } 55 void c_print() 56 { 57 cout << "a b c is" << a << " " << b << " " << c << endl; 58 } 59 void same_fuc() 60 { 61 cout << "child test...\n"; 62 } 63 int a; 64 int b; 65 int c; 66 }; 67 68 int main() 69 { 70 71 Child c1; 72 c1.c_print(); 73 c1.a = 520;//默认等价于c1.Child::a=520; 74 c1.c_print(); 75 c1.Parent::a = 5200; 76 c1.p_print(); 77 c1.c_print(); 78 c1.Object::a = 52000; 79 c1.o_print(); 80 c1.c_print(); 81 82 c1.same_fuc();//默认等价于c1.Child::same_fuc(); 83 c1.Parent::same_fuc(); 84 c1.Object::same_fuc(); 85 return 0; 86 }
同名函数再探:
1 #include<iostream> 2 using namespace std; 3 4 class Parent { 5 public: 6 Parent(int test):a(100),b(200),c(300) 7 { 8 cout << "parent 构造。。。\n"; 9 } 10 ~Parent() 11 { 12 cout << "Parent 析构。。。\n"; 13 } 14 int a; 15 int b; 16 int c; 17 virtual void same_fuc()//不加virtual就不会产生多态 18 { 19 cout << "Parent test...\n"; 20 } 21 void p_print() 22 { 23 cout << "a b c is" << a << " " << b << " " << c << endl; 24 } 25 26 }; 27 class Child :public Parent 28 { 29 public: 30 Child() :Parent(1), a(1000), b(2000), c(3000) { cout << "child 构造\n"; } 31 ~Child() 32 { 33 cout << "child 析构,,,\n"; 34 } 35 void c_print() 36 { 37 cout << "a b c is" << a << " " << b << " " << c << endl; 38 } 39 void same_fuc() 40 { 41 cout << "child test...\n"; 42 } 43 int a; 44 int b; 45 int c; 46 }; 47 void test(Parent &p) 48 { 49 p.same_fuc(); 50 } 51 int main() 52 { 53 54 Child c1; 55 test(c1); 56 Parent p1(1); 57 test(p1); 58 return 0; 59 }
当且仅当对通过引用或者指针调用虚函数时,才会在运行时解析该调用,也只有在这种情况下对象的动态类型才有可能与静态类型不同。
一旦某个函数被声明成虚函数,则在所有派生类中它都是虚函数。
一个派生类的函数如果覆盖了某个继承而来的虚函数,那么它的参数类型必须与被它覆盖的基类函数完全一致。同样,返回类型也必须与基类匹配,该返回类型规则存在一个例外,当类的虚函数返回类型是类本身的指针或者引用时,该返回类型匹配一致规则无效。eg:如果B类由A类派生得到,则基类A的虚函数可以返回A*或者A&而派生类B对应的虚函数可以返回B*或者B&,但是这样的返回类型要求从B到A的类型是可以访问的。
相关文章推荐
- C++对象模型的那些事儿之六:成员函数调用方式
- C++对象模型的那些事儿之六:成员函数调用方式
- C++中的成员函数调用原理及this指针的传递方式
- C++如何在派生类成员函数内调用基类的成员函数(包括构造函数)
- [疑问]C/C++中为什么在类外利用多态基类指向派生类指针可以调用类的私有成员函数?
- C/C++中为什么在类外利用多态基类指向派生类指针可以调用类的私有成员函数?
- C++继承时子类定义同名成员变量时的调用继承函数的问题
- 【深度探索c++对象模型】Function语义学之成员函数调用方式
- c++ 基类已经存在virtual修饰的同名成员函数,派生类加不加无所谓,都是virtual的
- C++中通过派生类调用第二基类的普通成员函数时this指针的调整
- C++继承时子类定义同名成员变量时的调用继承函数的问题
- 使用类成员函数指针调用C++成员函数的一种方式
- C++继承时子类定义同名成员变量时的调用继承函数的问题
- 探索C++函数默认的调用方式
- 基类派生类构造析构函数调用顺序、成员初始化和销毁顺序
- C++中函数调用时的三种参数传递方式
- C++的栈的生长方向和函数调用方式
- C++继承中构造函数、析构函数调用顺序及虚函数的动态绑定
- [笔试题 5][c/c++]模板函数和普通函数的调用优先顺序
- C++:静态成员函数,静态数据成员,成员函数存储方式