Class as memebrs of other classes : Create a constructor that takes all data
-
Hello ! I have 2 classes Books and Author. On of members of Books is of type Author. On Class Books I want to have a Constructor that takes all the parametres for book and for author : This is my code :
class author
{private :
string name;
string email;public :
string get_name(){return name;}
string get_email(){return email;}void set\_name(string name){this->name=name;} void set\_email(string email){this->email=email;} author(string name,string email) { this->name=name; this->email=email; }
}
class book
{
private :
string title;
int year;
author auth;public:
string get\_title(){return title;} int get\_year(){return year;} void set\_title(string title){this->title=title;} void set\_year(float year){this->year=year;} book(string title, int year):author(string name,string email) { this->title=title; this->year=year; ??????? }
}
I don't know how can i change the constructor of book in order that it takes all the perameter for book and for the author? Thank you !
-
Hello ! I have 2 classes Books and Author. On of members of Books is of type Author. On Class Books I want to have a Constructor that takes all the parametres for book and for author : This is my code :
class author
{private :
string name;
string email;public :
string get_name(){return name;}
string get_email(){return email;}void set\_name(string name){this->name=name;} void set\_email(string email){this->email=email;} author(string name,string email) { this->name=name; this->email=email; }
}
class book
{
private :
string title;
int year;
author auth;public:
string get\_title(){return title;} int get\_year(){return year;} void set\_title(string title){this->title=title;} void set\_year(float year){this->year=year;} book(string title, int year):author(string name,string email) { this->title=title; this->year=year; ??????? }
}
I don't know how can i change the constructor of book in order that it takes all the perameter for book and for the author? Thank you !
To answer your question
class book {
...
book(string title, int year, string name, string email) : auth(name, email) {
...
}
};Some things for you to think about: 1) If you have getters and setters for all your private members, how is this different than making the private members public? 2) Although names don't usually change change (Ferdinand Lewis Alcindor Jr might disagree), emails can and do. What happens if you have a large collection of books and want to change the email of an author? There might be a better way to structure your data.
-
Hello ! I have 2 classes Books and Author. On of members of Books is of type Author. On Class Books I want to have a Constructor that takes all the parametres for book and for author : This is my code :
class author
{private :
string name;
string email;public :
string get_name(){return name;}
string get_email(){return email;}void set\_name(string name){this->name=name;} void set\_email(string email){this->email=email;} author(string name,string email) { this->name=name; this->email=email; }
}
class book
{
private :
string title;
int year;
author auth;public:
string get\_title(){return title;} int get\_year(){return year;} void set\_title(string title){this->title=title;} void set\_year(float year){this->year=year;} book(string title, int year):author(string name,string email) { this->title=title; this->year=year; ??????? }
}
I don't know how can i change the constructor of book in order that it takes all the perameter for book and for the author? Thank you !
-
Hello ! I have 2 classes Books and Author. On of members of Books is of type Author. On Class Books I want to have a Constructor that takes all the parametres for book and for author : This is my code :
class author
{private :
string name;
string email;public :
string get_name(){return name;}
string get_email(){return email;}void set\_name(string name){this->name=name;} void set\_email(string email){this->email=email;} author(string name,string email) { this->name=name; this->email=email; }
}
class book
{
private :
string title;
int year;
author auth;public:
string get\_title(){return title;} int get\_year(){return year;} void set\_title(string title){this->title=title;} void set\_year(float year){this->year=year;} book(string title, int year):author(string name,string email) { this->title=title; this->year=year; ??????? }
}
I don't know how can i change the constructor of book in order that it takes all the perameter for book and for the author? Thank you !
Presumably this is homework. At any rate a "copy constructor" sounds quite possibly what the homework really wants you to come up with. Although perhaps the assignment is to let you copy everything first and then next demonstrate what a copy constructor is.
-
To answer your question
class book {
...
book(string title, int year, string name, string email) : auth(name, email) {
...
}
};Some things for you to think about: 1) If you have getters and setters for all your private members, how is this different than making the private members public? 2) Although names don't usually change change (Ferdinand Lewis Alcindor Jr might disagree), emails can and do. What happens if you have a large collection of books and want to change the email of an author? There might be a better way to structure your data.
@1) he doesn't have a getter and setter for book::author ;-) @2) agreed. Either the class author should really be a class author_reference, and name and email are just a (n unsuitable) choice of a unique reference to this specific author which is represented within a separate class and my contain more (private) data like home address and preferred brand of peanut butter. Or the class author really represents an actual author, in which case (s)he should only be referenced, not defined as a data member. After all, the author shouldn't die just because a book is burned... :omg:
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)