简体   繁体   中英

How do you make a vector of class objects?

I'm a bit confused as to how vectors work. What I'm trying to do is create a vector of 5 TaxBill objects. Then, what I want to do is read from an input file that has the names and different tax rates for 5 states. I want to store the name of the state in the object and the tax rates of the state in an array in int main() .

Here's the input file called "Tax Rates.dat". The numbers are the sales, property and income tax rate, respectively, for each state.

TEXAS      .0825 .02 -.03
MARYLAND   .065 .04 .05
OHIO       .03 .025 .03
CALIFORNIA .095 .055 .045
MAINE      .02 .015 .02

Here's my class interface called "Tax Bill.h".

using namespace std;

class TaxBill
{
public:
      void setValue(string, int);
      void dataValid(double&, double&, double&);

private:
      string Name;
      int index;
      double taxBill;
}

Here's my class implementation called "Tax Bill.cpp".

#include "Tax Bill.h"

void TaxBill::setValue(string name, int x)
{}


void TaxBill::dataValid(double& a, double& b, double& c)
{
      if(a < 0)
            a = 0;
      if(b < 0)
            b = 0;
      if(c < 0)
            c = 0;

      return;
}

Here's my main source code so far.

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <vector>
#include "Tax Bill.h"

using namespace std;


int main()
{
      const int SALARY = 100000,
            HOUSE = 246000,
            PURCHASE = 36000;
      ifstream fin;
      fin.open("Tax Rates.dat");
      vector <TaxBill> someVector (5);
      double sales,
            property,
            income;
      double taxRates[5][3];
      string name;

      if(!fin)
            return 0;
      else
      {
            for(int i = 0; fin >> name >> sales >> property >> income; i++)
            {
                  dataValid(sales, property, income);
                  taxRates[i][0] = sales;
                  taxRates[i][1] = property;
                  taxRates[i][2] = income;
            }
      }

The for loop is where I want to store the name of the state read from the input file and i into the string Name and index of the class object. The reason for the index is because later in the program I want to sort the objects in the vector alphabetically but not the array where the corresponding tax rates are stored.

I also don't want to use the function push_back() .

I guess my question is, how do I make an vector of 5 class objects and access them?

Please keep in mind that my program is hardly complete and it's this one hurdle that's holding me back.

Here example of using vector from your code. Here you declare

vector <TaxBill> someVector (5);

So now, you have someVector[0] - [4] (5 in total). To use it, actually you just have to assign it like an normal array.

someVector[0].{insert property here}

But wait, in your class, there is no clear way to set the string Name and Index. So i think you forget to place it here, hence i make my own in the class.

class TaxBill
{
public:
      void setValue(string Name, int Index){
         name = Name; index = Index;
      }
      void dataValid(double&, double&, double&);

private:
      string name;
      int index;
      double taxBill;
}

Now to use the vector, i just used the property this way

someVector[0].setValue("someName",1);

Tada, you get it to works. Btw, i dont know why you declare a procedure in the class, but you want to used it multiple times in main program. I mean this one

dataValid(sales, property, income);

to used it, i suggest you make a procedure in main program rather than in class and anyway that line should produce an error anyway. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM