此程序中不调用没有参数的构造函数

Constructor without argument is not called in this program

本文关键字:参数 构造函数 调用 程序      更新时间:2023-10-16

我在cpp中编写了这个简单的程序。但不调用没有参数的构造函数。请帮我说一下为什么没有人叫它。。

该程序的输出是

623423
--------------------------------
Process exited after 0.07689 seconds with return value 0
Press any key to continue . . .

程序:

#include<iostream>
class Date{
int day,month,year;
public:
Date();
Date(int,int);
Date(int);
Date(int,int,int);  
};
Date :: Date(){
day=10;
month=5;
year=19;
std::cout<<"********************************************";
std::cout<<day<<month<<year;
}
Date :: Date(int a){
day=a;
std::cout<<day;
}
Date ::Date (int a,int b){
day=a;
month=b;
std::cout<<day<<month;
}
Date ::Date(int a,int b,int c){
day=a;
month=b;
year=c;
std::cout<<day<<month<<year;
}
int main(){
Date one();
Date two(6);
Date three(2,3,4);
Date four(2,3);

return 0;
}
Date one();

这不是做你期望的事。它声明了一个名为one的函数,该函数按值返回Date,正如@Some programmer dude在评论部分正确地说的那样。您可以编写Date one;,也可以使用C++11统一初始化Date one{};