دوست عزیز،
من برنامه شما را به صورت زیر تغییر دادم و در
BDS 2006
اجرا شد باید در
Visual C++
هم اجرا شود. اگر استاندارد نوشته نشده به علت کپی کردن در این وبسایت است.
کد:
#include<conio.h>
#include<iostream>
#include<map>
#include<string>
usingnamespace std;
class Student
{
public:
Student(): itsName("new student"), itsAge(16)
{}
Student(const string & str,constint & ci): itsName(str), itsAge(ci)
{}
Student(const Student & rhs):itsName(rhs.GetName()),itsAge(rhs.GetAge())
{}
Student & operator = (const Student & rhs)
{
itsAge = rhs.GetAge();
itsName = rhs.GetName();
return * this;
}
void SetName(string str)
{
itsName = str;
}
void SetAge(int age)
{
itsAge = age;
}
string GetName() const
{
return itsName;
}
int GetAge() const
{
return itsAge;
}
private:
int itsAge;
string itsName;
};
ostream & operator << (ostream & os,Student & st)
{
os <<st.GetName()<<" is "<<st.GetAge()<<" years old\n";
return os;
}
template <class T,class A>
void ShowMap(const map<T,A> & alk)
{
for(map<T,A>::const_iterator ci = alk.begin();ci != alk.end(); ++ci)
{
Student stu = ci->second;
cout << ci->first <<" : "<< stu;
}
cout <<endl;
}
void main()
{
Student Mahdi("Mahdi",16);
Student Abolfazl("Abolfazl",14);
Student Bird("bird",12);
Student Mohammad("Mohammad",17);
map<string,Student> MathClass;
MathClass[Mahdi.GetName()] = Mahdi;
MathClass[Abolfazl.GetName()] = Abolfazl;
MathClass[Bird.GetName()] = Bird;
MathClass[Mohammad.GetName()] = Mohammad;
ShowMap(MathClass);
cout <<MathClass["Mahdi"].GetName()<<" is "<<MathClass["Mahdi"].GetAge()
<<" years old\n";
_getch();
}