s******c 发帖数: 1920 | 1 为什么一下程序可以省略构造函数?什么情况下可以省略?
// friend functions
#include
using namespace std;
class CRectangle {
int width, height;
public:
void set_values (int, int);
int area () {return (width * height);}
friend CRectangle duplicate (CRectangle);
};
void CRectangle::set_values (int a, int b) {
width = a;
height = b;
}
CRectangle duplicate (CRectangle rectparam)
{
CRectangle rectres;
rectres.width = rectparam.width*2;
rectres.height = rectparam.height*2;
return (rectres);
}
int main () {
CRectangle rect, rectb;
rect.set_values (2,3);
rectb = duplicate (rect);
cout << rectb.area();
return 0;
} | N***m 发帖数: 4460 | 2 你不写构造函数的话,compiler帮你自动生成。
没有特殊需求的话,你可以省略构造函数。
【在 s******c 的大作中提到】 : 为什么一下程序可以省略构造函数?什么情况下可以省略? : // friend functions : #include : using namespace std; : class CRectangle { : int width, height; : public: : void set_values (int, int); : int area () {return (width * height);} : friend CRectangle duplicate (CRectangle);
| r*******m 发帖数: 109 | 3 If you define any constructor, the compiler will not synthesize default one
for you. But if you don't define, it will.
【在 s******c 的大作中提到】 : 为什么一下程序可以省略构造函数?什么情况下可以省略? : // friend functions : #include : using namespace std; : class CRectangle { : int width, height; : public: : void set_values (int, int); : int area () {return (width * height);} : friend CRectangle duplicate (CRectangle);
|
|