#pragma once
class Library
{
Publication* publications[100]; //所有馆藏,假设馆藏上限为 100
int total; //实际馆藏数
public:
Library();
void add(Publication& p); //添加新馆藏(此函数需完成)
void show();// //输出所有馆藏的相应信息
};
//此函数可用于输出所有馆藏的相应信息
// 如: C++ zhengli
// Thames 2010/1/1
// DuZhe 2011/5/1 第 10 期
//注意不同种类出版物会输出不同种类的信息。
Library::Library():total(0)
{
fill(publications, publications+100, nullptr);
}
void Library::add(Publication& p)
{
publications[total++] = &p;
}
void Library::show()
{
for (int i = 0; i < total; i++)
publications[i]->print();
cout << endl;
}
#pragma once
class Publication
{
public:
virtual ~Publication() {};
virtual void print() = 0;
};
#pragma once
class Book :public Publication
{
private:
string name;
string authorName;
public:
Book();
Book(string _n, string _an = "Unknown");
~Book();
void print();
};
Book::Book() :name("Unknown"), authorName("Unknown"){}
Book::Book(string _n, string _an ) :name(_n), authorName(_an){}
Book::~Book(){}
void Book::print()
{
cout << name << ' ' << authorName << endl;
}
#pragma once
class Newspaper :public Publication
{
private:
string name;
string publishTime;
public:
Newspaper();
Newspaper(string _n, string _pt = "Unknown");
~Newspaper();
void print();
};
Newspaper::Newspaper() :name("Unknown"), publishTime("Unknown") {}
Newspaper::Newspaper(string _n, string _pt ) :name(_n), publishTime(_pt) {}
Newspaper::~Newspaper() {}
void Newspaper::print()
{
cout << name << ' ' << publishTime << endl;
}
#pragma once
class Magzine :public Publication
{
private:
string name;
string publishTime;
string number;
public:
Magzine();
Magzine(string _n, string _pt = "Unknown", string _nm = "Unknown");
~Magzine();
void print();
};
Magzine::Magzine() :name("Unknown"), publishTime("Unknown"), number("Unknown") {};
Magzine(string _n, string _pt , string _nm )
:name(_n), publishTime(_pt), number(_nm)
{}
Magzine::~Magzine(){}
void Magzine::print()
{
cout << name << ' ' << publishTime << ' ' << number << endl;
}
#include
#include
#include
#include
using namespace std;
#include"Publication.h"
#include"Book.h"
#include"Newspaper.h"
#include"Magzine.h"
#include"Library.h"
int main()
{
}
成员私有
不知道你这个问题是否已经解决, 如果还没有解决的话:void TestString1()
{
String s1;
String s2("hello world");
String s3(s2);
s1 = s3;
cout << s1 << endl;
cout << s2 << endl;
cout << s3 << endl;
}
void TestString2()
{
String s1("hello");
s1.PushBack(' ');
s1.PushBack('1');
s1.Append("31");
s1 += '4';
cout << s1 << endl;
cout << s1.Size() << endl;
cout << s1.Capacity() << endl;
// 利用迭代器打印string中的元素
auto it = s1.Begin();
while (it != s1.End())
{
cout << *it++;
}
cout << endl;
String s2("hello world!!!");
s1.Swap(s2);
cout << s1 << endl;
cout << s2 << endl;
}
void TestString3()
{
String s("hello");
cout << s << endl;
cout << s.Size() << endl;
cout << s.Capacity() << endl;
s.Resize(10, 'a');
cout << s << endl;
cout << s.Size() << endl;
cout << s.Capacity() << endl;
s.Resize(20);
cout << s << endl;
cout << s.Size() << endl;
cout << s.Capacity() << endl;
s.Resize(5);
cout << s << endl;
cout << s.Size() << endl;
cout << s.Capacity() << endl;
s.Reserve(50);
cout << s << endl;
cout << s.Size() << endl;
cout << s.Capacity() << endl;
}
void TestString4()
{
String s1("abcdefg");
String s2 = s1;
cout << s1 << endl;
s1.Insert(2, 'h');
cout << s1 << endl;
s1.Insert(2, "123");
cout << s1 << endl;
s2.Erase(2, 5);
cout << s2 << endl;
}
void TestString5()
{
String s1 = "abcdef";
String s2 = "abcdh";
cout << (s1 < s2) << endl;
cout << (s1 > s2) << endl;
cout << (s1 == s2) << endl;
cout << (s1 != s2) << endl;
cout << (s1 >= s2) << endl;
cout << (s1 <= s2) << endl;
}
int main()
{
cout << "Test1: 默认成员函数" << endl;
TestString1();
cout << endl;
cout << "Test2: Iterators + Modifiers" << endl;
TestString2();
cout << endl;
cout << "Test3: Capacity" << endl;
TestString3();
cout << endl;
cout << "Test4: Modifiers" << endl;
TestString4();
cout << endl;
cout << "Test5: 比较" << endl;
TestString5();
cout << endl;
return 0;
}