0

I have this classes below and I want to save a HomePC to a vector, what's the method that I can do that, and how can I print the first or the second element from the vector.


class PC
{
public:
    PC(string in_operatingSystem,int in_ramSlots,int in_pcieSlots,int in_totalRamSlots, int in_gbPerRam, int in_cpu, int in_ssd, int in_cost);

    virtual void Print() = 0;
    virtual void Upgrade() = 0;
protected:
    string operatingSystem;
    int ramSlots,pcieSlots,totalRamSlots,gbPerRam;
    int cpu,ssd;
    int cost;
};

class HomePC: public PC
{
public:
    HomePC(string in_operatingSystem,int in_ramSlots,int in_pcieSlots,int in_totalRamSlots, int in_gbPerRam, int in_cpu, int in_ssd, int in_cost, string in_model);

    void Print();
    void Upgrade();
private:
    string model;
};
Steiros24
  • 1
  • 1

3 Answers3

0
// create
std::vector<HomePC> homePcs;

// insert
homePcs.push_back(HomePC{...});
// or
homePcs.emplace_back(...);

// get and print
std::cout << homePcs[0] << " " << homePcs[1] << '\n';

I highly recommend learning how to use cppreference.com, as this site could have easily answered your question.

Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
  • What's the format of the vector after that? for example if I push_back ("MacOS",2,3,4,...) what's the first element of the vector? – Steiros24 May 23 '20 at 16:32
  • The first element of the vector is the first element which you inserted, then comes the second one which you inserted etc. Note that your syntax for `push_back` is false. You can use `emplace_back` to directly forward the arguments like in your example or you can use `push_back({"MacOs", 2, 3, 4, ...})` to keep it simple. – Jan Schultke May 23 '20 at 16:37
  • Undefined symbol: vtable for HomePC, got this, any idea? – Steiros24 May 23 '20 at 16:48
  • Did you define, not just declare `HomePC::Print()` and `HomePC::Upgrade()` somewhere? – Jan Schultke May 23 '20 at 22:01
0

You can write something like this: vector<PC*> myPCs; myPCs.push_pack(new HomePC(...)); to create your vector and add HomePC object in this vector. Also you need to include library for using vector: #include <vector> and write using namespace std; below including line.

Alexandr
  • 1
  • 1
0
//First create the vector which will store the objects
std::vector<HomePC> homePcs;


// Create the object to push into the vector
HomePC temp;  //i named it temp as it will be used only to pass the object

//After inputing data in temp with accessors/mutators , just store it in the vector created

homePcs.push_back(temp);

You can print easily by using a for loop and the .size() methods in vector , to know how many times the loop should iterate.

for(int i=0;i<homePcs.size();i++)
{
    std::cout<<homePcs[i]<<std::endl;
}
RNGesus.exe
  • 205
  • 1
  • 12
  • 1
    You can loop through a vector in a much prettier way by using a range-based for: `for (const auto &pc : homePcs) {std::cout << pc << '\n';}` Also the use of `std::endl`is excessive here, see [C++: “std::endl” vs “\n”](https://stackoverflow.com/questions/213907/c-stdendl-vs-n). – Jan Schultke May 23 '20 at 16:40