计算机小混子 2022-03-26 17:02 采纳率: 100%
浏览 22
已结题

为什么函数的形参列表是指针我主函数中传入的却是个字符串,如下图


// 106.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
using namespace std;
//定义rect类
class rect {
    int length;
    int width;
    int area;
public:
    rect(int l = 1, int w = 1)
    {
        length = l;
        width = w;
        area = length * width;
    }
    void show_rect(char* name)
    {
        cout << name << ":" << endl;
        cout << "length=" << length << endl;
        cout << "width=" << width << endl;
        cout << "area=" << area << endl;
    }
};
//测试使用rect类
void main()
{
    //用rect类创建对象
    rect a;
    rect b(2);
    rect c(2, 3);
    //调用对象的函数显示对象中的数据
    a.show_rect("a");
    b.show_rect("b(2)");
    c.show_rect("c(2,3)");
    
}

img

  • 写回答

1条回答 默认 最新

  • 浪客 2022-03-26 17:54
    关注

    void show_rect(const char* name)
    你传入的是字符串常量

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 4月3日
  • 已采纳回答 3月26日
  • 创建了问题 3月26日