我正在学习C++类,在我的课程中,我需要创建一个包含“book”,“author”和“Store”类的书店。C++在类之间传递对象
我不太清楚如何在Book类中提取作者的名字时如何在类之间进行交互。
当我运行在Xcode下面一段代码我通知对全部作品的类以下错误的构造器:预期‘(’或‘{’
UDPATE 22-NOV-2016 我更新了什么我迄今所做的代码。
首先,我有问题,当我尝试从Book_copy类显示作者的名字,我简直不知道该怎么做。
我现在已经放了author.getName(),但我知道它不起作用。
第二个问题是禁用作者拷贝构造函数和Book构造函数时出现的错误。 ???
第三个问题,假设上述内容是固定的,它不会完全按照我收到的指导来构建这个程序。
Book类必须的特点:一个字符串标题,经常提及它的作者,而另一个字符串这是写在语言
这就是我在第一时间得到了拧欲以对作者的常量引用...
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Author{
private:
string name;
bool award;
public:
Author(string name, bool award=false) : name(name), award(award) {}
const string getName(){ return name; }
const bool getAward() { return award; }
Author(Author const&) = delete;
};
class Book{
private:
string title;
Author &author;
string language;
public:
Book(string title, Author author, string language) : title(title), author(author), language(language) {}
const string getTitle() { return title; }
string getAuthor() { return author.getName(); }
const string getLanguage() { return language; }
void display_book() { cout << title << author.getName() << endl; }
~Book() { cout << title << author.getName() << endl;}
Book(Book const&) = delete;
};
class Book_copy{
private:
Book &book;
public:
Book_copy(Book &nuovo) : book(nuovo) {
cout << book.getTitle() << author.getName() << endl;
}
/*** Copy constructor ***/
// Book_copy(Book_copy const& dupli)
~Book_copy() {
cout << book.getTitle() << author.getName() << endl;
}
const Book &getBook() const { return book; }
void display_book() { cout << book.getTitle() << author.getName() << endl; }
};
class Library{
private:
vector <Book_copy*> bibil;
string name;
public:
Library(string name) : name(name){
cout << name << " is open" << endl;
}
const string &getName() { return name; }
void store(Book_copy const &livre, unsigned int qty = 1) {
for(unsigned int i(0); i < qty; i++){
// This is where I need to store the number of book copy in the library...
}
}
~Library() {
cout << name << " is closing down" << endl;
}
};
int main()
{
Author a1("Victor Hugo"),
a2("Stephen King"),
a3("Raymond Queneau", true);
Book o1("Les Misérables", a1, "french");
Book o2("Carrie", a2, "english");
Library biblio("whatever");
biblio.store(o1);
biblio.store(o2, 2);
return 0;
}
可能有一些概念我没有完全理解,这就是为什么我创建这些问题。
请赐教。
UPDATE 29 - 11月-2016
没人?我要去这里圈。每当我找到工作时,其他的东西就可以工作了:( 下面的代码几乎可以工作,除了看起来Bibiliotheque矢量是空的。 线条太多,有些完全没用,我要去来回尝试不同的东西,使其工作...
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Auteur{
private:
string nom;
bool prix;
public:
Auteur(string nom="", bool prix=false)
: nom(nom), prix(prix) {}
string getNom() { return nom; }
bool getPrix() { return prix; }
Auteur(Auteur const&) = delete;
};
class Oeuvre{
private:
string titre;
Auteur &auteur;
string langue;
public:
Oeuvre(string titre, Auteur &auteur, string langue)
: titre(titre), auteur(auteur), langue(langue) {}
const string getTitre() { return titre; }
string getAuteur() const { return auteur.getNom(); }
const Auteur &getAut() const { return auteur; }
string getLangue() { return langue; }
void affiche() const { cout<<"Affiche "<<titre<<auteur.getNom()<<langue << endl; }
~Oeuvre() { cout<<"Destroyed "<<titre<<auteur.getNom()<<langue<< endl; }
Oeuvre(Oeuvre const&) = delete;
};
class Examplaire{
private:
Oeuvre &oeuvre;
public:
Examplaire(Oeuvre &nuovo)
: oeuvre(nuovo){ cout<<"New "<<oeuvre.getTitre()<<oeuvre.getAuteur()<<oeuvre.getLangue() << endl; }
Examplaire(Examplaire const& copie)
: oeuvre(copie.oeuvre){ cout<<"Copy "<<oeuvre.getTitre()<<oeuvre.getAuteur()<<oeuvre.getLangue() << endl; }
const Oeuvre &getOeuvre() const { return oeuvre; }
string getTitre() { return //TODO need to return title
oeuvre.getTitre();
}
string getLangue() { return "";
// oeuvre.getLangue(); // crashes!! TODO need to return language
}
void affiche() { cout<<"Affiche "<< oeuvre.getTitre()<<oeuvre.getAuteur()<<oeuvre.getLangue();
}
};
class Bibliotheque{
private:
vector <Examplaire*> bibil;
string name;
public:
Bibliotheque(string name) : name(name){
cout << "La bibliothèque " << name << " est ouverte !" << endl;
}
const string &getNom() { return name; }
void stocker(Oeuvre & livre, unsigned int qty = 1) {
for(int i(0); i < qty; i++){
Examplaire temp = livre;
bibil.push_back(&temp);
}
}
const void lister_exemplaires(string langue = "none") const {
for(auto & book : bibil){
if((book->getLangue() == langue))
cout << book->getTitre() << endl;
// TODO display books in a chosen language or all if none
}
}
int compter_exemplaires(Oeuvre &livre) const{
int compt = 0;
for(auto * book : bibil){
// TODO display how many copy of livre
if(book->getTitre() == livre.getTitre())
compt++;
}
return compt;
}
void afficher_auteurs(bool prix = false) const{
for(auto * book : bibil){
// TODO display auteur names based on prix true or false
}
}
~Bibliotheque() {
cout << "La bibliothèque " << name << " ferme ses portes,"<< endl;
cout << "et détruit ses exemplaires :" << endl;
}
};
int main()
{
Auteur a1("Victor Hugo"),
a2("Alexandre Dumas"),
a3("Raymond Queneau", true);
Oeuvre o1("Les Misérables " , a1, " français"),
o2("L'Homme qui rit " , a1, " français"),
o3("Le Comte de Monte-Cristo " , a2, " français"),
o4("Zazie dans le métro " , a3, " français"),
o5("The Count of Monte-Cristo ", a2, " anglais");
Bibliotheque biblio("public");
biblio.stocker(o1, 2);
biblio.stocker(o2);
biblio.stocker(o3, 3);
biblio.stocker(o4);
biblio.stocker(o5);
cout << "The library contains :"<< endl;
biblio.lister_exemplaires();
cout << "The books is english are :" << endl;
biblio.lister_exemplaires("anglais");
cout << "Award authors are:" << endl;
biblio.afficher_auteurs(true);
cout << biblio.compter_exemplaires(o3)<< " cpoy of Monte christo (should be 3)"<<endl;
cout << "o4 title is " << o4.getTitre()<< "(should be Zazie) :" << endl;
return 0;
}
拼写错误'作者'构造函数的名字。 –
构造函数的名称应该与类名称匹配。另外一个对象持有对另一个对象的引用,那更多的是设计问题...排序组合vs聚合。 – basav
谢谢。类,属性,构造函数等是用不同的语言,我翻译它们给出一些上下文。我忘了那一个。 –