Vector

Hi . I created a class and in main program i use a vector like this .
how can i Access to class method in main program ?????
class AAA
{
private:
int a
public:
int getA() { return a ;}

};
int main()
{
vector<AAA>* test= new vector<AAA>();

}


closed account (zb0S216C)
Like this:

1
2
3
test->push_back( AAA( ) );

std::cout << test->at( 0 ).getA( ) << std::endl;

Wazzak
Hi .first tnx for help . i created this class , but when i want to show output my program show just last item ....
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class Driver
{
private:
string name;

public:
void setDriver() ;
string getDriver() const;


};

void Driver::setDriver()
{
cout << "\n Please Enter Name : ";
cin >> name;

}

string Driver::getDriver() const
{
cout << "\n Name : " << name;

}

int main()
{
const int size = 2;
Driver dri;
vector<Driver>* driver = new vector<Driver>();
for ( int i = 0 ; i < size ; i++)
dri.setDriver();
driver->push_back(dri);

for ( int i = 0 ; i < driver->size(); i++)
{
driver->at(i).getDriver();

}
return 0;
}


closed account (zb0S216C)
You need to re-write your first for loop, preferably, like this:

1
2
3
4
5
for( int i( 0 ), i < size; i++ )
{
    driver->push_back( Driver( ) );
    driver->at( i ).setDriver( );
}

Here, you're adding a new driver, and assigning the drivers name with each pass of the loop.

Wazzak
thank youuuuuuuuuuuuuuuu

Topic archived. No new replies allowed.