R
Randy
Hi,
I am a noob. I am trying to store Student objects pointers, in a
vector, within a School object.
I call a setter function to add the pointer. If I use this method it
works,
Student st = Student("Randy");
Student * stp = & st;
sc.addStudent(stp);
My problem is passing a Student to the School function. I have an
overridden function. I try to create a pointer from the passed
Student. I bet that's my problem because it's a copy ?
This all came about because I want to have a more succinct method for
doing this, just like the big kids do:
Student st = Student("Randy");
Student * stp = & st;
sc.addStudent(stp);
I've read lots but am apperantly missing the point. I am referencing
the C++ Primer.
here's my output too ... 2 Bills ?? ahaha ...
Schools Constructor 1
Student Constructor2 Randy
Overload addStudent1()
Student Constructor2 Biff
Overload addStudent3()
Student Constructor2 Bill
Overload addStudent1()
*** List ***
1 Randy
3 Bill
3 Bill
*** EOF ***
Press any key to continue . . .
main.cpp
---------------------------------------------------------------------------------------
#include <cstdlib>
#include <iostream>
#include <vector>
// #include "Templates.h"
// #include "PointersAndReference.h"
#include "StudentsAndSchools.h"
using namespace std;
int main(int argc, char *argv[])
{
// mypair<int> ok(10,23); // Declare an INT class w/
construction
// ok.printValues();
// referenceTests();
School sc;
sc.setNames("Parkside");
Student st = Student("Randy");
Student * stp = & st;
sc.addStudent(stp);
sc.addStudent(Student("Biff"));
Student st1 = Student("Bill");
Student * stp1 = & st1;
sc.addStudent(stp1);
sc.getStudentNames();
system("PAUSE");
return EXIT_SUCCESS;
}
--------------- StudentsAndSchools.h
-------------------------------------------------------------
#ifndef __StudentsAndSchools__
#define __StudentsAndSchools__
using namespace std;
class School;
class Student {
private:
static int mLastId;
string mName;
int mId;
School * mSchool;
public:
Student(){
cout << "Student Constructor1" << endl;
}
Student(string n){
cout << "Student Constructor2 " << n << endl;
mName = n;
mId = ++mLastId;
}
void setName(string n);
string getName();
int getId();
};
int Student::mLastId = 0;
string Student::getName(){ return mName; };
int Student::getId(){ return mId; };
class School{
private:
string mName;
vector<Student*> mStudents;
public:
School(){
cout << "Schools Constructor 1" << endl;
}
void addStudent(Student* );
void addStudent(Student &); // overloaded
function
void addStudent(Student);
void setNames(string n);
void getStudentNames();
};
void School::setNames(string n){
mName = n;
}
void School::addStudent(Student* n){
cout << "Overload addStudent1()" << endl;
mStudents.push_back(n);
}
void School::addStudent(Student &n ){
cout << "Overload addStudent2()" << endl;
Student * s = & n;
mStudents.push_back(s);
}
void School::addStudent(Student n ){
cout << "Overload addStudent3()" << endl;
Student * s = & n;
mStudents.push_back(s);
}
void School::getStudentNames(){
cout << "*** List ***" << endl;
for(int i=0;i < mStudents.size(); i++)
{
cout << mStudents.at(i)->getId() << " " <<
mStudents.at(i)->getName()<<endl;
}
cout << "*** EOF ***" << endl;
}
#endif
I am a noob. I am trying to store Student objects pointers, in a
vector, within a School object.
I call a setter function to add the pointer. If I use this method it
works,
Student st = Student("Randy");
Student * stp = & st;
sc.addStudent(stp);
My problem is passing a Student to the School function. I have an
overridden function. I try to create a pointer from the passed
Student. I bet that's my problem because it's a copy ?
This all came about because I want to have a more succinct method for
doing this, just like the big kids do:
Student st = Student("Randy");
Student * stp = & st;
sc.addStudent(stp);
I've read lots but am apperantly missing the point. I am referencing
the C++ Primer.
here's my output too ... 2 Bills ?? ahaha ...
Schools Constructor 1
Student Constructor2 Randy
Overload addStudent1()
Student Constructor2 Biff
Overload addStudent3()
Student Constructor2 Bill
Overload addStudent1()
*** List ***
1 Randy
3 Bill
3 Bill
*** EOF ***
Press any key to continue . . .
main.cpp
---------------------------------------------------------------------------------------
#include <cstdlib>
#include <iostream>
#include <vector>
// #include "Templates.h"
// #include "PointersAndReference.h"
#include "StudentsAndSchools.h"
using namespace std;
int main(int argc, char *argv[])
{
// mypair<int> ok(10,23); // Declare an INT class w/
construction
// ok.printValues();
// referenceTests();
School sc;
sc.setNames("Parkside");
Student st = Student("Randy");
Student * stp = & st;
sc.addStudent(stp);
sc.addStudent(Student("Biff"));
Student st1 = Student("Bill");
Student * stp1 = & st1;
sc.addStudent(stp1);
sc.getStudentNames();
system("PAUSE");
return EXIT_SUCCESS;
}
--------------- StudentsAndSchools.h
-------------------------------------------------------------
#ifndef __StudentsAndSchools__
#define __StudentsAndSchools__
using namespace std;
class School;
class Student {
private:
static int mLastId;
string mName;
int mId;
School * mSchool;
public:
Student(){
cout << "Student Constructor1" << endl;
}
Student(string n){
cout << "Student Constructor2 " << n << endl;
mName = n;
mId = ++mLastId;
}
void setName(string n);
string getName();
int getId();
};
int Student::mLastId = 0;
string Student::getName(){ return mName; };
int Student::getId(){ return mId; };
class School{
private:
string mName;
vector<Student*> mStudents;
public:
School(){
cout << "Schools Constructor 1" << endl;
}
void addStudent(Student* );
void addStudent(Student &); // overloaded
function
void addStudent(Student);
void setNames(string n);
void getStudentNames();
};
void School::setNames(string n){
mName = n;
}
void School::addStudent(Student* n){
cout << "Overload addStudent1()" << endl;
mStudents.push_back(n);
}
void School::addStudent(Student &n ){
cout << "Overload addStudent2()" << endl;
Student * s = & n;
mStudents.push_back(s);
}
void School::addStudent(Student n ){
cout << "Overload addStudent3()" << endl;
Student * s = & n;
mStudents.push_back(s);
}
void School::getStudentNames(){
cout << "*** List ***" << endl;
for(int i=0;i < mStudents.size(); i++)
{
cout << mStudents.at(i)->getId() << " " <<
mStudents.at(i)->getName()<<endl;
}
cout << "*** EOF ***" << endl;
}
#endif