随笔 - 108,  文章 - 0,  评论 - 7,  阅读 - 78961

前面学习了winApp程序的Hello word(差不多差不多,改一下输出文字就是了)

现在主要是复习一下C++的一些基本内容,简单复习即可。

建立一个控制台项目,即 Win32 Console Application ,就是平常写C++的项目了,

然后想了一下,主要还是C++的:封装、继承、多态、重载

在MFC中,很多类之间的继承派生问题,其间的虚函数重载等问题,

比如调用某个函数,是用子类函数响应还是父类函数响应?一般会默认使用父类虚函数,而你也可以根据自己想要的功能重写子类函数。

下面是两个简单的程序,复习一下C++:

复制代码
 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 
 6 class Point {
 7 public:
 8     int x;
 9     int y;
10     char * name;
11     Point() :x(0), y(0) { name = new char[10]; }
12     Point(int x_, int y_) :x(x_), y(y_) { name = new char[10]; }
13     Point(int x_, int y_, char *name_) :x(x_), y(y_) { name = new char[10]; strcpy(name, name_); }
14     void OutPut() {
15         cout << '(' << x << ',' << y << ')' << " -> " << name << endl;
16     }
17     void UpDate(int x, int y) {
18         this->x = x;
19         this->y = y;
20     }
21     ~Point() {
22         delete[] name;
23     }
24 };
25 
26 
27 int main01() {
28     char name[10] = "我是点";
29     Point point;
30     strcpy(point.name, name);
31     point.OutPut();
32     Point point1(1,3);
33     strcpy(point1.name, name);
34     point1.OutPut();
35     Point point2(4, 8, name);
36     point2.OutPut();
37     point2.UpDate(7,1);
38     point2.OutPut();
39 
40     printf("%d", '1');
41     return 0;
42 }
43 
44 /*
45 构造函数、重载、析构函数、成员函数、public/private 、this指针、默认复制构造函数
46 
47 bool operator == (const MyWord & rhs) {    return data == rhs.data;    }
48 MyWord operator + (const MyWord & rhs);        // + 运算符
49 friend ostream & operator << (ostream & os ,const MyWord & rhs);    // << 流插入
50 friend istream & operator >> (istream & is , MyWord & rhs);        // >> 流提取
51 
52 */
复制代码

 

复制代码
 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 class Animal {
 6 public:
 7     virtual void Eat() {
 8         cout << "animal  eat -_- " << endl;
 9     }
10     virtual void Sleep() {
11         cout << "animal  sleep *_* " << endl;
12     }
13     virtual void Breathe() {
14         cout << "animal  breathe $_$ " << endl;
15     }
16 };
17 
18 class Fish:public Animal{
19 public:
20     void Eat() {
21         cout << "fish  eat -_- " << endl;
22     }
23 };
24 
25 void Function(Animal *an) {
26     an->Eat();
27 }
28 
29 int main02() {
30     Animal * animal;
31     Fish fish;
32     Fish * fi = & fish;
33     animal = &fish;
34     Function(animal);
35     Function(&fish);
36     Function(fi);
37 
38     return 0;
39 }
40 
41 
42 
43 /*
44 抽象/继承/多态
45 覆盖/隐藏
46 ---------------->>>>>>>无多态,隐藏情况下
47 Animal * animal;
48 Fish fish;
49 Fish * fi = & fish;
50 animal = &fish;
51 Function(animal);
52 Function(&fish);
53 Function(fi);
54 输出-----------------------
55 animal  eat -_-
56 animal  eat -_-
57 animal  eat -_-
58 ---->>>animal = &fish,当把fish对象的地址赋给animal对象时,C++编译器进行类型转换,认为保存的是animal对象地址。
59 先构造animal,再构造fish;基类->派生类
60 ---------------->>>>>>>多态,覆盖情况下----------各找各妈
61 Animal * animal;
62 Fish fish;
63 Fish * fi = & fish;
64 animal = &fish;
65 Function(animal);
66 Function(&fish);
67 Function(fi);
68 输出-----------------------
69 fish  eat -_-
70 fish  eat -_-
71 fish  eat -_-
72 
73 virtual 关键字,运行时会根据对象的实际类型来调用相应的函数(运行时,指向谁就是哪一个类型
74 */
复制代码

 

1、endl在输出流中插入一个换行,并刷新输出缓冲区。
2、struct默认成员公有,public,class默认成员私有,private
3、构造函数、默认构造函数
4、析构函数,释放资源
5、char *strcpy(char* dest, const char *src);把含有'\0'结束符的字符串复制到另一个地址空间,返回值的类型为char*
6、函数重载,函数参数类型/参数个数不同 -> 构成重载
7、this指针
8、复制构造函数:一个对象初始化另一个同类对象,赋值语句不会引发复制构造函数的调用;函数返回值;形参调用;
9、类型转换构造函数
10、抽象/继承/多态/覆盖/隐藏/virtual/虚函数
11、引用:引用是一个别名,是一个变量或对象的替换名称,(函数中修改形参--引用/指针
12、编辑 (预处理).cpp、编译.obj、链接.exe,

2022-07-31(07-14,拖延症晚期。。)

 


 前来补充:必须注意虚函数 -- 覆盖写法,不然真是找不到为什么调用了一个看起来很没用的函数,但实际上已经重新实现了这个功能;
(1)被隐藏的属性,在子类被强制转换成父类后,访问的是父类中的属性
(2)被覆盖的方法,在子类被强制转换成父类后,调用的还是子类自身的方法

覆盖是指:在派生类中覆盖基类中的同名函数,要求两个函数的参数个数、参数类型、返回类型都相同,且基类函数必须是虚函数。(各找各妈)(还是用自己的)
隐藏指的是在某些情况下,派生类中的函数屏蔽了基类中的同名函数,这些情况包括:(转成父类就用父类)
      ——2个函数参数相同,但基类函数不是虚函数。和覆盖的区别在于基类函数是否是虚函数。
      ——2个函数参数不同,无论基类函数是否是虚函数,基类函数都会被屏蔽。和重载的区别在于两个函数不在同一类中。

2022-09-19

posted on   夜_归_人  阅读(98)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示