#include #include #include class student { public: student(); //default constructor student(const std::string& name, const int age); //copy constructor student(const student& rhs); //surcharge constructor ~student(); //destructor void setName(const std::string& name); std::string getName() const; void setAge(const int age); int getAge() const; student& operator=(const student& rhs); private: std::string pName; //instance of std::string class int pAge; }; student::student(): pName("New student"), pAge(20) {} student::student(const std::string& name, const int age): pName(name), pAge(age) {} student::student(const student& rhs): pName(rhs.getName()), pAge(rhs.getAge()) {} student::~student() {} void student::setName(const std::string& name) { pName = name; } std::string student::getName() const { return pName; } void student::setAge(int age) { pAge = age; } int student::getAge() const { return pAge; } student& student::operator=(const student& rhs) { pName = rhs.getName(); pAge = rhs.getAge(); return *this; } std::ostream& operator<<(std::ostream& os, const student& rhs) { os << rhs.getName() << " has " << rhs.getAge() << " years old."; return os; } //display vector properties template void display_vector(const std::vector& v); typedef std::vector classRoom; int main() { student Jean; student Michel("Michel", 15); student Martine("Martine", 20); student Pierre("Pierre", 18); classRoom emptyClassRoom; std::cout << "emptyClassRoom:" << std::endl; display_vector(emptyClassRoom); classRoom increasedClassRoom(3); std::cout << "increasedClassRoom(3):" << std::endl; display_vector(emptyClassRoom); increasedClassRoom[0] = Jean; increasedClassRoom[1] = Martine; increasedClassRoom[2] = Pierre; std::cout << "increasedClassRoom(3):" << std::endl; display_vector(emptyClassRoom); increasedClassRoom.push_back(Pierre); std::cout << "increasedClassRoom() after append of 4th student:" << std::endl; display_vector(emptyClassRoom); std::cout << "increasedClassRoom() after setName / setAge:" << std::endl; display_vector(emptyClassRoom); increasedClassRoom[0].setName("Jean"); increasedClassRoom[0].setAge(18); system("pause"); return 0; } template void display_vector(const std::vector& v) { std::cout << "max_size() = " << v.max_size() << "\tsize() = " << v.size() << "\tcapacity() = " << v.capacity << "\t" << (v.empty()? "empty": "not empty") << std::endl; for (unsigned int i = 0; i < v.size(); i++) std::cout << v[i] << std::endl; std::cout << std::endl; }