C++函数调用与Struct和CLass的区别是什么

发布时间:2022-05-10 14:13:21 作者:zzz
来源:亿速云 阅读:215

C++函数调用与Struct和Class的区别

在C++编程中,函数调用、结构体(struct)和类(class)是三个非常重要的概念。理解它们之间的区别和联系,对于编写高效、可维护的代码至关重要。本文将详细探讨C++中函数调用与structclass的区别。

1. 函数调用

函数调用是C++中最基本的编程概念之一。函数是一段可重复使用的代码块,用于执行特定的任务。函数调用是指程序在执行过程中,通过函数名和参数列表来调用函数,从而执行函数体内的代码。

1.1 函数调用的基本语法

return_type function_name(parameter_list) {
    // 函数体
    // 执行特定任务的代码
    return return_value; // 可选
}

1.2 函数调用的过程

  1. 参数传递:调用函数时,实参的值会被传递给形参。
  2. 执行函数体:函数体内的代码会被执行。
  3. 返回值:函数执行完毕后,可能会返回一个值给调用者。

1.3 函数调用的示例

#include <iostream>

int add(int a, int b) {
    return a + b;
}

int main() {
    int result = add(3, 4); // 函数调用
    std::cout << "Result: " << result << std::endl;
    return 0;
}

2. Struct 和 Class 的区别

structclass是C++中用于定义自定义数据类型的两种关键字。它们的主要区别在于默认的访问控制权限。

2.1 默认访问控制权限

2.2 示例代码

2.2.1 使用 struct

#include <iostream>

struct Point {
    int x; // 默认是 public
    int y; // 默认是 public
};

int main() {
    Point p;
    p.x = 10; // 可以直接访问
    p.y = 20; // 可以直接访问
    std::cout << "Point: (" << p.x << ", " << p.y << ")" << std::endl;
    return 0;
}

2.2.2 使用 class

#include <iostream>

class Point {
    int x; // 默认是 private
    int y; // 默认是 private

public:
    void setX(int xVal) { x = xVal; }
    void setY(int yVal) { y = yVal; }
    int getX() const { return x; }
    int getY() const { return y; }
};

int main() {
    Point p;
    p.setX(10); // 通过公有成员函数访问
    p.setY(20); // 通过公有成员函数访问
    std::cout << "Point: (" << p.getX() << ", " << p.getY() << ")" << std::endl;
    return 0;
}

2.3 其他区别

3. 函数调用与Struct和Class的关系

函数调用与structclass的关系主要体现在以下几个方面:

3.1 成员函数

structclass都可以包含成员函数。成员函数是定义在structclass内部的函数,用于操作该结构体或类的成员变量。

class Rectangle {
    int width, height;

public:
    void setDimensions(int w, int h) {
        width = w;
        height = h;
    }
    int area() const {
        return width * height;
    }
};

int main() {
    Rectangle rect;
    rect.setDimensions(5, 10); // 调用成员函数
    std::cout << "Area: " << rect.area() << std::endl; // 调用成员函数
    return 0;
}

3.2 构造函数和析构函数

structclass都可以定义构造函数和析构函数,用于对象的初始化和清理。

class MyClass {
public:
    MyClass() {
        std::cout << "Constructor called!" << std::endl;
    }
    ~MyClass() {
        std::cout << "Destructor called!" << std::endl;
    }
};

int main() {
    MyClass obj; // 构造函数被调用
    return 0; // 析构函数被调用
}

3.3 函数重载

structclass中的成员函数可以重载,即定义多个同名函数,但参数列表不同。

class Calculator {
public:
    int add(int a, int b) {
        return a + b;
    }
    double add(double a, double b) {
        return a + b;
    }
};

int main() {
    Calculator calc;
    std::cout << "Int add: " << calc.add(3, 4) << std::endl;
    std::cout << "Double add: " << calc.add(3.5, 4.5) << std::endl;
    return 0;
}

4. 总结

理解这些概念及其区别,有助于编写更加清晰、高效的C++代码。

推荐阅读:
  1. 关于Swift中Struct,Class和Enum的哪些事儿
  2. Struct和Union的区别

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++ class struct

上一篇:es6中from方法如何用

下一篇:linux中la文件指的是什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》