C++
2021/12/13 12:47:11
本文主要是介绍C++,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
一、类
1、类就是C语言里面的结构体
2、函数放在结构体里面和外面,区别是需要用结构体变量(变量就是c++中的对象)调用,结构体大小没有发生改变
3、调用结构体函数时,就算一一个参数都不传递,汇编层面会传递一个结构体首地址的参数
4、封装就是将函数放在结构体里面
5、this指针是为了区分成员与参数同名,或者是获取结构体首地址
二、构造函数
1、函数(方法)不能写返回值
2、函数(方法)要与结构(类)名一样
3、初始化对象用的(创建一个变量(对象)就调用了)
4、构造函数可以有多个
5、重载函数判断依据是参数的个数和类型不一样就可以
#include "stdafx.h" struct Test { int age; int level; //构造函数 Test(int age, int level) { this->age = age; this->level = level; } //重载构造函数 Test() { } //成员函数 void Print() { printf("%d %d",this->age,this->level); } }; int main(int argc, char* argv[]) { Test p(1,2); p.Print(); getchar(); return 0; }
三、析构函数
1、只能有一个析构函数,不能重载
2、不能带任何参数
3、不能带返回值
4、主要用于清理工作
5、编译器不要求必须提供
#include "stdafx.h" #include "malloc.h" struct Person { int age; int level; char* arr; Person(int age,int level) { printf("有参构造函数执行了...\n"); this->age = age; this->level = level; arr = (char*)malloc(1024); } ~Person() { printf("析构函数执行了..."); free(arr); arr = NULL; } void Print() { printf("%d-%d\n",age,level); } }; void Test() { Person p(1,2); p.Print(); } int main(int argc, char* argv[]) { Test(); getchar(); return 0; }
四、继承
1、继承就是数据的复制
2、减少重复代码的编写
3、Person 称为父类或者基类
4、Teacher、Student称为子类或者派生类
5、t和s可以称为对象或者实例
6、可以用父类指针指向子类的对象(但是不能访问子类成员)
#include "stdafx.h" struct Person { int age; int sex; }; struct Teacher:Person { int level; int classId; }; struct Student:Person { int code; int score; }; void Test() { Student t; t.age = 1; t.sex = 2; t.code = 3; t.score = 4; Person* pt = &t; printf("%d\n",pt->age); } int main(int argc, char* argv[]) { Test(); getchar(); return 0; }
五、多层继承
不推荐写法
#include "stdafx.h" struct X { int a; int b; }; struct Y:X { int a; int d; }; struct Z:Y { int e; int f; }; void Test() { Z z; z.X::a = 1; z.b = 2; z.Y::a = 3; z.d = 4; z.e = 5; z.f = 6; printf("%d\n",sizeof(z)); } int main(int argc, char* argv[]) { Test(); getchar(); return 0; }
六、多重继承
不推荐写法
#include "stdafx.h" struct X { int a; int b; }; struct Y { int a; int d; }; struct Z:X,Y { int e; int f; }; void Test() { Z z; z.X::a = 1; z.b = 2; z.Y::a = 3; z.d = 4; z.e = 5; z.f = 6; printf("%d\n",sizeof(z)); } int main(int argc, char* argv[]) { Test(); getchar(); return 0; }
七、public和private
不能编译,因为私有属性不能直接调用,需要在结构体内调用
#include "stdafx.h" struct Test { private: int x; public: int y; }; int main(int argc, char* argv[]) { Test p; p.x = 10; return 0; }
八、使用指针调用私有属性
-
private修饰的成员与普通的成员没有区别 只是编译器会检测.
-
private修饰的成员只要自己的其他成员才能访问
#include "stdafx.h" struct Test { private: int x; public: int y; void Init(int x,int y) { this->x = x; this->y = y; } }; int main(int argc, char* argv[]) { Test t; t.Init(1,2); int* p = (int*)&t; int n = *p; int m = *(p+1); printf("%d %d\n",n,m); getchar(); return 0; }
九、class与struct的区别
1、class 默认是private,必须手动添加public才能访问
2、struct默认是public,直接就可以访问
3、class 继承需要手动添加如何继承,不添加默认是private继承,手动添加public,原来是public继承就是public,原来继承private,继承就是private
4、struct继承直接可以访问
十、私有属性继承
继承,相当于将父类的属性复制一份,但是编译器不允许我们使用私有的,但我们可以通过指针来调用
#include "stdafx.h" class Base { public: Base() { x = 11; y = 12; } private: int x; int y; }; class Sub:Base { public: int a; int b; }; int main(int argc, char* argv[]) { Sub sub; sub.a = 1; sub.b = 2; int* p = (int*)⊂ printf("%d\n",sizeof(sub)); printf("%d\n",*(p+0)); printf("%d\n",*(p+1)); printf("%d\n",*(p+2)); printf("%d\n",*(p+3)); getchar(); return 0; }
这篇关于C++的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-07fastcgi 是什么-icode9专业技术文章分享
- 2024-10-07fastcgi 的详细使用教程介绍-icode9专业技术文章分享
- 2024-10-07git如何更新单个文件到本地-icode9专业技术文章分享
- 2024-10-07如何使用ASM(Abstract Syntax Tree Manipulation)技术来修改第三方AAR依赖中的函数-icode9专业技术文章分享
- 2024-10-07Activity 跳转时间耗时很长怎么优化解决-icode9专业技术文章分享
- 2024-10-07Androud Toast 有哪些常用的第三方组件-icode9专业技术文章分享
- 2024-10-07在viewmodel中怎么使用 mmkv?-icode9专业技术文章分享
- 2024-10-07MMKV.defaultMMKV() 是单例模式吗?-icode9专业技术文章分享
- 2024-10-04el-table 开启定时器下,表格的选中状态会消失是什么原因-icode9专业技术文章分享
- 2024-10-03如何安装和初始化飞牛私有云 fnOS?-icode9专业技术文章分享