dynamic memory allocation of class objects
I am not able to add data on run time (dynamic allocation of memory) and then read that data and print all of the objects of class on screen!
can some one help please?
these are the functions i am using to store and display data!
1 2 3 4 5 6 7 8 9 10 11 12 13
|
void studentAdd() {
stds = new Students;
stds->Students::addStudent();
stds++;
}
void studentShow() {
int num=0;
int total = stds->Students::totalStds();
for (int i=0;i<total;i++) {
stds->Students::showStudent();
}
}
| |
class member functions are!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
void Students::addStudent() {
cout << "\n\n\tStudent ID:\t"<< stdId;
cout << "\n\tStudent Name:\t";
cin >> stdName;
cout << "\tStudent Age:\t";
cin >> stdAge;
cout << "\tStudent Class:\t";
cin >> stdClass;
cout << "\tTrnspt [1,0]:\t";
cin >> stdTransport;
cout << "\tHostel [1,0]:\t";
cin >> stdHostel;
stdExists=1;
}
void Students::showStudent() {
cout << "\n\n\tStudent ID:\t"<< stdId;
cout << "\n\tStudent Name:\t"<< stdName;
cout << "\n\tStudent Age:\t"<< stdAge;
cout << "\n\tStudent Class:\t"<< stdClass;
cout << "\n\tTrnspt [1,0]:\t"<< stdTransport;
cout << "\n\tHostel [1,0]:\t"<< stdHostel;
}
| |
Here is the complete code!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
class Students {
private:
int static countStdId;
int stdId;
string stdName;
int stdAge;
string stdClass;
int stdTransport;
int stdHostel;
int stdExists;
public:
Students(); // constructor declaration
~Students(); // destructor declaration
void addStudent();
void showStudent();
int checkExt() { return stdExists; }
int totalStds() { return countStdId; }
};
int Students::countStdId=0; // static variable
Students * stds;
//////////////////////////////////////////
// Constructor function
//////////////////////////////////////////
Students::Students() {
stdId=++countStdId;
stdAge=0;
stdTransport=0;
stdHostel=0;
stdExists=0;
}
//////////////////////////////////////////
// Destructor
//////////////////////////////////////////
Students::~Students() {
}
//////////////////////////////////////////
// Students :: Add New Student
//////////////////////////////////////////
void Students::addStudent() {
cout << "\n\n\tStudent ID:\t"<< stdId;
cout << "\n\tStudent Name:\t";
cin >> stdName;
cout << "\tStudent Age:\t";
cin >> stdAge;
cout << "\tStudent Class:\t";
cin >> stdClass;
cout << "\tTrnspt [1,0]:\t";
cin >> stdTransport;
cout << "\tHostel [1,0]:\t";
cin >> stdHostel;
stdExists=1;
}
void Students::showStudent() {
cout << "\n\n\tStudent ID:\t"<< stdId;
cout << "\n\tStudent Name:\t"<< stdName;
cout << "\n\tStudent Age:\t"<< stdAge;
cout << "\n\tStudent Class:\t"<< stdClass;
cout << "\n\tTrnspt [1,0]:\t"<< stdTransport;
cout << "\n\tHostel [1,0]:\t"<< stdHostel;
}
void studentAdd() {
// int num=0;
// cout << "\n\n\tEnter number of students you wish to add: ";
// cin >> num;
//int total = stds->Students::totalStds();
stds = new Students;
stds->Students::addStudent();
stds++;
// for (int i=total-1;i<num;i++) {
// stds[i].Students::addStudent();
// }
studentMenu(2); // call students menu with successfully added message
}
void studentShow() {
int num=0;
int total = stds->Students::totalStds();
for (int i=0;i<total;i++) {
stds->Students::showStudent();
}
}
| |
take a look at <
http://www.cplusplus.com/doc/tutorial/dynamic/ >
and <
http://www.cplusplus.com/doc/tutorial/arrays/ >
there are a couple of problems, stds needs to be an array of (Students*), and memory needs to be allocated for that array:
1 2 3
|
Students* stds[ size_of_array ];
//or
Students** stds = new Students*[ size_of_array ];
| |
right now you are using stds as an array, but without allocating memory for it, and without using proper indexing to access items:
1 2
|
int index = some_number;
stds[ index ] = new Students();
| |
taking a look at the links above should help you fix your code. good luck!
p.s. - an alternative since you're using c++ is use an STL container to store pointers to your objects
Topic archived. No new replies allowed.