目录
1、构造函数和析构函数
构造函数在对象被创建的时候同时被调用,而析构函数在对象被销毁的时候调用
#include<iostream>
using namespace std;
typedef class Box {
public:
double length;
double setWeight(double wid) {
return wid;
}
// Box(); //constructorFunction,构造函数需和类同名,在对象创建时执行,类似于__init__函数
Box(double len); // 带参数的构造函数
~Box(); // 析构函数的创建,不能带参数,在每次删除对象的时候执行
} Box;
//Box::Box() {
// cout << "This is the init func." << endl;
//}
//Box::Box(double len) {
// cout << "This is the init func." << endl;
// cout << "The init len is " << len << endl;
//}
Box::Box(double len) : length(len) { // 使用初始化列表进行初始化
cout << "the length is " << length << endl;
}
上面的函数等价于下面这个函数
//Box::Box(double len) {
// length = len;
// cout << "the length is " << length << endl;
//}
//Box::Box(double len,double hei) : length(len),height(hei) { // 使用初始化列表进行初始化
// cout << "the length is " << length << endl;
//}
Box::~Box() {
cout << "process exit" << endl;
}
int main() {
// Box littleBox(); // 不带参数的调用形式
// Box littleBox(10); // 带参数的调用形式
Box little(10);
return 0;
}
//output
the length is 10
process exit
2、拷贝构造函数
#include<iostream>
using namespace std;
//拷贝构造函数是使用同一类中所创建的对象来初始化新的对象
class Box {
public:
// 构造函数
Box(int len);
int getLen();
// 拷贝构造函数
Box(const Box &obj);
//析构函数
~Box();
private:
int *ptr;
};
// 构造函数
Box::Box(int len) {
cout << "This is box is " << len << endl;
ptr = new int;
*ptr = len;
}
//拷贝构造函数函数
Box::Box(const Box &obj) {
ptr = new int;
*ptr = *obj.ptr;
}
// 成员函数
int Box::getLen() {
return *ptr;
}
//析构函数
Box::~Box() {
cout << "Process exiting..." << endl;
}
void display(Box obj) {
cout << "The box length is " << obj.getLen() << endl;
}
int main() {
Box littleBox(10);
display(littleBox);
return 0;
}
// output
This is box is 10
The box length is 10
Process exiting...
Process exiting...
3、友元函数
(吐槽下这名字起的真烂,记住英文名friend function)
友元函数定义在类外部,但有权访问类的所有成员(私有和保护),而且其并不是成员函数
友元可以是一个函数,该函数被称为友元函数;友元也可以是一个类,该类被称为友元类,在这种情况下,整个类及其所有成员都是友元
#include<iostream>
using namespace std;
class Box {
public:
friend void printLen(Box box);
private:
double len = 10;
};
// 友元函数不需要如下的声明形式
//double Box::getLen(double len)
// 以下是友元函数
void printLen(Box box) {
cout << "The Box length is " << box.len << endl;
}
int main() {
Box littleBox;
// littleBox.len = 10;
printLen(littleBox);
return 0;
}
// output
The Box length is 10
4、内联函数(inline函数)
//inline函数通常和类一起使用,
//引入内联函数的目的是为了解决程序中函数调用的效率问题,程序在编译器编译的时候,编译器将程序中出现的内联函数的调用表达式用内联函数的函数体进行替换,而对于其他的函数,都是在运行时候才被替代。这其实就是个空间代价换时间的i节省。所以内联函数一般都是1-5行的小函数。
#include <iostream>
using namespace std;
此外注意下面的提示
//1.在内联函数内不允许使用循环语句和开关语句;
//2.内联函数的定义必须出现在内联函数第一次调用之前;
//3.类结构中所在的类说明内部定义的函数是内联函数
inline int Max(int x, int y) {
return (x > y) ? x : y;
}
// 程序的主函数
int main() {
cout << "Max (20,10): " << Max(20, 10) << endl;
cout << "Max (0,200): " << Max(0, 200) << endl;
cout << "Max (100,1010): " << Max(100, 1010) << endl;
return 0;
}
//output:
Max (20,10): 20
Max (0,200): 200
Max (100,1010): 1010
5、this、类指针、静态变量
#include <iostream>
using namespace std;
class Box {
public:
static int size; // 静态变量
Box(double l, double h, double w) {
cout << "process init..." << endl;
length = l;
heigth = h;
width = w;
}
double Volume(void) {
return length * heigth * width;
}
int compare(Box box) { // this指针,this表示自身对象
return this->Volume() > box.Volume();
}
private:
double length;
double heigth;
double width;
};
int Box::size = 1; // 初始化静态变量
int main() {
Box box1(1.1, 1.2, 1.3);
Box box2(2.1, 2.2, 2.3);
Box *ptr;
ptr = &box1;
cout << "pointer object' volume is " << ptr->Volume() << endl;
if (box1.compare(box2)) {
cout << "box1 is bigger than box2" << endl;
} else {
cout << "box1 is smaller than box2" << endl;
}
cout<<"the size of box1 is "<<box1.size<<endl;
return 0;
}
//output:
process init...
process init...
pointer object' volume is 1.716
box1 is smaller than box2
the size of box1 is 1