菜鸟教程Cpp学习笔记
一、面向对象开发的特征
面向对象开发的四大特性:
- 封装
- 抽象
- 继承
- 多态
二、C++ 中的类型限定符
限定符 | 含义 |
---|---|
const | const 类型的对象在程序执行期间不能被修改改变。 |
volatile | 修饰符 volatile 告诉编译器不需要优化volatile声明的变量,让程序可以直接从内存中读取变量。对于一般的变量编译器会对变量进行优化,将内存中的变量值放在寄存器中以加快读写效率。 |
restrict | 由 restrict 修饰的指针是唯一一种访问它所指向的对象的方式。只有 C99 增加了新的类型限定符 restrict。 |
三、存储类
从 C++ 17 开始,auto 关键字不再是 C++ 存储类说明符,且 register 关键字被弃用。
auto
自 C++ 11 以来,auto 关键字用于两种情况:声明变量时根据初始化表达式自动推断该变量的类型、声明函数时函数返回值的占位符。
自动推敲初始化
auto f=3.14; //double
auto s("hello"); //const char*
auto z = new auto(9); // int*
auto x1 = 5, x2 = 5.0, x3='r';//错误,必须是初始化为同一类型
register 存储类
register 存储类用于定义存储在寄存器中而不是 RAM 中的局部变量。这意味着变量的最大尺寸等于寄存器的大小(通常是一个词),且不能对它应用一元的 ‘&’ 运算符(因为它没有内存位置)。
static 存储类
static 存储类指示编译器在程序的生命周期内保持局部变量的存在,而不需要在每次它进入和离开作用域时进行创建和销毁。因此,使用 static 修饰局部变量可以在函数调用之间保持局部变量的值。
static 修饰符也可以应用于全局变量。当 static 修饰全局变量时,会使变量的作用域限制在声明它的文件内。
在 C++ 中,当 static 用在类数据成员上时,会导致仅有一个该成员的副本被类的所有对象共享。
extern 存储类
extern 存储类用于提供一个全局变量的引用,全局变量对所有的程序文件都是可见的。当您使用 ‘extern’ 时,对于无法初始化的变量,会把变量名指向一个之前定义过的存储位置。
当您有多个文件且定义了一个可以在其他文件中使用的全局变量或函数时,可以在其他文件中使用 extern 来得到已定义的变量或函数的引用。可以这么理解,extern 是用来在另一个文件中声明一个全局变量或函数。
extern 修饰符通常用于当有两个或多个文件共享相同的全局变量或函数的时候,如下所示:
//file1.cpp
#include <iostream>int count ;
extern void write_extern(); //别的地方定义了这个函数 如果在这个文件里面也对这个函数做定义动作,将报错int main()
{count = 5;write_extern();
}
//file2.cpp
#include <iostream>extern int count;void write_extern(void)
{std::cout << "Count is " << count << std::endl;
}
执行 g++ file1.cpp file2.cpp -o file
输出 Count is 5
mutable 存储类
mutable 说明符仅适用于类的对象,这将在本教程的最后进行讲解。它允许对象的成员替代常量。也就是说,mutable 成员可以通过 const 成员函数修改。
thread_local 存储类
使用 thread_local 说明符声明的变量仅可在它在其上创建的线程上访问。 变量在创建线程时创建,并在销毁线程时销毁。 每个线程都有其自己的变量副本。
thread_local 说明符可以与 static 或 extern 合并。
可以将 thread_local 仅应用于数据声明和定义,thread_local 不能用于函数声明或定义。
#include <string>
#include <vector>thread_local int x; // 命名空间下的全局变量
class X
{static thread_local std::string s; // 类的static成员变量
};
// static thread_local std::string X::s; // X::s 是需要定义的void foo()
{thread_local std::vector<int> v; // 本地变量
}int main()
{return 0;
}
四、函数
传值调用、指针调用、引用调用
指针调用:void swap(int *x, int *y)
引用调用:void swap(int &x, int &y)
//引用调用
void swap(int &x, int &y)
{int temp;temp = x; /* 保存地址 x 的值 */x = y; /* 把 y 赋值给 x */y = temp; /* 把 x 赋值给 y */return;
}
五、数组
指向数组的指针
double runoobAarray[50];
,runoobAarray 是一个指向 &runoobAarray[0] 的指针。
*(runoobAarray + 4)
是一种访问 runoobAarray[4] 数据的合法方式。
double *p = runoobAarray; *(p + i)
等同于 *(runoobAarray + i)
传递数组给函数
void myFunction(int *param);
void myFunction(int param[10]);
void myFunction(int param[]);
从函数返回数组
#include <iostream>
#include <cstdlib>
#include <ctime>using namespace std;// 要生成和返回随机数的函数
int * getRandom( )
{static int r[10]; //不能用局部变量,函数用后被清理,局部将空指针// 设置种子srand( (unsigned)time( NULL ) );for (int i = 0; i < 10; ++i){r[i] = rand();cout << r[i] << endl;}return r;
}// 要调用上面定义函数的主函数
int main ()
{// 一个指向整数的指针int *p;p = getRandom();for ( int i = 0; i < 10; i++ ){cout << "*(p + " << i << ") : ";cout << *(p + i) << endl;}return 0;
}
六、指针
指针是一个变量,其值为另一个变量的地址,即,内存位置的直接地址。
指针的算术运算
自增自减,跨度是其数据类型的字节数
指针的比较
循环写成这样 ptr <= &var[MAX - 1]
指针 vs 数组
int var[MAX] = {10, 100, 200};
var++; // 这是不正确的
*(var + 2) = 500; //这是可以的
指针数组
#include <iostream>using namespace std;
const int MAX = 3;int main ()
{int var[MAX] = {10, 100, 200};int *ptr[MAX];for (int i = 0; i < MAX; i++){ptr[i] = &var[i]; // 赋值为整数的地址}for (int i = 0; i < MAX; i++){cout << "Value of var[" << i << "] = ";cout << *ptr[i] << endl;}return 0;
}
#include <iostream>using namespace std;
const int MAX = 4;int main ()
{const char *names[MAX] = {"Zara Ali","Hina Ali","Nuha Ali","Sara Ali",};for (int i = 0; i < MAX; i++){cout << "Value of names[" << i << "] = ";cout << names[i] << endl;}return 0;
}
指向指针的指针
多级间接寻址
传递指针给函数
希望变量保存在函数里面的修改
从函数返回指针
#include <iostream>
#include <ctime>
#include <cstdlib>using namespace std;// 要生成和返回随机数的函数
int * getRandom( )
{static int r[10];// 设置种子srand( (unsigned)time( NULL ) );for (int i = 0; i < 10; ++i){r[i] = rand();cout << r[i] << endl;}return r;
}// 要调用上面定义函数的主函数
int main ()
{// 一个指向整数的指针int *p;p = getRandom();for ( int i = 0; i < 10; i++ ){cout << "*(p + " << i << ") : ";cout << *(p + i) << endl;}return 0;
}
七、引用
引用变量是一个别名,也就是说,它是某个已存在变量的另一个名字。一旦把引用初始化为某个变量,就可以使用该引用名称或变量名称来指向变量。
C++ 引用 vs 指针
引用很容易与指针混淆,它们之间有三个主要的不同:
- 不存在空引用。引用必须连接到一块合法的内存。
- 一旦引用被初始化为一个对象,就不能被指向到另一个对象。指针可以在任何时候指向到另一个对象。
- 引用必须在创建时被初始化。指针可以在任何时间被初始化。
#include <iostream>using namespace std;int main ()
{// 声明简单的变量int i;double d;// 声明引用变量int& r = i;double& s = d;i = 5;cout << "Value of i : " << i << endl;cout << "Value of i reference : " << r << endl;d = 11.7;cout << "Value of d : " << d << endl;cout << "Value of d reference : " << s << endl;return 0;
}
把引用作为参数
void swap(int& x, int& y);
把引用作为返回值
#include <iostream>using namespace std;double vals[] = {10.1, 12.6, 33.1, 24.1, 50.0};double& setValues(int i) { double& ref = vals[i]; return ref; // 返回第 i 个元素的引用,ref 是一个引用变量,ref 引用 vals[i]
}// 要调用上面定义函数的主函数
int main ()
{cout << "改变前的值" << endl;for ( int i = 0; i < 5; i++ ){cout << "vals[" << i << "] = ";cout << vals[i] << endl;}setValues(1) = 20.23; // 改变第 2 个元素setValues(3) = 70.8; // 改变第 4 个元素cout << "改变后的值" << endl;for ( int i = 0; i < 5; i++ ){cout << "vals[" << i << "] = ";cout << vals[i] << endl;}return 0;
}
八、基本的输入输出
C++ 的 I/O 发生在流中,流是字节序列。
头文件 | 函数和描述 |
---|---|
该文件定义了 cin、cout、cerr 和 clog 对象,分别对应于标准输入流、标准输出流、非缓冲标准错误流和缓冲标准错误流。 | |
该文件通过所谓的参数化的流操纵器(比如 setw 和 setprecision),来声明对执行标准化 I/O 有用的服务。 | |
该文件为用户控制的文件处理声明服务。我们将在文件和流的相关章节讨论它的细节。 |
标准错误流(cerr)
cerr 对象附属到标准输出设备,通常也是显示屏,但是 cerr 对象是非缓冲的,且每个流插入到 cerr 都会立即输出。
char str[] = "Unable to read....";
cerr << "Error message : " << str << endl;
标准日志流(clog)
clog 对象附属到标准输出设备,通常也是显示屏,但是 clog 对象是缓冲的。这意味着每个流插入到 clog 都会先存储在缓冲区,直到缓冲填满或者缓冲区刷新时才会输出。
#include <iostream>using namespace std;int main( )
{char str[] = "Unable to read....";clog << "Error message : " << str << endl;
}
九、数据结构
typedef struct Books
{char title[50];char author[50];char subject[100];int book_id;
}B;B Book1; //需要有typedef
十、类 & 对象
类是 C++ 的核心特性,通常被称为用户定义的类型。
类访问修饰符
public、private、protected
**protected(受保护)**成员变量或函数与私有成员十分相似,但有一点不同,protected(受保护)成员在派生类(即子类)中是可访问的。
#include <iostream>
using namespace std;class Box
{protected:double width;
};class SmallBox:Box // SmallBox 是派生类
{public:void setSmallWidth( double wid );double getSmallWidth( void );
};// 子类的成员函数
double SmallBox::getSmallWidth(void)
{return width ;
}void SmallBox::setSmallWidth( double wid )
{width = wid;
}// 程序的主函数
int main( )
{SmallBox box;// 使用成员函数设置宽度box.setSmallWidth(5.0);cout << "Width of box : "<< box.getSmallWidth() << endl;return 0;
}
有public, protected, private三种继承方式,它们相应地改变了基类成员的访问属性。
- **public 继承:**基类 public 成员,protected 成员,private 成员的访问属性在派生类中分别变成:public, protected, private
- **protected 继承:**基类 public 成员,protected 成员,private 成员的访问属性在派生类中分别变成:protected, protected, private
- **private 继承:**基类 public 成员,protected 成员,private 成员的访问属性在派生类中分别变成:private, private, private
类外不能访问protected成员、private成员
#include<iostream>
#include<assert.h>
using namespace std;
class A{public:int a;A(){a1 = 1;a2 = 2;a3 = 3;a = 4;}void fun(){cout << a << endl; //正确cout << a1 << endl; //正确cout << a2 << endl; //正确cout << a3 << endl; //正确}
public:int a1;
protected:int a2;
private:int a3;
};
class B : private A{public:int a;B(int i){A();a = i;}void fun(){cout << a << endl; //正确,public成员。cout << a1 << endl; //正确,基类public成员,在派生类中变成了private,可以被派生类访问。cout << a2 << endl; //正确,基类的protected成员,在派生类中变成了private,可以被派生类访问。cout << a3 << endl; //错误,基类的private成员不能被派生类访问。}
};
int main(){B b(10);cout << b.a << endl; //正确。public成员cout << b.a1 << endl; //错误,private成员不能在类外访问。cout << b.a2 << endl; //错误, private成员不能在类外访问。cout << b.a3 << endl; //错误,private成员不能在类外访问。system("pause");return 0;
}
类构造函数 & 析构函数
类的构造函数是类的一种特殊的成员函数,它会在每次创建类的新对象时执行。
拷贝构造函数
拷贝构造函数通常用于:
- 通过使用另一个同类型的对象来初始化新创建的对象。
- 复制对象把它作为参数传递给函数。
- 复制对象,并从函数返回这个对象。
#include <iostream>using namespace std;class Line
{public:int getLength( void );Line( int len ); // 简单的构造函数Line( const Line &obj); // 拷贝构造函数~Line(); // 析构函数private:int *ptr;
};// 成员函数定义,包括构造函数
Line::Line(int len)
{cout << "调用构造函数" << endl;// 为指针分配内存ptr = new int;*ptr = len;
}Line::Line(const Line &obj)
{cout << "调用拷贝构造函数并为指针 ptr 分配内存" << endl;ptr = new int;*ptr = *obj.ptr; // 拷贝值
}Line::~Line(void)
{cout << "释放内存" << endl;delete ptr;
}
int Line::getLength( void )
{return *ptr;
}void display(Line obj)
{cout << "line 大小 : " << obj.getLength() <<endl;
}// 程序的主函数
int main( )
{Line line1(10);Line line2 = line1; // 这里也调用了拷贝构造函数display(line1);display(line2);return 0;
}
调用构造函数
调用拷贝构造函数并为指针 ptr 分配内存
调用拷贝构造函数并为指针 ptr 分配内存
line 大小 : 10
释放内存
调用拷贝构造函数并为指针 ptr 分配内存
line 大小 : 10
释放内存
释放内存
释放内存
友元函数
类的友元函数是定义在类外部,但有权访问类的所有私有(private)成员和保护(protected)成员。尽管友元函数的原型有在类的定义中出现过,但是友元函数并不是成员函数。
友元可以是一个函数,该函数被称为友元函数;友元也可以是一个类,该类被称为友元类,在这种情况下,整个类及其所有成员都是友元。
#include <iostream>using namespace std;class Box
{double width;
public:friend void printWidth( Box box );void setWidth( double wid );
};// 成员函数定义
void Box::setWidth( double wid )
{width = wid;
}// 请注意:printWidth() 不是任何类的成员函数
void printWidth( Box box )
{/* 因为 printWidth() 是 Box 的友元,它可以直接访问该类的任何成员 */cout << "Width of box : " << box.width <<endl;
}// 程序的主函数
int main( )
{Box box;// 使用成员函数设置宽度box.setWidth(10.0);// 使用友元函数输出宽度printWidth( box );return 0;
}
内联函数
引入内联函数的目的是为了解决程序中函数调用的效率问题。
程序在编译器编译的时候,编译器将程序中出现的内联函数的调用表达式用内联函数的函数体进行替换,而对于其他的函数,都是在运行时候才被替代。
this 指针
在 C++ 中,每一个对象都能通过 this 指针来访问自己的地址。this 指针是所有成员函数的隐含参数。因此,在成员函数内部,它可以用来指向调用对象。
友元函数没有 this 指针,因为友元不是类的成员。只有成员函数才有 this 指针。
return this->Volume() > box.Volume();
类的静态成员
我们可以使用 static 关键字来把类成员定义为静态的。当我们声明类的成员为静态时,这意味着无论创建多少个类的对象,静态成员都只有一个副本。
静态成员函数只能访问静态成员数据、其他静态成员函数和类外部的其他函数。
静态成员函数有一个类范围,他们不能访问类的 this 指针。您可以使用静态成员函数来判断类的某些对象是否已被创建。
十一、继承
继承允许我们依据另一个类来定义一个类,这使得创建和维护一个应用程序变得更容易。
当创建一个类时,您不需要重新编写新的数据成员和成员函数,只需指定新建的类继承了一个已有的类的成员即可。这个已有的类称为基类,新建的类称为派生类。
访问 | public | protected | private |
---|---|---|---|
同一个类 | yes | yes | yes |
派生类 | yes | yes | no |
外部的类 | yes | no | no |
通常使用 public 继承。
- 公有继承(public):当一个类派生自公有基类时,基类的公有成员也是派生类的公有成员,基类的保护成员也是派生类的保护成员,基类的私有成员不能直接被派生类访问,但是可以通过调用基类的公有和保护成员来访问。
- 保护继承(protected): 当一个类派生自保护基类时,基类的公有和保护成员将成为派生类的保护成员。
- 私有继承(private):当一个类派生自私有基类时,基类的公有和保护成员将成为派生类的私有成员。
多继承
#include <iostream>using namespace std;// 基类 Shape
class Shape
{public:void setWidth(int w){width = w;}void setHeight(int h){height = h;}protected:int width;int height;
};// 基类 PaintCost
class PaintCost
{public:int getCost(int area){return area * 70;}
};// 派生类
class Rectangle : public Shape, public PaintCost
{public:int getArea(){return (width * height);}
};int main(void)
{Rectangle Rect;int area;Rect.setWidth(5);Rect.setHeight(7);area = Rect.getArea();// 输出对象的面积cout << "Total area: " << Rect.getArea() << endl;// 输出总花费cout << "Total paint cost: $" << Rect.getCost(area) << endl;return 0;
}
十二、重载运算符和重载函数
函数重载
#include <iostream>
using namespace std;class printData
{public:void print(int i) {cout << "整数为: " << i << endl;}void print(double f) {cout << "浮点数为: " << f << endl;}void print(char c[]) {cout << "字符串为: " << c << endl;}
};int main(void)
{printData pd;// 输出整数pd.print(5);// 输出浮点数pd.print(500.263);// 输出字符串char c[] = "Hello C++";pd.print(c);return 0;
}
运算符重载
#include <iostream>
using namespace std;class Box
{public:double getVolume(void){return length * breadth * height;}void setLength( double len ){length = len;}void setBreadth( double bre ){breadth = bre;}void setHeight( double hei ){height = hei;}// 重载 + 运算符,用于把两个 Box 对象相加Box operator+(const Box& b){Box box;box.length = this->length + b.length;box.breadth = this->breadth + b.breadth;box.height = this->height + b.height;return box;}private:double length; // 长度double breadth; // 宽度double height; // 高度
};
// 程序的主函数
int main( )
{Box Box1; // 声明 Box1,类型为 BoxBox Box2; // 声明 Box2,类型为 BoxBox Box3; // 声明 Box3,类型为 Boxdouble volume = 0.0; // 把体积存储在该变量中// Box1 详述Box1.setLength(6.0); Box1.setBreadth(7.0); Box1.setHeight(5.0);// Box2 详述Box2.setLength(12.0); Box2.setBreadth(13.0); Box2.setHeight(10.0);// Box1 的体积volume = Box1.getVolume();cout << "Volume of Box1 : " << volume <<endl;// Box2 的体积volume = Box2.getVolume();cout << "Volume of Box2 : " << volume <<endl;// 把两个对象相加,得到 Box3Box3 = Box1 + Box2;// Box3 的体积volume = Box3.getVolume();cout << "Volume of Box3 : " << volume <<endl;return 0;
}
下面是不可重载的运算符列表:
- .:成员访问运算符
- .*, ->*:成员指针访问运算符
- :::域运算符
- sizeof:长度运算符
- ?::条件运算符
- #: 预处理符号
重载输入输出
#include <iostream>
using namespace std;class Distance
{private:int feet; // 0 到无穷int inches; // 0 到 12public:// 所需的构造函数Distance(){feet = 0;inches = 0;}Distance(int f, int i){feet = f;inches = i;}friend ostream &operator<<( ostream &output, const Distance &D ){ output << "F : " << D.feet << " I : " << D.inches;return output; }friend istream &operator>>( istream &input, Distance &D ){ input >> D.feet >> D.inches;return input; }
};
int main()
{Distance D1(11, 10), D2(5, 11), D3;cout << "Enter the value of object : " << endl;cin >> D3;cout << "First Distance : " << D1 << endl;cout << "Second Distance :" << D2 << endl;cout << "Third Distance :" << D3 << endl;return 0;
}
Enter the value of object :
70
10
First Distance : F : 11 I : 10
Second Distance :F : 5 I : 11
Third Distance :F : 70 I : 10
重载递增递减
#include <iostream>
using namespace std;class Time
{private:int hours; // 0 到 23int minutes; // 0 到 59public:// 所需的构造函数Time(){hours = 0;minutes = 0;}Time(int h, int m){hours = h;minutes = m;}// 显示时间的方法void displayTime(){cout << "H: " << hours << " M:" << minutes <<endl;}// 重载前缀递增运算符( ++ )Time operator++ () {++minutes; // 对象加 1if(minutes >= 60) {++hours;minutes -= 60;}return Time(hours, minutes);}// 重载后缀递增运算符( ++ )Time operator++( int ) {// 保存原始值Time T(hours, minutes);// 对象加 1++minutes; if(minutes >= 60){++hours;minutes -= 60;}// 返回旧的原始值return T; }
};
int main()
{Time T1(11, 59), T2(10,40);++T1; // T1 加 1T1.displayTime(); // 显示 T1++T1; // T1 再加 1T1.displayTime(); // 显示 T1T2++; // T2 加 1T2.displayTime(); // 显示 T2T2++; // T2 再加 1T2.displayTime(); // 显示 T2return 0;
}
重载赋值
void operator=(const Distance &D )
{ feet = D.feet;inches = D.inches;
}
重载运算符()
#include <iostream>
using namespace std;class Distance
{private:int feet; // 0 到无穷int inches; // 0 到 12public:// 所需的构造函数Distance(){feet = 0;inches = 0;}Distance(int f, int i){feet = f;inches = i;}// 重载函数调用运算符Distance operator()(int a, int b, int c){Distance D;// 进行随机计算D.feet = a + c + 10;D.inches = b + c + 100 ;return D;}// 显示距离的方法void displayDistance(){cout << "F: " << feet << " I:" << inches << endl;}};
int main()
{Distance D1(11, 10), D2;cout << "First Distance : "; D1.displayDistance();D2 = D1(10, 10, 10); // invoke operator()cout << "Second Distance :"; D2.displayDistance();return 0;
}
下标运算符 []
#include <iostream>
using namespace std;
const int SIZE = 10;class safearay
{private:int arr[SIZE];public:safearay() {register int i;for(i = 0; i < SIZE; i++){arr[i] = i;}}int& operator[](int i){if( i >= SIZE ){cout << "索引超过最大值" <<endl; // 返回第一个元素return arr[0];}return arr[i];}
};
int main()
{safearay A;cout << "A[2] 的值为 : " << A[2] <<endl;cout << "A[5] 的值为 : " << A[5]<<endl;cout << "A[12] 的值为 : " << A[12]<<endl;return 0;
}
类成员访问运算符 -> 重载
#include <iostream>
#include <vector>
using namespace std;// 假设一个实际的类
class Obj {static int i, j;
public:void f() const { cout << i++ << endl; }void g() const { cout << j++ << endl; }
};// 静态成员定义
int Obj::i = 10;
int Obj::j = 12;// 为上面的类实现一个容器
class ObjContainer {vector<Obj*> a;
public:void add(Obj* obj){ a.push_back(obj); // 调用向量的标准方法}friend class SmartPointer;
};// 实现智能指针,用于访问类 Obj 的成员
class SmartPointer {ObjContainer oc;int index;
public:SmartPointer(ObjContainer& objc){ oc = objc;index = 0;}// 返回值表示列表结束bool operator++() // 前缀版本{ if(index >= oc.a.size() - 1) return false;if(oc.a[++index] == 0) return false;return true;}bool operator++(int) // 后缀版本{ return operator++();}// 重载运算符 ->Obj* operator->() const {if(!oc.a[index]){cout << "Zero value";return (Obj*)0;}return oc.a[index];}
};int main() {const int sz = 10;Obj o[sz];ObjContainer oc;for(int i = 0; i < sz; i++){oc.add(&o[i]);}SmartPointer sp(oc); // 创建一个迭代器do {sp->f(); // 智能指针调用sp->g();} while(sp++);return 0;
}
十三、多态
多态按字面的意思就是多种形态。当类之间存在层次结构,并且类之间是通过继承关联时,就会用到多态。
C++ 多态意味着调用成员函数时,会根据调用函数的对象的类型来执行不同的函数。
虚函数
#include <iostream>
using namespace std;class Shape
{protected:int width, height;public:Shape(int a = 0, int b = 0){width = a;height = b;}virtual int area(){cout << "Parent class area :" << endl;return 0;}
};
class Rectangle : public Shape
{public:Rectangle(int a = 0, int b = 0) : Shape(a, b) {}int area(){cout << "Rectangle class area :" << endl;return (width * height);}
};
class Triangle : public Shape
{public:Triangle(int a = 0, int b = 0) : Shape(a, b) {}int area(){cout << "Triangle class area :" << endl;return (width * height / 2);}
};
// 程序的主函数
int main()
{Shape *shape;Rectangle rec(10, 7);Triangle tri(10, 5);// 存储矩形的地址shape = &rec;// 调用矩形的求面积函数 areashape->area();// 存储三角形的地址shape = &tri;// 调用三角形的求面积函数 areashape->area();return 0;
}
十四、文件和流
void open(const char *filename, ios::openmode mode);
模式标志 | 描述 |
---|---|
ios::app | 追加模式。所有写入都追加到文件末尾。 |
ios::ate | 文件打开后定位到文件末尾。 |
ios::in | 打开文件用于读取。 |
ios::out | 打开文件用于写入。 |
ios::trunc | 如果该文件已经存在,其内容将在打开文件之前被截断,即把文件长度设为 0。 |
#include <fstream>
#include <iostream>
using namespace std;int main ()
{char data[100];// 以写模式打开文件ofstream outfile;outfile.open("test.txt");cout << "Writing to the file" << endl;cout << "Enter your name: "; cin.getline(data, 100);// 向文件写入用户输入的数据outfile << data << endl;cout << "Enter your age: "; cin >> data;cin.ignore();// 再次向文件写入用户输入的数据outfile << data << endl;// 关闭打开的文件outfile.close();// 以读模式打开文件ifstream infile; infile.open("test.txt"); cout << "Reading from the file" << endl; infile >> data; // 在屏幕上写入数据cout << data << endl;// 再次从文件读取数据,并显示它infile >> data; cout << data << endl; // 关闭打开的文件infile.close();return 0;
}
文件位置指针
// 定位到 fileObject 的第 n 个字节(假设是 ios::beg)
fileObject.seekg( n );// 把文件的读指针从 fileObject 当前位置向后移 n 个字节
fileObject.seekg( n, ios::cur );// 把文件的读指针从 fileObject 末尾往回移 n 个字节
fileObject.seekg( n, ios::end );// 定位到 fileObject 的末尾
fileObject.seekg( 0, ios::end );
十五、异常处理
异常提供了一种转移程序控制权的方式。C++ 异常处理涉及到三个关键字:try、catch、throw。
- throw: 当问题出现时,程序会抛出一个异常。这是通过使用 throw 关键字来完成的。
- catch: 在您想要处理问题的地方,通过异常处理程序捕获异常。catch 关键字用于捕获异常。
- try: try 块中的代码标识将被激活的特定异常。它后面通常跟着一个或多个 catch 块。
try
{// 保护代码
}catch( ExceptionName e1 )
{// catch 块
}catch( ExceptionName e2 )
{// catch 块
}catch( ExceptionName eN )
{// catch 块
}
#include <iostream>
using namespace std;double division(int a, int b)
{if (b == 0) {throw "zero";}return (1.0 * a / b);
}int main ()
{int a = 50, b = 7;double c = 0;try {c = division(a, b);cout << c << endl;} catch (const char* msg) {cerr << msg << endl;}return 0;
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-q9gMtpNK-1669206512210)(https://www.runoob.com/wp-content/uploads/2015/05/exceptions_in_cpp.png)]
异常 | 描述 |
---|---|
std::exception | 该异常是所有标准 C++ 异常的父类。 |
std::bad_alloc | 该异常可以通过 new 抛出。 |
std::bad_cast | 该异常可以通过 dynamic_cast 抛出。 |
std::bad_typeid | 该异常可以通过 typeid 抛出。 |
std::bad_exception | 这在处理 C++ 程序中无法预期的异常时非常有用。 |
std::logic_error | 理论上可以通过读取代码来检测到的异常。 |
std::domain_error | 当使用了一个无效的数学域时,会抛出该异常。 |
std::invalid_argument | 当使用了无效的参数时,会抛出该异常。 |
std::length_error | 当创建了太长的 std::string 时,会抛出该异常。 |
std::out_of_range | 该异常可以通过方法抛出,例如 std::vector 和 std::bitset<>::operator。 |
std::runtime_error | 理论上不可以通过读取代码来检测到的异常。 |
std::overflow_error | 当发生数学上溢时,会抛出该异常。 |
std::range_error | 当尝试存储超出范围的值时,会抛出该异常。 |
std::underflow_error | 当发生数学下溢时,会抛出该异常。 |
#include <iostream>
#include <exception>
using namespace std;struct MyException : public exception
{const char *what() const throw(){return "C++ Exception";}
};int main()
{try{throw MyException();}catch (MyException &e){std::cout << "MyException caught" << std::endl;std::cout << e.what() << std::endl;}catch (std::exception &e){//其他的错误}
}
十六、动态内存
new 与 malloc() 函数相比,其主要的优点是,new 不只是分配了内存,它还创建了对象。
二维数组
#include <iostream>
using namespace std;int main()
{int **p;int i,j; //p[4][8] //开始分配4行8列的二维数据 p = new int *[4];for (int i = 0; i < 4; i ++) {p[i] = new int [8];}for(i=0; i<4; i++){for(j=0; j<8; j++){p[i][j] = i*8+j;}} //打印数据 for(i=0; i<4; i++){for(j=0; j<8; j++) { cout<<p[i][j]<<"\t"; if(j==7) cout<<endl; }} //开始释放申请的堆 for(i=0; i<4; i++){delete [] p[i]; }delete [] p; return 0;
}
三维数组
int ***p;
p = new int **[2];
for (int i = 0; i < 2; i ++) {p[i] = new int *[3];for (int j = 0; j < 3; j ++) {p[i][j] = new int [4];}
}
十七、命名空间
#include <iostream>
using namespace std;// 第一个命名空间
namespace first_space{void func(){cout << "Inside first_space" << endl;}
}
// 第二个命名空间
namespace second_space{void func(){cout << "Inside second_space" << endl;}
}
int main ()
{// 调用第一个命名空间中的函数first_space::func();// 调用第二个命名空间中的函数second_space::func(); return 0;
}
using 。这个指令会告诉编译器,后续的代码将使用指定的命名空间中的名称。
#include <iostream>
using namespace std;// 第一个命名空间
namespace first_space{void func(){cout << "Inside first_space" << endl;}
}
// 第二个命名空间
namespace second_space{void func(){cout << "Inside second_space" << endl;}
}
using namespace first_space;
int main ()
{// 调用第一个命名空间中的函数func();return 0;
}
using 指令也可以用来指定命名空间中的特定项目。
#include <iostream>
using std::cout;int main ()
{cout << "std::endl is used with std!" << std::endl;return 0;
}
嵌套命名空间
#include <iostream>
using namespace std;// 第一个命名空间
namespace first_space{void func(){cout << "Inside first_space" << endl;}// 第二个命名空间namespace second_space{void func(){cout << "Inside second_space" << endl;}}
}
// using namespace first_space::second_space;
int main ()
{// 调用第二个命名空间中的函数first_space::second_space::func();return 0;
}
十八、模板
函数模板定义
template <typename type> ret-type func-name(parameter list)
{// 函数的主体
}
类模板
#include <iostream>
#include <string>using namespace std;template <typename T>
inline T const& Max (T const& a, T const& b)
{ return a < b ? b:a;
}
int main ()
{int i = 39;int j = 20;cout << "Max(i, j): " << Max(i, j) << endl; double f1 = 13.5; double f2 = 20.7; cout << "Max(f1, f2): " << Max(f1, f2) << endl; string s1 = "Hello"; string s2 = "World"; cout << "Max(s1, s2): " << Max(s1, s2) << endl; return 0;
}
#include <iostream>
#include <vector>
#include <cstdlib>
#include <string>
#include <stdexcept>using namespace std;template <class T>
class Stack { private: vector<T> elems; // 元素 public: void push(T const&); // 入栈void pop(); // 出栈T top() const; // 返回栈顶元素bool empty() const{ // 如果为空则返回真。return elems.empty(); }
}; template <class T>
void Stack<T>::push (T const& elem)
{ // 追加传入元素的副本elems.push_back(elem);
} template <class T>
void Stack<T>::pop ()
{ if (elems.empty()) { throw out_of_range("Stack<>::pop(): empty stack"); }// 删除最后一个元素elems.pop_back();
} template <class T>
T Stack<T>::top () const
{ if (elems.empty()) { throw out_of_range("Stack<>::top(): empty stack"); }// 返回最后一个元素的副本 return elems.back();
} int main()
{ try { Stack<int> intStack; // int 类型的栈 Stack<string> stringStack; // string 类型的栈 // 操作 int 类型的栈 intStack.push(7); cout << intStack.top() <<endl; // 操作 string 类型的栈 stringStack.push("hello"); cout << stringStack.top() << std::endl; stringStack.pop(); stringStack.pop(); } catch (exception const& ex) { cerr << "Exception: " << ex.what() <<endl; return -1;}
}
十九、预处理器
#define 预处理指令用于创建符号常量。该符号常量通常称为宏,指令的一般形式是:
#define macro-name replacement-text
参数宏
#include <iostream>
using namespace std;#define MIN(a,b) (a<b ? a : b)int main ()
{int i, j;i = 100;j = 30;cout <<"较小的值为:" << MIN(i, j) << endl;return 0;
}
条件编译
#include <iostream>
using namespace std;
#define DEBUG#define MIN(a,b) (((a)<(b)) ? a : b)int main ()
{int i, j;i = 100;j = 30;
#ifdef DEBUGcerr <<"Trace: Inside main function" << endl;
#endif#if 0/* 这是注释部分 */cout << MKSTR(HELLO C++) << endl;
#endifcout <<"The minimum is " << MIN(i, j) << endl;#ifdef DEBUGcerr <<"Trace: Coming out of main function" << endl;
#endifreturn 0;
}
# 运算符会把 replacement-text 令牌转换为用引号引起来的字符串
#include <iostream>
using namespace std;#define MKSTR( x ) #xint main ()
{cout << MKSTR(HELLO C++) << endl;return 0;
}
## 运算符用于连接两个令牌。下面是一个实例:
#include <iostream>
using namespace std;#define concat(a, b) a ## b
int main()
{int xy = 100;cout << concat(x, y) << endl;return 0;
}
C++ 中的预定义宏
宏 | 描述 |
---|---|
LINE | 这会在程序编译时包含当前行号。 |
FILE | 这会在程序编译时包含当前文件名。 |
DATE | 这会包含一个形式为 month/day/year 的字符串,它表示把源文件转换为目标代码的日期。 |
TIME | 这会包含一个形式为 hour:minute:second 的字符串,它表示程序被编译的时间。 |
#include <iostream>
using namespace std;int main ()
{cout << "Value of __LINE__ : " << __LINE__ << endl;cout << "Value of __FILE__ : " << __FILE__ << endl;cout << "Value of __DATE__ : " << __DATE__ << endl;cout << "Value of __TIME__ : " << __TIME__ << endl;return 0;
}
二十、信号处理
有些信号不能被程序捕获,但是下表所列信号可以在程序中捕获,并可以基于信号采取适当的动作。这些信号是定义在 C++ 头文件 <csignal>
中。
信号 | 描述 |
---|---|
SIGABRT | 程序的异常终止,如调用 abort。 |
SIGFPE | 错误的算术运算,比如除以零或导致溢出的操作。 |
SIGILL | 检测非法指令。 |
SIGINT | 程序终止(interrupt)信号。 |
SIGSEGV | 非法访问内存。 |
SIGTERM | 发送到程序的终止请求。 |
不管您想在程序中捕获什么信号,您都必须使用 signal 函数来注册信号,并将其与信号处理程序相关联。
void (*signal (int sig, void (*func)(int)))(int);
这个函数接收两个参数:第一个参数是一个整数,代表了信号的编号;第二个参数是一个指向信号处理函数的指针。
#include <iostream>
#include <csignal>
#include <unistd.h>using namespace std;void signalHandler( int signum )
{cout << "Interrupt signal (" << signum << ") received.\n";// 清理并关闭// 终止程序 exit(signum); }int main ()
{// 注册信号 SIGINT 和信号处理程序signal(SIGINT, signalHandler); while(1){cout << "Going to sleep...." << endl;sleep(1);}return 0;
}
二十一、多线程
多线程是多任务处理的一种特殊形式,多任务处理允许让电脑同时运行两个或两个以上的程序。一般情况下,两种类型的多任务处理:基于进程和基于线程。
- 基于进程的多任务处理是程序的并发执行。
- 基于线程的多任务处理是同一程序的片段的并发执行。
创建线程
#include <pthread.h>
pthread_create (thread, attr, start_routine, arg)
参数 | 描述 |
---|---|
thread | 指向线程标识符指针。 |
attr | 一个不透明的属性对象,可以被用来设置线程属性。您可以指定线程属性对象,也可以使用默认值 NULL。 |
start_routine | 线程运行函数起始地址,一旦线程被创建就会执行。 |
arg | 运行函数的参数。它必须通过把引用作为指针强制转换为 void 类型进行传递。如果没有传递参数,则使用 NULL。 |
终止线程
#include <pthread.h>
pthread_exit (status)
实例
#include <iostream>
// 必须的头文件
#include <pthread.h>using namespace std;#define NUM_THREADS 5// 线程的运行函数
void* say_hello(void* args)
{cout << "Hello Runoob!" << endl;return 0;
}int main()
{// 定义线程的 id 变量,多个变量使用数组pthread_t tids[NUM_THREADS];for(int i = 0; i < NUM_THREADS; ++i){//参数依次是:创建的线程id,线程参数,调用的函数,传入的函数参数int ret = pthread_create(&tids[i], NULL, say_hello, NULL);if (ret != 0){cout << "pthread_create error: error_code=" << ret << endl;}}//等各个线程退出后,进程才结束,否则进程强制结束了,线程可能还没反应过来;pthread_exit(NULL);
}
g++ t.cpp -o t -lpthread
//文件名:test.cpp#include <iostream>
#include <cstdlib>
#include <pthread.h>using namespace std;#define NUM_THREADS 5void *PrintHello(void *threadid)
{ // 对传入的参数进行强制类型转换,由无类型指针变为整形数指针,然后再读取int tid = *((int*)threadid);cout << "Hello Runoob! 线程 ID, " << tid << endl;pthread_exit(NULL);
}int main ()
{pthread_t threads[NUM_THREADS];int indexes[NUM_THREADS];// 用数组来保存i的值int rc;int i;for( i=0; i < NUM_THREADS; i++ ){ cout << "main() : 创建线程, " << i << endl;indexes[i] = i; //先保存i的值// 传入的时候必须强制转换为void* 类型,即无类型指针 rc = pthread_create(&threads[i], NULL, PrintHello, (void *)&(indexes[i]));if (rc){cout << "Error:无法创建线程," << rc << endl;exit(-1);}}pthread_exit(NULL);
}
向线程传递参数
这个实例演示了如何通过结构传递多个参数。您可以在线程回调中传递任意的数据类型,因为它指向 void,如下面的实例所示:
#include <iostream>
#include <cstdlib>
#include <pthread.h>using namespace std;#define NUM_THREADS 5struct thread_data{int thread_id;char *message;
};void *PrintHello(void *threadarg)
{struct thread_data *my_data;my_data = (struct thread_data *) threadarg;cout << "Thread ID : " << my_data->thread_id ;cout << " Message : " << my_data->message << endl;pthread_exit(NULL);
}int main ()
{pthread_t threads[NUM_THREADS];struct thread_data td[NUM_THREADS];int rc;int i;for( i=0; i < NUM_THREADS; i++ ){cout <<"main() : creating thread, " << i << endl;td[i].thread_id = i;td[i].message = (char*)"This is message";rc = pthread_create(&threads[i], NULL,PrintHello, (void *)&td[i]);if (rc){cout << "Error:unable to create thread," << rc << endl;exit(-1);}}pthread_exit(NULL);
}
连接和分离线程
pthread_join (threadid, status)
pthread_detach (threadid)
pthread_join() 子程序阻碍调用程序,直到指定的 threadid 线程终止为止。当创建一个线程时,它的某个属性会定义它是否是可连接的(joinable)或可分离的(detached)。只有创建时定义为可连接的线程才可以被连接。如果线程创建时被定义为可分离的,则它永远也不能被连接。
这个实例演示了如何使用 pthread_join() 函数来等待线程的完成。
#include <iostream>
#include <cstdlib>
#include <pthread.h>
#include <unistd.h>using namespace std;#define NUM_THREADS 5void *wait(void *t)
{int i;long tid;tid = (long)t;sleep(1);cout << "Sleeping in thread " << endl;cout << "Thread with id : " << tid << " ...exiting " << endl;pthread_exit(NULL);
}int main ()
{int rc;int i;pthread_t threads[NUM_THREADS];pthread_attr_t attr;void *status;// 初始化并设置线程为可连接的(joinable)pthread_attr_init(&attr);pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);for( i=0; i < NUM_THREADS; i++ ){cout << "main() : creating thread, " << i << endl;rc = pthread_create(&threads[i], NULL, wait, (void *)&i );if (rc){cout << "Error:unable to create thread," << rc << endl;exit(-1);}}// 删除属性,并等待其他线程pthread_attr_destroy(&attr);for( i=0; i < NUM_THREADS; i++ ){rc = pthread_join(threads[i], &status);if (rc){cout << "Error:unable to join," << rc << endl;exit(-1);}cout << "Main: completed thread id :" << i ;cout << " exiting with status :" << status << endl;}cout << "Main: program exiting." << endl;pthread_exit(NULL);
}
C++ 11 之后添加了新的标准线程库 std::thread
// 演示多线程的CPP程序
// 使用三个不同的可调用对象
#include <iostream>
#include <thread>
using namespace std;// 一个虚拟函数
void foo(int Z)
{for (int i = 0; i < Z; i++) {cout << "线程使用函数指针作为可调用参数\n";}
}// 可调用对象
class thread_obj {public:void operator()(int x){for (int i = 0; i < x; i++)cout << "线程使用函数对象作为可调用参数\n";}
};int main()
{cout << "线程 1 、2 、3 ""独立运行" << endl;// 函数指针thread th1(foo, 3);// 函数对象thread th2(thread_obj(), 3);// 定义 Lambda 表达式auto f = [](int x) {for (int i = 0; i < x; i++)cout << "线程使用 lambda 表达式作为可调用参数\n";};// 线程通过使用 lambda 表达式作为可调用的参数thread th3(f, 3);// 等待线程完成// 等待线程 t1 完成th1.join();// 等待线程 t2 完成th2.join();// 等待线程 t3 完成th3.join();return 0;
}
菜鸟教程Cpp学习笔记相关推荐
- python scrapy菜鸟教程_scrapy学习笔记(一)快速入门
安装Scrapy Scrapy是一个高级的Python爬虫框架,它不仅包含了爬虫的特性,还可以方便的将爬虫数据保存到csv.json等文件中. 首先我们安装Scrapy. pip install sc ...
- 菜鸟教程-css3学习笔记
学习链接是: https://www.runoob.com/css3/css3-tutorial.html ############################################## ...
- 菜鸟教程-css学习笔记
CSS实例中有很多CSS的例子,可以学习. <!DOCTYPE html> <html> <head> <meta charset="utf-8&q ...
- Java菜鸟教程系列 学习笔记总结 基础篇(1)
基础语法 本博客通过学习菜鸟教程Java专栏,并整理得出的Java基础知识. 命名规范 1.项目名全部小写 2.包名全部小写 3.类名首字母大写,如果类名由多个单词组成,每个单词的首字母都要大写.如: ...
- 菜鸟教程-Javascript学习笔记-JS函数之前
教程连接是: https://www.runoob.com/js/js-tutorial.html DOM(一些操作页面元素的方法) BOM(一些操作浏览器的方法) ################# ...
- 菜鸟教程PHP学习笔记(不定期更新)
NO.1-PHP表单验证 代码详细注释版 <!DOCTYPE HTML> <html> <head> <meta charset="utf-8&qu ...
- Java菜鸟教程系列 学习笔记总结 面向对象篇(1)
继承 继承就是子类继承父类的特征和行为,使得子类对象(实例)具有父类的实例域和方法,或子类从父类继承方法,使得子类具有父类相同的行为. 继承的特性 子类拥有父类非 private 的属性.方法. 子类 ...
- 《C++大学教程》学习笔记(九)
<C++大学教程>学习笔记(九) 1.Time类实例研究 1.1包含防护 在开始之前,先说明一个重要的C++软件工程概念:在头文件中使用"包含防护",从而避免头文件中的 ...
- python编程16章教程_Python学习笔记__16.2章 TCP编程
# 这是学习廖雪峰老师python教程的学习笔记 Socket是网络编程的一个抽象概念.通常我们用一个Socket表示"打开了一个网络链接",而打开一个Socket需要知道目标计算 ...
最新文章
- python快速安装pip
- 是时候理解下HTTPS及背后的加密原理了
- guava集合操作类的使用_使用Guava进行测试集合的实现
- python读取扫描形成的pdf_Python利用PyPDF2库获取PDF文件总页码实例
- 面向对象设计7大原则
- 2个recordset合并_史上最全!8种办法玩转Excel文字合并,总有一款适合你!
- selenium自动化测试-1.selenium介绍及环境安装
- CentOS7 yum方式安装MySQL5.7
- io_uring 新异步 IO 机制,性能提升超 150%,堪比 SPDK
- 中移苏研大规模资源高可用研究及实践
- 向上累积频数怎么算_excel数据分析向上累计和向下累计怎么做呢
- python function terminated_评论第2页 – Calibre 转换 TXT 文件出现 Errno 21 错误的解决方法 – 书伴...
- 华为服务器sn码查询网站,linux 查询服务器sn号
- Ant Design of Vue去掉加载遮罩
- Go语言学习之路(二)
- 在 uni-app 中 使用字体图标
- linux换屏快捷键,Linux系统快捷键最全合集
- OpenPCDet 训练自己的数据集详细教程!
- 不在B中的A的子串数量 HDU - 4416 (后缀自动机模板题目)
- C 语言的控制台输出只是 “黑底白字”吗 ?
热门文章
- 性能测试方法详解(验收性能、负载、压力、配置、并发、可靠性、失败恢复)
- 计算机网络应用基础实验指导,计算机及网络应用基础实验指导书.PDF
- 熊孩子乱敲键盘攻破linux桌面,“熊孩子”乱敲键盘就攻破了Linux桌面,大神:17年前我就警告过你们...
- HTML5期末大作业:管理系统网站设计——蓝色OA企业员工管理系统(10页) HTML+CSS+JavaScript 学生DW网页设计作业成品 web课程设计网页规划与设计 计算机毕设网页设计源
- android 添加日程失败,Android向系统日历中添加日程事件
- SkyWalking--使用/教程/实例
- 专题分纲目录 操作系统之哲学原理
- 计算机的心智:操作系统之哲学原理
- CRFPP 语言处理
- 2D游戏地图编辑器的制作(一)