The problem is that your class birthday
has a user-defined constructor and therefore the compiler will not synthesize the default constructor birthday::birthday()
for your class.
So when you wrote,
class person
{
//other members here
private:
birthday day; //this need the default constructor of birthday. But since class birthday has no default constructor this statement gives error
};
In the above snippet, the statetment, birthday day;
needs the default constructor of class birthday
but since there isn't one, so this statement results in the mentioned error.
Solution
To solve this you can add a default constructor for your class birthday as shown below:
class birthday
{
public:
//add default constructor
birthday() = default;
birthday(int d,int m,int y)
{
date=d;
month=m;
year=y;
}
void printdate()
{
cout<<date<<"/"<<month<<"/"<<year<<endl;
}
private:
//using in class initializers give some default values to the data members according to your needs
int date = 0;
int month =0;
int year = 0;
};
class person
{
public:
person(string x)
{
name=x;
}
void printname()
{
cout<< "the birthday person is "<< name << " whose birthday is on ";
day.printdate();//moved this out of the cout because printdat() does not return anything
}
private:
birthday day;
string name;
};
int main()
{
birthday b(10,11,2007);
person p("Jessica");
p.printname();
return 0;
}
Some of the changes that i made include:
- Added default constructor for class
birthday
.
- Used in-class initializers for data member
date
, year
and month
inside class birthday
.
- There was another problem with your code. Originally you had a statement
cout<<day.printdate()
in your program. But since printdate()
has return type void
, so the statement cout<<day.printdate()
will give you error. To solve this, i removed the cout
from this statement.
The output of the modified program can be seen here.