'How do you add objects to a new vector class in C++?
My goal is to add objects from a class to a new class where they will be stored in a vector, which I will use later to add/delete/display elements. The vector will act as a database.
In the vector class, I am unsure if I am able to return the inputDB from object class. Any help will be appreciated. It doesn't necessarily have to be done the way I have wrote it.
Header file for Object
#ifndef OBJECT_H
#define OBJECT_H
#include <iostream>
#include <string>
Class Object
{
friend std::istream& inputDB(std::istream&, Object&)
private:
std::string Name
public:
Object(std::string name); //constructor
const std::string& getName() const {return this -> Name;};
}
std::istream &inputDB(std::istream&, Object&)
#endif // OBJECT_H
Source file for Object
#include "OBJECT.H"
Object::Object(std::string name){Name { name }} {};
std::istream &inputDB (std::string istream& is, Object& x)
{
std::cout << "Enter name: ";
is >> x.Object
return is;
}
New vector class header
#ifndef VECTOR_H
#define VECTOR_H
#include <Object.h>
#include <iostream>
#include <string>
#include <vector>
Class VectorDB
{
private:
std::vector<Object> DB;
public:
Database();
void inputDB(std::istream&)
}
#endif // VECTOR_H
Source file for Vector
#include "VECTOR.h"
VectorDB::VectorDB()
{}
void inputDB(std::istream& ist)
{}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
