MST

星途 面试题库

面试题:C++ 构造函数声明形式规范的基础应用

请声明一个名为 `Rectangle` 的类,它有两个 `int` 类型的成员变量 `width` 和 `height`。要求写出符合 C++ 构造函数声明形式规范的默认构造函数和带参数的构造函数,带参数的构造函数用于初始化 `width` 和 `height`。
18.3万 热度难度
编程语言C++

知识考点

AI 面试

面试题答案

一键面试
class Rectangle {
public:
    int width;
    int height;

    // 默认构造函数
    Rectangle() : width(0), height(0) {}

    // 带参数的构造函数
    Rectangle(int w, int h) : width(w), height(h) {}
};