码迷,mamicode.com
首页 > 编程语言 > 详细

c++之——派生类的同名成员和函数调用方式及构造析构顺序

时间:2017-03-05 16:10:16      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:stream   font   space   int   ret   派生类   .com   test   默认   

 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 }

技术分享

 

c++之——派生类的同名成员和函数调用方式及构造析构顺序

标签:stream   font   space   int   ret   派生类   .com   test   默认   

原文地址:http://www.cnblogs.com/yangguang-it/p/6505625.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!