急~用C++的高手帮忙一下
设计一个基类base为抽象类,其中包含gettitle()和printtitle()两个成员函数,另有一个纯虚函数isgood()。由该类派生Book和Journal两个...
设计一个基类base为抽象类,其中包含gettitle()和printtitle()两个成员函数,另有一个纯虚函数isgood()。由该类派生Book和Journal两个类,分别实现纯函数isgood()对于前者版本,如果每月图书销售量超过500,则返回True;对于后者版本,如果每月杂志销售量超过2500,则返回True。
展开
展开全部
你没有说清楚gettitle和printtitle的作用呢,下面按你要求写的,不知道是不是你所说的那样
class base
{
public:
void gettitle(){}
void printtitle(){}
virtual bool isgood() = 0;
};
class Book : public base
{
public:
bool isgood()
{
if (sold > 500)
{
return true
}
return false;
}
private:
int sold;
};
class Journal : public base
{
public:
bool isgood()
{
if (sold > 2500)
{
return true
}
return false;
}
private:
int sold;
};
class base
{
public:
void gettitle(){}
void printtitle(){}
virtual bool isgood() = 0;
};
class Book : public base
{
public:
bool isgood()
{
if (sold > 500)
{
return true
}
return false;
}
private:
int sold;
};
class Journal : public base
{
public:
bool isgood()
{
if (sold > 2500)
{
return true
}
return false;
}
private:
int sold;
};
追问
感谢你的回答~gettitle和printtitle应该是书的资料(可能是书的书名)的录入和输出吧~我大概猜的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <iostream>
using namespace std;
class base
{
private:
int sales;
public:
base()
{
cout<<"base"<<endl;
}
base(int t)
{
sales = t;
cout<<"base sales"<<endl;
}
int gettitle()
{
return sales;
}
void printtitle()
{
cout<<sales<<endl;;
}
virtual bool isgood() = 0;
};
class Book:public base
{
public:
Book(int t):base(t)
{
cout<<"Book sales"<<endl;
}
bool isgood()
{
if (gettitle() >= 500)
{
cout<<"Book sales return true"<<endl;
return true;
}
return false;
}
};
class Journal:public base
{
public:
Journal(int t):base(t)
{
cout<<"Journal sales"<<endl;
}
bool isgood()
{
if (gettitle() >= 2500)
{
cout<<"Journal sales return true"<<endl;
return true;
}
return false;
}
};
void main()
{
base *a;
Book b(600);
a = &b;
a->isgood();
Journal c(3600);
a = &c;
a->isgood();
system("pause");
}
using namespace std;
class base
{
private:
int sales;
public:
base()
{
cout<<"base"<<endl;
}
base(int t)
{
sales = t;
cout<<"base sales"<<endl;
}
int gettitle()
{
return sales;
}
void printtitle()
{
cout<<sales<<endl;;
}
virtual bool isgood() = 0;
};
class Book:public base
{
public:
Book(int t):base(t)
{
cout<<"Book sales"<<endl;
}
bool isgood()
{
if (gettitle() >= 500)
{
cout<<"Book sales return true"<<endl;
return true;
}
return false;
}
};
class Journal:public base
{
public:
Journal(int t):base(t)
{
cout<<"Journal sales"<<endl;
}
bool isgood()
{
if (gettitle() >= 2500)
{
cout<<"Journal sales return true"<<endl;
return true;
}
return false;
}
};
void main()
{
base *a;
Book b(600);
a = &b;
a->isgood();
Journal c(3600);
a = &c;
a->isgood();
system("pause");
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
注意纯虚函数定义成virtual bool isgood() = 0;
这样就OK了,其他的都很基础!
另外,你让写的是伪码,楼上写的不错。
这样就OK了,其他的都很基础!
另外,你让写的是伪码,楼上写的不错。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <iostream>
#include <string>
using namespace std;
class base{
string title;
int sales;
public:
base(string str,int s=0){title=str;sales=s;}
string gettitle(){return title;}
void printtitle(){cout<<title;}
int getsales(){return sales;}
virtual bool isgood(){return true;}
};
class Book:public base{
public:
Book(string str,int s=0):base(str,s){}
bool isgood(){return getsales()>500;}
};
class Journal:public base{
public:
Journal(string str,int s=0):base(str,s){}
bool isgood(){return getsales()>2500;}
};
void main()
{
base *p[2];
p[0]=new Book("abc",600);
p[1]=new Journal("xyz",2000);
if(p[0]->isgood())
cout<<"abc is good."<<endl;
else
cout<<"abc isn't good."<<endl;
if(p[1]->isgood())
cout<<"xyz is good."<<endl;
else
cout<<"xyz isn't good."<<endl;
delete p[0];
delete p[1];
}
#include <string>
using namespace std;
class base{
string title;
int sales;
public:
base(string str,int s=0){title=str;sales=s;}
string gettitle(){return title;}
void printtitle(){cout<<title;}
int getsales(){return sales;}
virtual bool isgood(){return true;}
};
class Book:public base{
public:
Book(string str,int s=0):base(str,s){}
bool isgood(){return getsales()>500;}
};
class Journal:public base{
public:
Journal(string str,int s=0):base(str,s){}
bool isgood(){return getsales()>2500;}
};
void main()
{
base *p[2];
p[0]=new Book("abc",600);
p[1]=new Journal("xyz",2000);
if(p[0]->isgood())
cout<<"abc is good."<<endl;
else
cout<<"abc isn't good."<<endl;
if(p[1]->isgood())
cout<<"xyz is good."<<endl;
else
cout<<"xyz isn't good."<<endl;
delete p[0];
delete p[1];
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询