#include <iostream>
using namespace std;
class date{
public:
int month;
int day;
int year;
private:
date(int x, int y, int z);
public:
date(int x, int y);
};
date::date(int x, int y, int z): month{x}, day{y}, year{z} {
cout << "Hello you called me PRIVATE constructor" << endl;
}
date::date(int x, int y){
cout << "Hello you called me PUBLIC constructor" << endl;
date(x, y, 100);
}
int main(){
date x{11, 21};
cout << x.month << endl;
cout << x.day << endl;
cout << x.year << endl;
}
As you can see in above code i have two constructor and in main i create object x with two arguments.
This should call the public constructor which in turn calls the private constructor and initialises the public member month day and year.
But when i print members values out i don't get the desired results.
Hello you called me PUBLIC constructor
Hello you called me PRIVATE constructor
392622664
1
0
whereas the output should be:
Hello you called me PUBLIC constructor
Hello you called me PRIVATE constructor
11
21
100
I don't know where i did something wrong. Any help will be appreciated. Thank you.