C++ 参数化构造函数

2025年8月29日 | 3 分钟阅读

我们知道,每当我们创建一个类时,都会生成一个默认构造函数,这在其他语言中通常是不可见的。但是,当我们自己创建一个构造函数时,我们就可以灵活地传递在主函数中创建对象时我们认为对构造函数必要的参数。创建一个参数化构造函数非常简单。我们只需在类函数中创建我们的构造函数,并传递必要的参数。

C++ 代码

输出

/ tm p/ po Rl 42 Ph db .o
the value we have created for p1.b is: 10, the value we have created for p1.b is: p1.c = 15

C++ 代码

输出

/tmp/poRl42Phdb.o
the values for t1.x is:  20 the values for t2.x is:  15
After destructing the objects which have the value of x is as follows:  15
After destructing the objects which have the value of x is as follows:  20

C++ 代码

输出

after calculating the area of rectangle 1, we have got as 15624
after estimating the size of rectangle two, we have obtained: 28911