64,685
社区成员
发帖
与我相关
我的任务
分享
using namespace std;
void displayInfo();
class MEDIA
{
protected:
int ID;
string Title;
int Year;
public:
MEDIA()
{
ID=0;
Title="";
Year=0;
}
virtual ~MEDIA(){}
virtual void changeID(int)=0;
virtual void Print(ostream&)const=0;
friend ostream& operator <<(ostream&,const MEDIA&);
};
ostream& operator <<(ostream out,const MEDIA& M)
{
M.Print(out);
return out;
}
class DVD:public MEDIA
{
private:
string Director;
public:
DVD(int newID,const char* newTitle,int newYear,const char* name="Unknown")
{
ID=newID;
Title=newTitle;
Year=newYear;
Director=name;
}
virtual void changeID(int newID)
{
ID=newID;
}
virtual void Print(ostream& out)const
{
out<<"DVD: ID"<<ID<<"\tTitle "<<Title<<"\t("<<Year<<")\tDirected by "<<Director<<endl;
}
};
class Book:public MEDIA
{
private:
string Author;
int NumPage;
public:
Book(int newID,const char* newTitle,int newYear,const char* name,int page)
{
ID=newID;
Title=newTitle;
Year=newYear;
Author=name;
NumPage=page;
}
virtual void changeID(int newID)
{
ID=newID;
}
virtual void Print(ostream& out)const
{
out<<"Book: ID"<<ID<<"\tTitle "<<Title<<"\t("<<Year<<")\t by "<<Author<<",\t"<<NumPage<<" pages"<<endl;
}
};
class Journal:public MEDIA
{
private:
int Volumn;
int Number;
public:
Journal(int newID,const char* newTitle,int newYear,int newVol,int num)
{
ID=newID;
Title=newTitle;
Year=newYear;
Volumn=newVol;
Number=num;
}
virtual void changeID(int newID)
{
ID=newID;
}
virtual void Print(ostream& out)const
{
out<<"Journal: ID"<<ID<<"\tTitle "<<Title<<"\t("<<Year<<")\t Volumn: "<<Volumn<<",\tNumber"<<Number<<endl;
}
};
int main()
{
MEDIA *ptr[10];
ptr[0] = new DVD(352, "Remember The Alamo", 1945, "George Smith");
ptr[1] = new DVD(831, "High School Blues", 1984);
ptr[2] = new DVD(194, "Going For The Touchdown", 1984, "Frank Madden");
ptr[3] = new DVD(576, "Martian Hairdresser", 1992, "Debbie Gold");
ptr[4] = new Book(608,"How to Make Money", 1987, "Phil Barton", 324);
ptr[5] = new Book(442,"Garden Projects At Home", 1998, "Mary Freeman", 164);
ptr[6] = new Book(185,"The Haunted House Mystery", 1996, "Bert Morgan", 53);
ptr[7] = new Journal(294, "ACM", 2009, 6, 8);
ptr[8] = new Journal(521, "J of Logic", 2008, 23, 14);
ptr[9] = new Journal(630, "J of AI", 2009, 35, 11);
cout << "Printing 10 items..." << endl << endl;
for (int i = 0; i < 10; ++i)
{
cout << *ptr[i] << endl;
}
ptr[3]->changeID(707);
ptr[5]->changeID(808);
ptr[7]->changeID(909);
cout << endl << "Printing again..." << endl << endl;
cout << *ptr[3] << endl;
cout << *ptr[5] << endl;
cout << *ptr[7] << endl;
return 0;
}
{
protected:
int ID;
string Title;
int Year;
public:
MEDIA()
{
ID=0;
Title="";
Year=0;
}
virtual ~MEDIA(){}
virtual void changeID(int)=0;
virtual void Print(ostream&)const=0;
friend ostream& operator <<(ostream&,const MEDIA&);// here , ostream &
};
ostream& operator <<(ostream out,const MEDIA& M)// 没有 &
{
M.Print(out);
return out;
}