I am trying to implement a class, that has another class as a member.
class Book
{
class Author;
}
In a .cpp file I am implementing the class Author as follows:
class Author
{
string firstName;
string lastName;
public:
// the constructor for the class
Author(string _firstName, string _lastName)
{
firstName = _firstName;
lastName = _lastName;
}
}
I want to do the following: in main, when I declare an object of class Book, I want it to instantiate the constructor of the class Author, from the code above. I was thinking of doing so by passing the parameters to the constructor of the Book class, and then using them to instantiate the constructor of Author, like this:
Book::Book(string a, string b)
{
Book::Author(a, b);
}
But if I do so, if I have another inner class, as a member of the class Book, that has, let's say - 4 members, I would have too many parameters for the constructor of Book.
So my question is: is there another way to instantiate the constructor of a class that is a member when I create an object of the main class?
And also, how do I print the members of that main class, if these members are classes? I tried to overload the << operator for the inner classes, but I get the error: "invalid use of nonstatic data member."