一、面向对象
1.1 类和对象
关键字class
- 类是抽象的(class定义的是类)
- 对象是具体的(具体在函数中定义类型+变量名)
1.2类中成员的访问权限
- public:都可
- private:类内可
- protected:子父可
1.3代码
#include <iostream>
#include <string>
using namespace std;
class MoonCake {
string inside;
string outside;
string taste;
public:
void init(string inside, string outside, string taste);
void Tase();
};
void MoonCake::init(string inside, string outside, string taste) {
this -> inside = inside;
this -> outside = outside;
this -> taste = taste;
}
void MoonCake::Tase() {
cout << taste << endl;
}
int main(int argc, char *argv[])
{
MoonCake wuRen;
wuRen.init("aa", "bb", "cc");
wuRen.Tase();
return 0;
}
二、构造函数
2.1提出
对 不同区域的定义的变量确定初值,如堆区,.bss,栈区初值不同
2.2格式
类名() {
}
2.3特点
类名做函数名,没有返回值
可以有参数,也可以无参
2.4代码
class Boy {
int age;
public:
Boy() {
age = 0;
}
Boy(int age = 0) {
this->age = age;
}
};
int main() {
Boy boy; //正确,age初值为0
Boy boy(); //错误,会产生歧义,即使注释掉无参的依旧会歧义,不可这么定义
return 0;
}
三、析构函数
3.1特点
~类名做函数名,没有返回值,没有参数,不能重载
3.2析构函数格式
~Boy() {
//delete i_p;
}
四、初始化列表
4.1语法规则
类名(形参1, 形参2) :成员变量1(形参1), 成员变量2(形参2) {
}
4.2代码
class Test {
const int n;
int *i_p;
string str;
public:
Test(int i):n(i), i_p(new int(i)), str("hello") {
cout << "n = " << n << nendl;
cout << "*i_p = " << i << endl;
cout << "str = " << str << endl;
}
};
4.3注意
形参列表是在构造函数之前执行的,形参列表实现的是真正的初始化操作,即开辟内存空间的同时赋初值
五、string
可以直接等号赋值(strcpy),可以加号链接(strcat),可以直接进行逻辑比较(strcmp)
5.1代码
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
string str1, str2;
str1 = "hello ";
str2 = "world";
cout << str1 << endl << str2 << endl << str1+str2 << endl;
if (str1 == "hello ") {
cout << "str1 == hello\n";
}
return 0;
}
六、new和delete
对比malloc和free
6.1代码
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int *ptr = new int;
cout << *ptr << endl;
delete ptr;
int *ptr1 = new int(5);
cout << *ptr1 << endl;
int *ptr2 = new int[5];
ptr2[3] = 6;
cout << ptr2[3] << endl;
delete ptr1;
delete[] ptr2;
return 0;
}
6.2new和malloc辨析
- 两者都用来在堆区申请内存
- new是关键字,malloc是标准C库的函数
- new以数据类型自动开辟空间,malloc以字节为单位通过用户传递字节的大小申请内存空间
- new可以对堆区内存空间初始化,malloc不行
七、拷贝构造函数
7.1深拷贝和浅拷贝
- 浅拷贝只是指针的指向相同,即两个对象的指针指向同一片内存
- 深拷贝是给新对象另开辟一块空间,并使其值和前对象相同
八、模块化编程
8.1代码
-
.h
#ifndef __OPERATER_H__ #define __OPERATER_H__ #define ZEROMIN 1e-6 class Operater { char mop; double mnum1; double mnum2; public: Operater(); Operater(Operater& that); ~Operater(); double result(); void Set(char mop, double mnum1, double mnum2); }; #endif -
.cpp
#include <iostream> #include "Operater.h" using namespace std; Operater::Operater() { mop = 0; mnum1 = 0; mnum1 = 0; } Operater::Operater(Operater& that) { mop = that.mop; mnum1 = that.mnum1; mnum2 = that.mnum2; } Operater::~Operater() { cout << "~Operater()\n"; } double Operater::result() { switch (mop) { case '+': return mnum1 + mnum2; break; case '-': return mnum1 - mnum2; break; case '/': if (mnum2 >= -ZEROMIN && mnum2 <= ZEROMIN) { cout << "zero !\n"; return -1; } return mnum1 / mnum2; break; case '*': return mnum1 * mnum2; break; default: cout << "mop err!\n"; break; } return 0; } void Operater::Set(char mop, double mnum1, double mnum2) { this ->mop = mop; this ->mnum1 = mnum1; t -
main.cpp
#include <iostream> #include "Operater.h" using namespace std; int main(int argc, char *argv[]) { Operater o1; o1.Set('/', 0, 0); cout << o1.result() << endl; o1.Set('+', 5, 6); cout << o1.result() << endl; return 0; }
九、this指针
9.1引子
成员函数不占内存空间
均放在代码段
9.2 this指针
- this指针是类中的一个隐藏的成员变量
- 指向对象的首地址
- 只有在成员函数内可见
- 类外不可见,初始化列表也不可见
9.3 用法
#include <iostream>
using namespace std;
class Test {
int i;
public:
Test& autuadd() {
cout << i << endl;
i++;
return *this;
}
Test() {
i = 0;
}
};
int main(int argc, char *argv[])
{
Test t1;
t1.autuadd().autuadd().autuadd();
return 0;
}
本文介绍了C++的基础知识,包括类与对象、构造函数、析构函数、初始化列表、string类的使用、内存管理(new和delete)、拷贝构造函数以及模块化编程。示例代码展示了如何创建类、初始化成员变量、操作字符串、动态分配内存以及实现运算符重载。此外,还探讨了成员函数中的this指针概念及其应用。
3693

被折叠的 条评论
为什么被折叠?



