C++编程题,在线求解!!!
/*
1、定义一个媒体Media类
2、它有两个派生类:图书Book类、光盘CD类
3、其中,图书有名称、销售数量、ISBN编号等属性
4、光盘有名称、销售数量、制作方等属性
5、要求:
1)设计上述三个类的相关属性;
2)利用多态性的概念,分别以虚函数的形式完成对图书和光盘信息的显示
3)编写一个普通函数f(),显示出销售数量超过1000的图书和光盘
4)在main函数中进行测试。
*/
#include <vector>
#include <string>
#include <iostream>
using namespace std;
class Media
{
public:
Media(){};
~Media(){};
virtual void Init(string name, int number)
{
m_Name = name;
m_Number = number;
};
virtual int GetNumber()
{
return m_Number;
}
virtual void Display()
{
cout<<"name: "<<m_Name<<endl;
cout<<"namber: "<<m_Number<<endl;
}
protected:
string m_Name;
int m_Number;
};
class Book : public Media
{
public:
Book(){};
~Book(){};
void Init(string name, int number, string ISBN)
{
Media::Init(name, number);
m_ISBN = ISBN;
};
void Display()
{
Media::Display();
cout<<"ISBN: "<<m_ISBN<<endl;
}
private:
string m_ISBN;
};
class CD : public Media
{
public:
CD(){};
~CD(){};
void Init(string name, int number, string Author)
{
Media::Init(name, number);
m_Author = Author;
};
void Display()
{
Media::Display();
cout<<"author: "<<m_Author<<endl;
}
private:
string m_Author;
};
int main()
{
// 添加书籍和光盘
Book *b1 = new Book();
Book *b2 = new Book();
Book *b3 = new Book();
b1->Init("book1", 100, "ISBN100");
b2->Init("book2", 1000, "ISBN1000");
b3->Init("book3", 10000, "ISBN10000");
CD *cd1 = new CD;
CD *cd2 = new CD;
CD *cd3 = new CD;
cd1->Init("cd1", 100, "author100");
cd2->Init("cd2", 1000, "author1000");
cd3->Init("cd3", 10000, "author10000");
vector<Media *> vectMedia;
vectMedia.push_back(b1);
vectMedia.push_back(b2);
vectMedia.push_back(b3);
vectMedia.push_back(cd1);
vectMedia.push_back(cd2);
vectMedia.push_back(cd3);
// 显示部分(销售数量大于1000)
for (int i=0; i<vectMedia.size(); i++)
{
if (vectMedia[i]->GetNumber() >= 1000)
{
cout<<"》》》销售量大于1000的书籍或光盘《《《"<<endl;
vectMedia[i]->Display();
}
}
return 0;
}
// 运行效果见图示
/*
图书信息:
名 称: VC++深入详解
销售量: 560000
ISBN: 978-7-121-17360-8
CD信息:
名 称: 民乐金品
销售量: 200000
生产商: 乐海楼
Press any key to continue
*/
#include <iostream>
#include <string>
using namespace std;
class Media {
private :
string name;
unsigned sales_volume;
public :
Media(string nm,unsigned sales);
// void Set(string nm,unsigned sales);
virtual Show()const = 0; // 纯虚函数
virtual ~Media() {}
};
class Book : public Media {
private :
string ISBN;
public :
Book(string nm = "",unsigned sales = 0,string isbn = "");
virtual Show()const;
virtual ~Book() {}
};
class CD : public Media {
private :
string producer;
public :
CD(string nm = "",unsigned sales = 0,string producer = "");
virtual Show()const;
virtual ~CD() {}
};
// Media construction and methods
Media::Media(string nm,unsigned sales) {
name = nm;
sales_volume = sales;
}
Media::Show() const {
cout << "名 称: " << name << endl;
cout << "销售量: " << sales_volume << endl;
}
// Book construction and Methods
Book::Book(string nm,unsigned sales,string isbn) : Media(nm,sales) {
ISBN = isbn;
}
Book::Show() const {
Media::Show();
cout << "ISBN: " << ISBN << endl;
}
// CD construction and Methods
CD::CD(string nm,unsigned sales,string producer) : Media(nm,sales) {
this->producer = producer;
}
CD::Show() const {
Media::Show();
cout << "生产商: " << producer << endl;
}
int main() {
Book Love("VC++深入详解",560000,"978-7-121-17360-8");
CD Like("民乐金品",200000,"乐海楼");
cout << "图书信息:\n";
Love.Show();
cout << "\nCD信息:\n";
Like.Show();
return 0;
}
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class Media{
public:
virtual void show() const=0;
virtual string GetName() const=0;
virtual int GetSaleNumber() const=0;
virtual ~Media()=0;
};
Media::~Media(){};
class Book:public Media{
public:
Book(const char* n,const int& number,const char* SN):name(n),SaleNumber(number),ISBN(SN){}
void show()const { cout<<"The book's name:["<<name<<"]\nSale number:"<<SaleNumber<<"\nISBN:"<<ISBN<<endl;}
string GetName() const{ return name;}
int GetSaleNumber() const{ return SaleNumber;}
private:
string name;
int SaleNumber;
string ISBN;
};
class CD:public Media{
public:
CD(const char* n,const int& number,const char* m):name(n),SaleNumber(number),maker(m){}
void show()const { cout<<"The CD's name:["<<name<<"]\nSale number:"<<SaleNumber<<"\nMaker:"<<maker<<endl;}
string GetName() const{ return name;}
int GetSaleNumber() const{ return SaleNumber;}
private:
string name;
int SaleNumber;
string maker;
};
void f(Media* tmp)
{
if(tmp->GetSaleNumber()>1000)
cout<<"["<<tmp->GetName()<<"]"<<endl;
}
int main()
{
Book test1("花开半夏",1000000,"IS-97-32-DS-21");
CD test2("ENDLESS STORY",1000000,"JAPAN");
Media* a[]={&test1,&test2};
for(int i=0;i<2;i++)
a[i]->show();
cout<<"销量超过1000的作品:"<<endl;
for(int i=0;i<2;i++)
f(a[i]);
return 0;
}
满意采纳吧!
#include <iostream>
#include <assert.h>
#include <cstring>
using namespace std;
#define ISBN_LEN 17
#define MAX_PATH 255
class Media{
protected:
char _name[MAX_PATH + 1];
unsigned int _num;
public:
Media(const char *name, unsigned int num);
unsigned int getNum(){
return _num;
}
Media();
virtual void print() const {}
};
class Book: public Media{
private:
char _ISBN[ISBN_LEN + 1];
public:
virtual void print() const;
Book(const char *name, unsigned int num, const char* isbn);
};
class CD: public Media{
private:
char _producer[MAX_PATH + 1];
public:
virtual void print() const;
CD(const char *name, unsigned int num, const char *producer);
};
Media::Media(const char *name, unsigned int num){
assert(name);
strncpy(_name, name, MAX_PATH);
_num = num;
}
Book::Book(const char *name, unsigned int num, const char *isbn): Media(name, num){
assert(isbn);
strncpy(_ISBN, isbn, ISBN_LEN);
}
CD::CD(const char *name, unsigned int num, const char *producer): Media(name, num){
assert(producer);
strncpy(_producer, producer, MAX_PATH);
}
void Book::print() const{
cout<<"Book: "
<<"Name: "<<_name<<"\tNumber: "
<<_num<<"\tISBN: "<<_ISBN<<endl;
}
void CD::print() const{
cout<<"CD: "
<<"Name: "<<_name<<"\tNumber: "
<<_num<<"\tproducer: "<<_producer<<endl;
}
void p(Media *media[], int num){
assert(media);
int index = 0;
for(; index < num; index++){
if (media[index]->getNum() > 1000){
media[index]->print();
}
}
}
int main()
{
Media *media[2];
media[0] = new CD("XX", 10, "you two");
media[0]->print();
media[1] = new Book("OO", 1001, "three of u");
media[1]->print();
p(media, 2);
return 0;
}
用media指向两个子类,再用media调用他们会自己找到自己应该调用的print,这是体现了多态性
#include <iostream>
#include <string.h>
class Media
{
public:
Media(){}
~Media(){}
virtual void showInfo(){}
public:
std::string m_name;
int m_saleNum;
};
class Book : public Media
{
public:
Book(std::string name,int sale,std::string isbn){
m_name = name;
m_saleNum = sale;
m_isbn = isbn;
}
~Book(){}
virtual void showInfo(){
std::cout <<"name: " << m_name.c_str() <<", sale: " << m_saleNum << ", isbn: " << m_isbn.c_str() << std::endl;
}
private:
std::string m_isbn;
};
class CD : public Media
{
public:
CD(std::string name,int sale,std::string producer){
m_name = name;
m_saleNum = sale;
m_producer = producer;
}
~CD(){}
virtual void showInfo(){
std::cout <<"name: " << m_name.c_str() <<", sale: " << m_saleNum << ", producer: " << m_producer.c_str() << std::endl;
}
private:
std::string m_producer;
};
void f(Media *p)
{
if(p->m_saleNum > 1000)
p->showInfo();
}
int main(int argr,char *argv[])
{
Media *test1 = new Book("花开半夏",100,"IS-97-32-DS-21");
Media *test2 = new CD("ENDLESS STORY",1000000,"JAPAN");
f(test1);
f(test2);
return 0;
}