4 Replies - 457 Views - Last Post: 17 October 2012 - 10:17 AM Rate Topic: -----

#1 Sdesser  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-October 12

Making a program in C++ that stores objects in vectors

Posted 17 October 2012 - 08:29 AM

Hey guys! I found this site when I was searching for help with my C++ courses weekly assignment.
Here's what I would need to do:

Quote

In this assignment, a container class for salary information is implemented. First, construct a class Person describing personal information with attributes first name, last name, and identity number (all can be of type string). Then construct class Salaryinfo that describes personís salary information. Itís one attribute is Person object and other attribute is salary (double). Finally construct the container class Wagecontainer that stores Salaryinfo objects in a vector (vector is defined in the Standard Template Library). Implement the following methods in the container class:

Method to insert a Salaryinfo object into the container
Method to print all the contents of the container
Method to print information of the persons whose salary is greater than a boundary value given as a parameter to the method
Method to print information of the persons whose salary is less than a boundary value given as a parameter to the method
Write a main program to test the container class.


I've got the very basic framework made so far and I've got no idea on how to get the vector to store the required data.
I've completed our schools introduction course for C language and I've learned the very basics of using classes and objects, but this problem just blew my mind and I've spent hours upon hours, just pondering on how to do it.

Help would be very much appreciated!

#include <iostream>
#include <conio.h>
#include <string>
#include <vector>

using namespace std;


class Person{
    public:
    Person();
    ~Person(){}
    string firstName;
    string lastName;
    string idNumber;

    private:

};

class SalaryInfo : public Person{
    public:
    SalaryInfo();
    ~SalaryInfo(){}
    double salary;
    Person SalaryInfoPerson;

    private:

};

class WageContainer{
    public:
    WageContainer();
    ~WageContainer(){}
    vector<SalaryInfo> DataContainer;

    private:

};


int main()
{

    

    getch();
    return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Making a program in C++ that stores objects in vectors

#2 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: Making a program in C++ that stores objects in vectors

Posted 17 October 2012 - 08:48 AM

Do you know how std::vector1 works? Do you know how to implement a method2 (member function)?

Go ahead and try, then post it here if you have trouble getting it done. Just read your assignment, it's nothing above your capabilities, I'm sure.
___
1) http://www.cplusplus...nce/stl/vector/
2) http://www.cplusplus...torial/classes/
Was This Post Helpful? 0
  • +
  • -

#3 Sdesser  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-October 12

Re: Making a program in C++ that stores objects in vectors

Posted 17 October 2012 - 09:11 AM

Method Implementing?
I'm not quite sure, can you give an example?
Was This Post Helpful? 0
  • +
  • -

#4 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: Making a program in C++ that stores objects in vectors

Posted 17 October 2012 - 10:11 AM

Maybe I was being too vague, if so, please excuse me.

Your assignment is as follows:

Quote

Implement the following methods in the container class:

Method to insert a Salaryinfo object into the container
Method to print all the contents of the container
Method to print information of the persons whose salary is greater than a boundary value given as a parameter to the method
Method to print information of the persons whose salary is less than a boundary value given as a parameter to the method

Let's take the first method, the one that inserts a SalaryInfo object into the container. You have this, at the moment:
class WageContainer{
    public:
    WageContainer();
    ~WageContainer(){}
    vector<SalaryInfo> DataContainer;

    private:

};

What makes sense, rather, is that vector<SalaryInfo> DataContainer is private, or protected. As it is now, anybody can use that vector, since it is public. You want to place restrictions on it, so one has to use your WageContainer public methods.
Now, after you put your vector under private, declare new public method, and in its implementation place a code that will loop trough and display all elements of the said private vector.

An example:
#include <iostream>

class A {
  private:
    int foo;
  public:
    void set_foo(int v); // declaration
    void print_foo();
};

void A::set_foo(int v) { // implementation
  if (v > 100) { // we set restrictions on what values priv can have, because we're that evil
    v = 100;
  } else if (v < -100) {
    v = -100;
  }
  foo = v;
}

void A::print_foo() { std::cout << foo << "\n"; }

int main() {
  A a;
  a.set_foo(-200);
  a.print_foo();
  return 0;
}


This post has been edited by Xupicor: 17 October 2012 - 10:13 AM

Was This Post Helpful? 0
  • +
  • -

#5 Sdesser  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-October 12

Re: Making a program in C++ that stores objects in vectors

Posted 17 October 2012 - 10:17 AM

Ah! I think I get it now! Our lecturer isn't that good at teaching stuff to others, so me and many of my friends are completely lost on the course, (he's good at reading PDF documents out loud though :P).

I'll give it a go and let you know how it went :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1