C艹笔记 1
C++: 类
封装、继承、多态
抽象、面向对象
一、常用语法
1.1 C++相对于C的特性
1. bool 类型:
- true、false;
- 一个字节,非零为真。
2.内联函数:
-
是一个函数,定义方法:在返回值类型前面加标识符 inline,见4;
-
通过“内存膨胀”的方式,以空间换取时间,目的是提高程序运行的速度;
-
一般函数体的代码不多,且需要多次反复调用可选用内联函数;
-
代码
#include <stdio.h> inline void func(int num); int main() { func(6); return 0; } inline void func(int num) { printf("void func(int num); num = %d\n",num); } /* 内联函数语法写法 inline 返回值类型 函数名(参数表) { 函数体; } */
3.函数重载:
-
在同一个项目中定义的函数名字可以重复;
-
函数名必须一致;
-
函数的参数列表(参数名或者参数类型)不同,返回值没有资格参与讨论。
-
代码
#include <iostream> using namespace std; int fun() { cout << "Hello world!" << endl; return 0; } int fun(int a, int b) { if (a > 100) { return 0; } cout << a << " "; return fun(b, a + b); } int main() { fun(); fun(1, 1); return 0; }
4.参数缺省
- 声明函数某个参数的时候指定一个默认值,调用该函数时如果采用默认值,无需指定该参数的值;
- 参数缺省只能从右往左缺省。
5.引用
-
可理解为“对一个变量取别名”,定义引用时必须要初始化。
-
代码
#include <iostream> int main() { int Number = 1; int& num = Number; std::cout << "Number = " << Number << std::endl; num = 10; std::cout << "Number = " << Number << std::endl; }
引用的本质是一个常量指针,操作的是原名地址上的内容。
6.命名空间
-
命名空间是用来组织和重用代码的编译单元;
-
通过使用命名空间来避免标识符冲突,解决重名现象;
-
作用域运算符 :
:: //作用域运算符
7.cin 和 cout
cin 和 cout 是C++中用来输出的对象。
8.new 和 delete
-
new的作用类似malloc,delete的作用类似free。
-
代码
#include <iostream> using namespace std; int main() { // 申请单个内存 int* p1 = new int; *p1 = 1; // 申请单个内存并初始化 int* p2 = new int(2); // 批量申请内存(无法初始化) int* p3 = new int[5]; p3[0] = 1; p3[2] = 2; cout << "*p1 = " << *p1 << " " << "占用内存为: " << sizeof(*p1) << endl; cout << "*p2 = " << *p2 << " " << "占用内存为: " << sizeof(*p2) << endl; cout << "*p3 = " << *p3 << " " << "占用内存为: " << sizeof(p3) << endl; // 使用完不要忘记释放 (***) delete p1; delete p2; // 注意批量释放内存的写法,且p3需指向首地址。 delete[] p3; }
输出结果:
1.2 面向对象编程思想
1. 面向过程
提出问题 —> 分析问题 —> 处理问题
大问题 分解为 小问题(从上到下一步一步解决问题,直到做完为止。)
特点:条理清晰,使问题简单化; 结构简单(三种基本结构);
数据和函数分开; 数据不具备封装性; 标识符重名现象;……
2. 面向对象
-
面向对象的程序的设计是面向过程的继承和发展,面向对象可以理解为:现实世界是有对象组成的。
-
对象在程序中抽象为两个部分:属性(数据)、行为(函数)。
-
三大特点:封装、继承、多态。
3. 类与对象
-
类是一种用户自定义的数据类型 (数据、函数);
-
类是具有相同属性 和 行为对象的集合;
-
类是对象的抽象,对象是类的具体;
-
对象:通过使用类 类型定义的变量
-
定义类:
// 语法
class 类名 // 定义类名是一般情况下首字母大写
{
// 默认是私有的
// 成员: 1.数据 2.函数
// 访问权限修饰关键字
public: // 公有的
// 成员:1 数据 2 函数
private: // 私有的
// 成员:1 数据 2 函数
protected: // 被保护的
// 成员:1 数据 2 函数
}; // 定义类时,大括号括起来的属于定义域,定义域需以分号结束。
- 注:
公有权限的属性和行为可以在类的内部和外部访问,私有权限的成员只能在类的内部(类的定义域范围以及类的成员函数的函数体都属于类的内部)访问,保护权限限定的权限可在类的内部和继承的类访问。
C++ 内如不设定访问权限,则默认为私有权限。
定义域需以分号结束,对于作用域则不需要分号。
在类的外面不可直接访问私有属性的成员,可通过公有属性成员间接访问。
代码:
#include <iostream>
using namespace std;
class Sheep
{
private:
int age;
public:
char name[32];
void setAge(int n)
{
age = n;
}
void eat()
{
cout << "闲着也是闲着,简单吃点草。" << endl;
}
void speak()
{
cout << "我叫" << name << ", 我" << age << "岁了。" << endl;
}
private:
void speakEnglish();
};
void Sheep::speakEnglish()
{
cout << "My name is" << name << "I'm" << age << "." << endl;
}
int main()
{
Sheep xiYY;
xiYY.eat();
strcpy_s(xiYY.name, "喜羊羊");
xiYY.setAge(12); // 在类的外面不可直接访问私有属性的成员,可通过公有属性成员间接访问。
xiYY.speak();
// 指针
Sheep* pY;
pY = &xiYY;
pY->eat();
pY->speak();
return 0;
}
- struct 与 class:
- 在C++中既可以用 struct 来定义一个类(struct 定义的类,默认属性为公有,而 class 中默认为私有),也可以用来定义结构体,在C语言中 struct 只能用来定义结构体;
- struct 定义的类,默认访问权限为公有,继承默认为公有;
- class 定义的类,默认访问权限为私有,继承默认为私有;
- 不要将struct <=> class 。
4. string类
- 头文件
#include <string> // 注意与C语言中的 #include <string.h> 区分开
-
String是C++中的字符串,类似于C语言中的字符数组,其中包括许多方法,使用时需要额外包含
-
代码示例:
#include <iostream> #include <string> using namespace std; class Sheep { private: int age; public: char name[32]; string name_str; void setAge(int n) { age = n; } void eat() { cout << "闲着也是闲着,简单吃点草。" << endl; } void speak() { cout << "我叫" << name << ", 我" << age << "岁了。" << endl; } private: void speakEnglish(); }; void Sheep::speakEnglish() { cout << "My name is" << name << "I'm" << age << "." << endl; } int main() { string str; str = "abc123"; char ch = str[2]; ch = str.at(1); cout << str.length() << endl; cout << str.empty()<< endl; cout << str << endl; str.clear(); cout << str << endl; cout << "***************************" << endl; Sheep xiYY; xiYY.eat(); xiYY.name_str = "xiYangYang"; cout << xiYY.name_str << endl; strcpy_s(xiYY.name, "喜羊羊"); xiYY.setAge(12); // 在类的外面不可直接访问私有属性的成员,可通过公有属性成员间接访问。 xiYY.speak(); // 指针 Sheep* pY; pY = &xiYY; pY->eat(); pY->speak(); return 0; }
<未完待续>