2.2孙鑫C++

1、继承

动物有 吃 睡 呼吸的方法 当然 鱼也有 不用重复再定义

1)public 那里都可以访问
2.2孙鑫C++2.2孙鑫C++

1 #include <iostream.h>
 2 class Animal //类 基类
 3 {
 4 public:
 5     void eat()//吃方法
 6     {
 7     cout<<"animal eat "<<endl;
 8 
 9     }
10     void sleep()//睡觉方法
11     {
12         cout<<"animal sleep"<<endl;
13     }
14     void breathe()//呼吸方法
15     {
16         cout<<"animal breathe"<<endl;
17     }
18 };
19 class Fish: public Animal//  //子类
20 {
21 
22 
23 };
24 
25 void main()
26 
27 {
28     Animal an;
29     an.eat();
30     Fish fh;
31     fh.sleep();
32 }

View Code
2)protected 只有子类能访问 外部其他的都不能被访问

2.2孙鑫C++2.2孙鑫C++

1 #include <iostream.h>
 2 class Animal //类 基类
 3 {
 4 public:
 5     void eat()//吃方法
 6     {
 7     cout<<"animal eat "<<endl;
 8 
 9     }
10 protected:
11     void sleep()//睡觉方法
12     {
13         cout<<"animal sleep"<<endl;
14     }
15     void breathe()//呼吸方法
16     {
17         cout<<"animal breathe"<<endl;
18     }
19 };
20 class Fish: public Animal//  //子类
21 {
22     void test()
23     {
24         sleep();
25         breathe();
26     }
27 
28 };
29 
30 void main()
31 
32 {
33     Animal an;
34     an.eat();
35     Fish fh;
36     fh.sleep();
37 }

View Code
3)private 只有自己能访问 ,子类 外部都不能访问
2.2孙鑫C++2.2孙鑫C++

1 #include <iostream.h>
 2 class Animal //类 基类
 3 {
 4 public:
 5     void eat()//吃方法
 6     {
 7     cout<<"animal eat "<<endl;
 8 
 9     }
10 protected:
11     void sleep()//睡觉方法
12     {
13         cout<<"animal sleep"<<endl;
14     }
15 private:
16     void breathe()//呼吸方法
17     {
18         cout<<"animal breathe"<<endl;
19     }
20 };
21 class Fish: public Animal//  //子类
22 {
23     void test()
24     {
25         sleep();
26         breathe();
27     }
28 
29 };
30 
31 void main()
32 
33 {
34     Animal an;
35     an.eat();
36     Fish fh;
37 //    fh.sleep();
38 }

View Code
4)访问特性

2.2孙鑫C++

最多为public继承

2、继承的先后性

没有父亲 就没有孩子

1)基类 子类 构造函数的调用顺序

动物先
2.2孙鑫C++2.2孙鑫C++

1 #include <iostream.h>
 2 class Animal //类 基类
 3 {
 4 public:
 5     
 6     Animal()
 7     {
 8         cout<<"Animal construct "<<endl;
 9     }
10     void eat()//吃方法
11     {
12     cout<<"animal eat "<<endl;
13 
14     }
15 //protected:
16     void sleep()//睡觉方法
17     {
18         cout<<"animal sleep"<<endl;
19     }
20 //private:
21     void breathe()//呼吸方法
22     {
23         cout<<"animal breathe"<<endl;
24     }
25 };
26 class Fish: public Animal// 继承特性 //子类
27 {
28 public:
29     Fish()
30     {
31         cout<<"Fish construct "<<endl;
32     }
33 
34 };
35 
36 void main()
37 
38 {
39 //    Animal an;
40 //    an.eat();
41     Fish fh;
42 //    fh.sleep();
43 }

View Code
2)基类 子类 析构函数的调用顺序

鱼先
2.2孙鑫C++2.2孙鑫C++

1 #include <iostream.h>
 2 class Animal //类 基类
 3 {
 4 public:
 5     
 6     Animal()
 7     {
 8         cout<<"Animal construct "<<endl;
 9     }
10     ~Animal()
11     {
12         cout<<"Animal deconstruct "<<endl;
13     }
14     void eat()//吃方法
15     {
16     cout<<"animal eat "<<endl;
17 
18     }
19 //protected:
20     void sleep()//睡觉方法
21     {
22         cout<<"animal sleep"<<endl;
23     }
24 //private:
25     void breathe()//呼吸方法
26     {
27         cout<<"animal breathe"<<endl;
28     }
29 };
30 class Fish: public Animal// 继承特性 //子类
31 {
32 public:
33     Fish()
34     {
35         cout<<"Fish construct "<<endl;
36     }
37     ~Fish()
38     {
39         cout<<"Fish deconstruct "<<endl;
40     }
41 };
42 
43 void main()
44 
45 {
46 //    Animal an;
47 //    an.eat();
48     Fish fh;
49 //    fh.sleep();
50 }

View Code
2.2孙鑫C++

3、继承出错:没有对象的基类 子类初始化 基类没有初始化 且基类有参数

子类只能初始化无 参数的 基类

错误:无 缺省的 基类可条用
2.2孙鑫C++2.2孙鑫C++

1 #include <iostream.h>
 2 class Animal //类 基类
 3 {
 4 public:
 5     
 6     Animal(int height , int weight)
 7     {
 8         cout<<"Animal construct "<<endl;
 9     }
10     ~Animal()
11     {
12         cout<<"Animal deconstruct "<<endl;
13     }
14     void eat()//吃方法
15     {
16     cout<<"animal eat "<<endl;
17 
18     }
19 //protected:
20     void sleep()//睡觉方法
21     {
22         cout<<"animal sleep"<<endl;
23     }
24 //private:
25     void breathe()//呼吸方法
26     {
27         cout<<"animal breathe"<<endl;
28     }
29 };
30 class Fish: public Animal// 继承特性 //子类
31 {
32 public:
33     Fish()
34     {
35         cout<<"Fish construct "<<endl;
36     }
37     ~Fish()
38     {
39         cout<<"Fish deconstruct "<<endl;
40     }
41 };
42 
43 void main()
44 
45 {
46 //    Animal an;
47 //    an.eat();
48     Fish fh;
49 //    fh.sleep();
50 }

View Code
解决:象基类中传递参数 Fish():Animal(400,300)
2.2孙鑫C++2.2孙鑫C++

1 #include <iostream.h>
 2 class Animal //类 基类
 3 {
 4 public:
 5     
 6     Animal(int height , int weight)
 7     {
 8         cout<<"Animal construct "<<endl;
 9     }
10     ~Animal()
11     {
12         cout<<"Animal deconstruct "<<endl;
13     }
14     void eat()//吃方法
15     {
16     cout<<"animal eat "<<endl;
17 
18     }
19 //protected:
20     void sleep()//睡觉方法
21     {
22         cout<<"animal sleep"<<endl;
23     }
24 //private:
25     void breathe()//呼吸方法
26     {
27         cout<<"animal breathe"<<endl;
28     }
29 };
30 class Fish: public Animal// 继承特性 //子类
31 {
32 public:
33     Fish():Animal(400,300)
34     {
35         cout<<"Fish construct "<<endl;
36     }
37     ~Fish()
38     {
39         cout<<"Fish deconstruct "<<endl;
40     }
41 };
42 
43 void main()
44 
45 {
46 //    Animal an;
47 //    an.eat();
48     Fish fh;
49 //    fh.sleep();
50 }

View Code
4)常量初始化

2.2孙鑫C++2.2孙鑫C++

1 class Fish: public Animal// 继承特性 //子类
 2 {
 3 public:
 4     Fish():Animal(400,300),a(1)
 5     {
 6 //        cout<<"Fish construct "<<endl;
 7     }
 8     ~Fish()
 9     {
10 //        cout<<"Fish deconstruct "<<endl;
11     }
12 private :
13     const int a;
14 };

View Code


4、函数的覆盖

1)不要基类的

父亲结婚:花轿

儿子击婚:轿车
2.2孙鑫C++2.2孙鑫C++

1 #include <iostream.h>
 2 class Animal //类 基类
 3 {
 4 public:
 5     
 6     Animal(int height , int weight)
 7     {
 8     //    cout<<"Animal construct "<<endl;
 9     }
10     ~Animal()
11     {
12     //    cout<<"Animal deconstruct "<<endl;
13     }
14     void eat()//吃方法
15     {
16     cout<<"animal eat "<<endl;
17 
18     }
19 //protected:
20     void sleep()//睡觉方法
21     {
22         cout<<"animal sleep"<<endl;
23     }
24 //private:
25     void breathe()//呼吸方法
26     {
27         cout<<"animal breathe"<<endl;
28     }
29 };
30 class Fish: public Animal// 继承特性 //子类
31 {
32 public:
33     Fish():Animal(400,300),a(1)
34     {
35 //        cout<<"Fish construct "<<endl;
36     }
37     ~Fish()
38     {
39 //        cout<<"Fish deconstruct "<<endl;
40     }
41     void sleep()
42     {
43         cout<<"jjjjjjjjjj"<<endl;
44     }
45 private :
46     const int a;
47 };
48 
49 void main()
50 
51 {
52 //    Animal an;
53 //    an.eat();
54     Fish fh;
55     fh.sleep();
56 }

View Code


1)两个都要

父亲结婚:花轿

儿子击婚:轿车 花轿
2.2孙鑫C++2.2孙鑫C++

1 #include <iostream.h>
 2 class Animal //类 基类
 3 {
 4 public:
 5     
 6     Animal(int height , int weight)
 7     {
 8     //    cout<<"Animal construct "<<endl;
 9     }
10     ~Animal()
11     {
12     //    cout<<"Animal deconstruct "<<endl;
13     }
14     void eat()//吃方法
15     {
16     cout<<"animal eat "<<endl;
17 
18     }
19 //protected:
20     void sleep()//睡觉方法
21     {
22         cout<<"animal sleep"<<endl;
23     }
24 //private:
25     void breathe()//呼吸方法
26     {
27         
28         cout<<"animal breathe"<<endl;
29     }
30 };
31 class Fish: public Animal// 继承特性 //子类
32 {
33 public:
34     Fish():Animal(400,300),a(1)
35     {
36 //        cout<<"Fish construct "<<endl;
37     }
38     ~Fish()
39     {
40 //        cout<<"Fish deconstruct "<<endl;
41     }
42     void sleep()
43     {
44         Animal::sleep();
45         cout<<"jjjjjjjjjj"<<endl;
46     }
47 private :
48     const int a;
49 };
50 
51 void main()
52 
53 {
54 //    Animal an;
55 //    an.eat();
56     Fish fh;
57     fh.sleep();
58 }

View Code


5、对象转换

类型转换

int--char 丢失精度 截掉了三个字节

char--int ok 允许

2.2孙鑫C++

对象转换:

内存模型相同才能转换

2.2孙鑫C++

2.2孙鑫C++

输出的是 动物的 而不是 鱼的
2.2孙鑫C++2.2孙鑫C++

1 #include <iostream.h>
 2 class Animal //类 基类
 3 {
 4 public:
 5     
 6     Animal(int height , int weight)
 7     {
 8     //    cout<<"Animal construct "<<endl;
 9     }
10     ~Animal()
11     {
12     //    cout<<"Animal deconstruct "<<endl;
13     }
14     void eat()//吃方法
15     {
16     cout<<"animal eat "<<endl;
17 
18     }
19 //protected:
20     void sleep()//睡觉方法
21     {
22         cout<<"animal sleep"<<endl;
23     }
24 //private:
25     void breathe()//呼吸方法
26     {
27         
28         cout<<"animal breathe"<<endl;
29     }
30 };
31 class Fish: public Animal// 继承特性 //子类
32 {
33 public:
34     Fish():Animal(400,300),a(1)
35     {
36 //        cout<<"Fish construct "<<endl;
37     }
38     ~Fish()
39     {
40 //        cout<<"Fish deconstruct "<<endl;
41     }
42     void sleep()
43     {
44         Animal::sleep();//作用域标示符
45         cout<<"jjjjjjjjjj"<<endl;
46     }
47 private :
48     const int a;
49 };
50 void fn(Animal *pAn)
51 {
52     pAn->sleep();
53 }
54 void main()
55 
56 {
57 //    Animal an;
58 //    an.eat();
59     Fish fh;
60     Animal *pAn;
61     pAn=&fh;
62     fn(pAn);
63 //    fh.sleep();
64 }

View Code

原文链接: https://www.cnblogs.com/TFH-FPGA/p/3192398.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/95889

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月10日 上午3:19
下一篇 2023年2月10日 上午3:19

相关推荐