The initialization vector with multiple data types or object classes

The task, like a book library. There is a class book that lists the author, title, year of publication. I want to create a separate class DataBase where the presence of the books will be listed exclusively. Is it possible to create a vector initialized class Book? That is, at a time enter the name, author, year.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
 
#pragma once
#include<string>
using namespace std;
 
class Catalog;
class Book
{
    string m_autor;
    string m_title;
    int m_year;
public:
    Book();
    Book(const string&,const string&,int);
    void setAutor(const string&);
    void setTitle(const string&);
    void setYear(const int);
    string getAutor()const {return m_autor; }
    string getTitle()const {return m_title; }
    int getYear()const { return m_year; }
    bool check(const string&)const;
    ~Book();
};
 
#pragma once
#include<vector>
#include"Book.h"
using namespace std;
 
class DataBase
{
    vector<Book>m_book(Book);
public:
    DataBase();
    
    ~DataBase();
};
 
#include "DataBase.h"
 
vector<Book> DataBase::m_book(Book)
{
    m_book(Book).push_back("Оtello", "Shakespeare", 1596);  //error
    return vector<Book>();
}
 
DataBase::DataBase()
{
}
1. define the overloaded ctor: Book (const string& autor, const string& title, int year)
2. remove the Book argument to Database::m_book() as it is superflous
3. declare a local std::vector<Book> that Database::m_book() would return
4. push_back above vector<Book> with Book objects created using the overloaded ctor
5. unless your ctors are acquiring resources (through new operator for eg) you don't need to explicitly define a dtor
6. I haven't removed all references to them but using namespace std is a bad idea:
http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <string>
#include<vector>
using namespace std;

class Catalog;
class Book
{
    string m_autor;
    string m_title;
    int m_year;
public:
    Book();
    Book (const string& autor, const string& title, int year)
        : m_autor(autor), m_title(title), m_year(year){}

    void setAutor(const string&);
    void setTitle(const string&);
    void setYear(const int);
    string getAutor()const {return m_autor; }
    string getTitle()const {return m_title; }
    int getYear()const { return m_year; }
    bool check(const string&)const;

};

class DataBase
{
    vector<Book> m_book();
public:
};

vector<Book> DataBase::m_book()
{
    vector<Book> vec;
    vec.push_back(Book("Оtello", "Shakespeare", 1596));
    return vec;
}
int main()
{

}
Topic archived. No new replies allowed.