4 Replies - 1630 Views - Last Post: 10 December 2009 - 12:55 PM Rate Topic: -----

#1 pennat05  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 09-December 09

how do i declare a vector of objects and access function of the class

Posted 09 December 2009 - 08:14 PM

#include<iostream>
using namespace std;
#include<vector>

Hi, I am trying to figure out the basics of vectors and and how to use them with objects. I was wondering how to properly declare and use them and how to store data in them by using member functions of the class.  Below i have included a sample program that i am trying to get to work. When i try to compile this its says "did" does not have the function addz. So if that does not work how do I get it to work the right way. Also after the data is stored, can i display it by adding a print function in tghe class. Any help would be great!!




class Add
{
      
public:
        
         int addz(int n,int m)
         {a= n+m;
         return a;}
        private:
               int a;
               };// end of class
               
               
               int main()
               {
                   int y,b;
               Add temp1;
               vector<Add*> did(2);
               cout<<"Enter too numbers: "<<endl;
               cin>>y>>b;
               int u= did.addz(y,b );
               cout<<"the sum is:"<<u<<endl;
               system("pause");
               return 0;
               }//end of main


*** MOD EDIT: Added code tags. Please :code: ***

This post has been edited by JackOfAllTrades: 10 December 2009 - 01:12 PM


Is This A Good Question/Topic? 0
  • +

Replies To: how do i declare a vector of objects and access function of the class

#2 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 379
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: how do i declare a vector of objects and access function of the class

Posted 09 December 2009 - 09:22 PM

I don't see a variable or object declared anywhere name 'did'. You might want to try and declare one before using one.
Was This Post Helpful? 0
  • +
  • -

#3 pennat05  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 09-December 09

Re: how do i declare a vector of objects and access function of the class

Posted 09 December 2009 - 09:43 PM

View Posteker676, on 9 Dec, 2009 - 08:22 PM, said:

I don't see a variable or object declared anywhere name 'did'. You might want to try and declare one before using one.


did, was used after the vector. its a vector of objects type Add. and the vector is called did
Was This Post Helpful? 0
  • +
  • -

#4 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 944
  • View blog
  • Posts: 2,464
  • Joined: 20-August 07

Re: how do i declare a vector of objects and access function of the class

Posted 10 December 2009 - 03:59 AM

Vectors are used when you need more than 1 object of a certain type; i.e. when you need to collect multiple bits of data together.

Assuming your Add class worked slightly differently in accepting the numbers through one function, then returning their result in another
class Add
{
    int a;
public:
    void set(int n, int m) 
    { 
        a = n+m; 
    }
    int get()
    {
        return a; 
    }
};

int main()
{
    //A single add object:
    Add object;
    object.set(1,2);
    cout << object.get() << endl;
} 
Small modification so that get() will access the stored data a without having to set anything.


With a vector, you can create 'copies' of an object - as many copies as you like in fact, using push_back(). a vector always starts out empty, but push_back will grow a vector by 1 in size using a copy in memory of whatever object you pass to it.
    vector<Add> numbers;
    Add object;
    int a,b;

    cout << "Enter two numbers: ";
    cin >> a >> b;
    object.set( a,b );
    numbers.push_back( object );

    cout << "Enter two more numbers: ";
    cin >> a >> b;
    object.set( a,b );
    numbers.push_back( object );

    cout << "Enter another two numbers: ";
    cin >> a >> b;
    object.set( a,b );
    numbers.push_back( object );

    cout << "first:  "   << numbers.at(0).get()
         << "\nsecond: " << numbers.at(1).get()
         << "\nthird:  " << numbers.at(2).get()
         << endl;
output:
Enter two numbers: 1 2
Enter two more numbers: 3 4
Enter another two numbers: 5 6
first:  3
second: 7
third:  11 


Notice the output statement
    cout << "first:  "   << numbers.at(0).get()
         << "\nsecond: " << numbers.at(1).get()
         << "\nthird:  " << numbers.at(2).get() 
numbers is the name of the vector.
numbers.at(X) means "reach the object stored at position X" - which is an Add. anything you can do with an Add object, you can also do with numbers.at(X) - such as .get().

When a vector is "grown" using push_back, the copy of the object you add is inserted to the next available position - a vector which has 2 stored objects will have those stored at positions 0 and 1 (computers always count from zero!). a newly inserted object will appear at position 2.



edit: changed to stop emoticons appearing in code

This post has been edited by Bench: 10 December 2009 - 04:03 AM

Was This Post Helpful? 1
  • +
  • -

#5 pennat05  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 09-December 09

Re: how do i declare a vector of objects and access function of the class

Posted 10 December 2009 - 12:55 PM

View PostBench, on 10 Dec, 2009 - 02:59 AM, said:

Vectors are used when you need more than 1 object of a certain type; i.e. when you need to collect multiple bits of data together.

Assuming your Add class worked slightly differently in accepting the numbers through one function, then returning their result in another
class Add
{
    int a;
public:
    void set(int n, int m) 
    { 
        a = n+m; 
    }
    int get()
    {
        return a; 
    }
};

int main()
{
    //A single add object:
    Add object;
    object.set(1,2);
    cout << object.get() << endl;
} 
Small modification so that get() will access the stored data a without having to set anything.


With a vector, you can create 'copies' of an object - as many copies as you like in fact, using push_back(). a vector always starts out empty, but push_back will grow a vector by 1 in size using a copy in memory of whatever object you pass to it.
    vector<Add> numbers;
    Add object;
    int a,b;

    cout << "Enter two numbers: ";
    cin >> a >> b;
    object.set( a,b );
    numbers.push_back( object );

    cout << "Enter two more numbers: ";
    cin >> a >> b;
    object.set( a,b );
    numbers.push_back( object );

    cout << "Enter another two numbers: ";
    cin >> a >> b;
    object.set( a,b );
    numbers.push_back( object );

    cout << "first:  "   << numbers.at(0).get()
         << "\nsecond: " << numbers.at(1).get()
         << "\nthird:  " << numbers.at(2).get()
         << endl;
output:
Enter two numbers: 1 2
Enter two more numbers: 3 4
Enter another two numbers: 5 6
first:  3
second: 7
third:  11 


Notice the output statement
    cout << "first:  "   << numbers.at(0).get()
         << "\nsecond: " << numbers.at(1).get()
         << "\nthird:  " << numbers.at(2).get() 
numbers is the name of the vector.
numbers.at(X) means "reach the object stored at position X" - which is an Add. anything you can do with an Add object, you can also do with numbers.at(X) - such as .get().

When a vector is "grown" using push_back, the copy of the object you add is inserted to the next available position - a vector which has 2 stored objects will have those stored at positions 0 and 1 (computers always count from zero!). a newly inserted object will appear at position 2.



edit: changed to stop emoticons appearing in code


Thanks alot this should help me. Thanks for explaining that cleary
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1