• C++学习日记 day003


    目录

    1、构造函数和析构函数

    2、拷贝构造函数

    3、友元函数

    4、内联函数(inline函数)

    5、this、类指针、静态变量


    1、构造函数和析构函数

    构造函数在对象被创建的时候同时被调用,而析构函数在对象被销毁的时候调用

    1. #include
    2. using namespace std;
    3. typedef class Box {
    4. public:
    5. double length;
    6. double setWeight(double wid) {
    7. return wid;
    8. }
    9. // Box(); //constructorFunction,构造函数需和类同名,在对象创建时执行,类似于__init__函数
    10. Box(double len); // 带参数的构造函数
    11. ~Box(); // 析构函数的创建,不能带参数,在每次删除对象的时候执行
    12. } Box;
    13. //Box::Box() {
    14. // cout << "This is the init func." << endl;
    15. //}
    16. //Box::Box(double len) {
    17. // cout << "This is the init func." << endl;
    18. // cout << "The init len is " << len << endl;
    19. //}
    20. Box::Box(double len) : length(len) { // 使用初始化列表进行初始化
    21. cout << "the length is " << length << endl;
    22. }
    23. 上面的函数等价于下面这个函数
    24. //Box::Box(double len) {
    25. // length = len;
    26. // cout << "the length is " << length << endl;
    27. //}
    28. //Box::Box(double len,double hei) : length(len),height(hei) { // 使用初始化列表进行初始化
    29. // cout << "the length is " << length << endl;
    30. //}
    31. Box::~Box() {
    32. cout << "process exit" << endl;
    33. }
    34. int main() {
    35. // Box littleBox(); // 不带参数的调用形式
    36. // Box littleBox(10); // 带参数的调用形式
    37. Box little(10);
    38. return 0;
    39. }
    40. //output
    41. the length is 10
    42. process exit

    2、拷贝构造函数

    1. #include
    2. using namespace std;
    3. //拷贝构造函数是使用同一类中所创建的对象来初始化新的对象
    4. class Box {
    5. public:
    6. // 构造函数
    7. Box(int len);
    8. int getLen();
    9. // 拷贝构造函数
    10. Box(const Box &obj);
    11. //析构函数
    12. ~Box();
    13. private:
    14. int *ptr;
    15. };
    16. // 构造函数
    17. Box::Box(int len) {
    18. cout << "This is box is " << len << endl;
    19. ptr = new int;
    20. *ptr = len;
    21. }
    22. //拷贝构造函数函数
    23. Box::Box(const Box &obj) {
    24. ptr = new int;
    25. *ptr = *obj.ptr;
    26. }
    27. // 成员函数
    28. int Box::getLen() {
    29. return *ptr;
    30. }
    31. //析构函数
    32. Box::~Box() {
    33. cout << "Process exiting..." << endl;
    34. }
    35. void display(Box obj) {
    36. cout << "The box length is " << obj.getLen() << endl;
    37. }
    38. int main() {
    39. Box littleBox(10);
    40. display(littleBox);
    41. return 0;
    42. }
    43. // output
    44. This is box is 10
    45. The box length is 10
    46. Process exiting...
    47. Process exiting...

    3、友元函数

    (吐槽下这名字起的真烂,记住英文名friend function)

    友元函数定义在类外部,但有权访问类的所有成员(私有和保护),而且其并不是成员函数
    友元可以是一个函数,该函数被称为友元函数;友元也可以是一个类,该类被称为友元类,在这种情况下,整个类及其所有成员都是友元

    1. #include
    2. using namespace std;
    3. class Box {
    4. public:
    5. friend void printLen(Box box);
    6. private:
    7. double len = 10;
    8. };
    9. // 友元函数不需要如下的声明形式
    10. //double Box::getLen(double len)
    11. // 以下是友元函数
    12. void printLen(Box box) {
    13. cout << "The Box length is " << box.len << endl;
    14. }
    15. int main() {
    16. Box littleBox;
    17. // littleBox.len = 10;
    18. printLen(littleBox);
    19. return 0;
    20. }
    21. // output
    22. The Box length is 10

    4、内联函数(inline函数)

    //inline函数通常和类一起使用,
    //引入内联函数的目的是为了解决程序中函数调用的效率问题,程序在编译器编译的时候,编译器将程序中出现的内联函数的调用表达式用内联函数的函数体进行替换,而对于其他的函数,都是在运行时候才被替代。这其实就是个空间代价换时间的i节省。所以内联函数一般都是1-5行的小函数。

    1. #include
    2. using namespace std;
    3. 此外注意下面的提示
    4. //1.在内联函数内不允许使用循环语句和开关语句;
    5. //2.内联函数的定义必须出现在内联函数第一次调用之前;
    6. //3.类结构中所在的类说明内部定义的函数是内联函数
    7. inline int Max(int x, int y) {
    8. return (x > y) ? x : y;
    9. }
    10. // 程序的主函数
    11. int main() {
    12. cout << "Max (20,10): " << Max(20, 10) << endl;
    13. cout << "Max (0,200): " << Max(0, 200) << endl;
    14. cout << "Max (100,1010): " << Max(100, 1010) << endl;
    15. return 0;
    16. }
    17. //output:
    18. Max (20,10): 20
    19. Max (0,200): 200
    20. Max (100,1010): 1010

    5、this、类指针、静态变量

    1. #include
    2. using namespace std;
    3. class Box {
    4. public:
    5. static int size; // 静态变量
    6. Box(double l, double h, double w) {
    7. cout << "process init..." << endl;
    8. length = l;
    9. heigth = h;
    10. width = w;
    11. }
    12. double Volume(void) {
    13. return length * heigth * width;
    14. }
    15. int compare(Box box) { // this指针,this表示自身对象
    16. return this->Volume() > box.Volume();
    17. }
    18. private:
    19. double length;
    20. double heigth;
    21. double width;
    22. };
    23. int Box::size = 1; // 初始化静态变量
    24. int main() {
    25. Box box1(1.1, 1.2, 1.3);
    26. Box box2(2.1, 2.2, 2.3);
    27. Box *ptr;
    28. ptr = &box1;
    29. cout << "pointer object' volume is " << ptr->Volume() << endl;
    30. if (box1.compare(box2)) {
    31. cout << "box1 is bigger than box2" << endl;
    32. } else {
    33. cout << "box1 is smaller than box2" << endl;
    34. }
    35. cout<<"the size of box1 is "<
    36. return 0;
    37. }
    38. //output:
    39. process init...
    40. process init...
    41. pointer object' volume is 1.716
    42. box1 is smaller than box2
    43. the size of box1 is 1
  • 相关阅读:
    利用异常实现短期hook
    ubuntu18.04与windows文件互传
    python如何筛选具有多列值相同的两个dataframe
    Spring 使用指南 ~ 1、Spring 的 IOC 和 DI 简介
    洛谷基础题练习2
    流媒体协议之RTSP详解
    [nlp] chathome—家居装修垂类大语言模型的开发和评估
    微信小程序python+uniapp+hbuiderx宠物美容用品商城领养寄养系统i843n
    Windows 安装,配置Tomcat
    Nginx学习笔记06——Nginx反向代理
  • 原文地址:https://blog.csdn.net/qq_38901850/article/details/126374442