-
-
[分享]C++基础八-类和对象
-
2021-8-27 17:17 4828
-
什么是类和对象
C++ 中的类(Class)可以看做是C语言中结构体(Struct)的升级。
结构体是一种构造类型,可以包含若干成员变量,每个成员变量的类型可以不同;可以通过结构体来定义结构体变量,每个变量拥有相同的性质。
C++ 中的类也是一种构造类型,但是进行了一些扩展,类的成员不但可以是变量,还可以是函数;通过类定义出来的变量有了新的名称,叫做对象。
类和对象的用法
类是创建对象的模板,一个类可以创建多个对象,每个对象都是类类型的一个变量。通过类创建对象的过程,叫做类的实例化,因此也称对象是类的一个实例,拥有类的成员变量和成员函数。
创建对象
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | #include <iostream> using namespace std; typedef struct _STUDENT { int nID; char m_szName[ 128 ]; double nScore; }STUDENT, * PSTUDENT; void ShowInfo(PSTUDENT tagStudent) { cout << "ID=" << tagStudent - >nID << endl; cout << "Name=" << tagStudent - >m_szName << endl; cout << "Score=" << tagStudent - >nScore << endl; } class CStudent { public: CStudent() { cout << "CStudent" << endl; } ~CStudent() { cout << "~CStudent" << endl; } void ShowInfo() { cout << "ID=" << m_nID << endl; cout << "Name=" << m_szName << endl; cout << "Score=" << m_nScore << endl; } public: int m_nID = 0 ; char m_szName[ 128 ] = { 0 }; double m_nScore = 0 ; }; int main() { / / 定义结构体变量 STUDENT tagStudent = { 123 , "xiaoxin" , 99 }; / / 传递结构体指针作为参数输出结构体信息 ShowInfo(&tagStudent); / / 结构体大小 cout << "sizeof struct:" << sizeof(tagStudent) << endl; / / 创建类对象 CStudent Student; / / 类对象赋值 Student.m_nID = 1 ; Student.m_nScore = 9 ; strcpy(Student.m_szName, "xiaoming" ); / / 直接输出类对象信息 Student.ShowInfo(); / / 类大小 cout << "sizeof class:" << sizeof(Student) << endl; return 0 ; } |
1、class 是C++ 中新增的关键字,专门用来定义类。
2、CStudent是类的名称,类名的首字母一般大写,以和其他的标识符区分开。
3、{ }内部是类所包含的成员变量和成员函数,它们统称为类的成员。
4、类的不同对象,数据成员是独享的,成员函数是共享的。
5、排除多态的情况下,类和结构体的成员变量在内存中的布局规划一致。
6、public是C++ 新增的关键字,它只能用在类的定义中,表示类的成员变量或成员函数具有公开的访问权限。
对象指针
new 和 delete
在C语言中,动态分配内存用malloc() 函数,释放内存用free() 函数。在C++中,这两个函数仍然可以使用,但是C++又新增了两个关键字,new 和 delete:new 用来动态分配内存,delete 用来释放内存。
1 2 3 4 5 | int * pNumber = new int ; / / 分配一个 int 数据类型的内存空间 delete pNumber; / / 释放指针pNumber指向的内存空间 char * szName = new char[ 128 ]; / / 分配 128 个连续的字符空间 delete[] szName; / / 释放指针szName指向的内存空间 |
和malloc 一样,new 也是在堆区分配内存空间,所以使用完毕后必须手动释放内存空间,否则只能等到程序运行结束时由操作系统回收,可能会造成内存泄漏。为了避免内存泄露,通常 new 和 delete、new[] 和 delete[] 操作符应该成对出现,并且不要和C语言中 malloc()、free() 一起混用。
创建对象指针
1 2 3 4 5 6 7 8 | CStudent * pStudent = new CStudent; CStudent * pStudentArray = new CStudent[ 2 ]; pStudent - >m_nID = 2 ; pStudent - >m_nScore = 8 ; strcpy_s(pStudent - >m_szName, "xiaobai" ); pStudent - >ShowInfo(); delete pStudent; delete[] pStudentArray; |
1、在栈上创建对象,形式和定义普通变量类似。
2、在堆上使用 new 关键字创建对象,必须要用一个指针指向它,使用完毕后用delete释放对象指针。
3、通过对象名字访问成员使用点号.,通过对象指针访问成员使用箭头->。
github:https://github.com/0I00II000I00I0I0
bilibili:https://space.bilibili.com/284022506
[CTF入门培训]顶尖高校博士及硕士团队亲授《30小时教你玩转CTF》,视频+靶场+题目!助力进入CTF世界