c++ 定义一个书的class,简单题目,写好代码,求大神指正细节,十分感谢。 30
代码已经编译和运行过了,但是因为是大一新手,刚学编程,不是很懂,老师的题目又要求很复杂,所以求细心的大神给看看代码,尤其是一些细节,跪谢,题目要求如下:Inyourpro...
代码已经编译和运行过了,但是因为是大一新手,刚学编程,不是很懂,老师的题目又要求很复杂,所以求细心的大神给看看代码,尤其是一些细节,跪谢,题目要求如下:
In your program, define a class named book that has the following behaviours:
(定义一个叫图书的类,要用到如下方法:)
voidsetName(stringbookName);stringgetName();voidsetPageNum(intpNum);int getPageNum();
Your program should create four books and set and display their details from an appropriate main program. State is left to the writer.
(你的程序要建立四本书,用个适当的主程序设置和显示他们的具体的一些属性,属性可议自己设定。)
This is worth 10 marks:
下面是评分点:
1 for clear and appropriate comments,
清楚和适当的注释1分
4 for correct definition of the book class behaviours, (you get 1 mark for each one that's defined with a body)
正确定义书的类和方法,每个1分。
2 for the correct functions for name handling, (1 mark for each IF they work)
正确的函数针对设定书名的,一个1分,要求运行正常。
2 for the correct functions for page number handling, (1 mark for each IF they work)
正确的函数针对设定页数的,一个1分,要求运行正常。
1 for overall style, legibility and layout of your code. (Code that does not compile will not receive comments or style marks.)
书写代码格式什么的1分。
代码如下:
#include <iostream>
#include <vector>
// String set-up
#include <string>
#include <sstream>
// Need stdlib for random numbers.
#include <stdlib.h>
using namespace std;
class Book //define Book
{
public:
Book();
Book(string, int, string);
void setName();
string getName();
void setPageNum();
int getPageNum();
void setwriterName();
string getwriterName();
private:
string bookName;
int pNum;
string writerName;
};
// Here are the method bodies
Book::Book() :bookName("Not set"), pNum( 0 ), writerName("Not set") {}
Book::Book(string aName, int aPNum, string aWName):bookName(aName), pNum(aPNum),writerName(aWName) {}
void Book::setName()
{
cout << "setting!";
}
void Book::setPageNum()
{
cout << 6 << pNum;
}
void Book::setwriterName()
{
cout << "setting!";
}
string Book::getName()
{
return bookName;
}
int Book::getPageNum()
{
return pNum;
}
string Book::getwriterName()
{
return writerName;
}
//Now let's create a library using a vector.
int main()
{
vector<Book> library(3);
library.at(0) = Book("book1", 24, "H");
library.at(1) = Book("book2", 67, "Y");
library.at(2) = Book("book3", 78, "W");
library.at(3) = Book("book4", 125, "Z");
for (int i = 0; i < 3; i++)
{
cout << library.at(i).getName() << endl;
cout << library.at(i).getPageNum() << endl;
cout << library.at(i).getwriterName() << endl;
}
system("pause");
}
运行结果如下:
上面贴的有个地方错了,自己已经修正了:
int main()
{
vector<Book> library(4);
library.at(0) = Book("book1", 24, "H");
library.at(1) = Book("book2", 67, "Y");
library.at(2) = Book("book3", 78, "W");
library.at(3) = Book("book4", 125, "Z");
for (int i = 0; i < 4; i++) 展开
In your program, define a class named book that has the following behaviours:
(定义一个叫图书的类,要用到如下方法:)
voidsetName(stringbookName);stringgetName();voidsetPageNum(intpNum);int getPageNum();
Your program should create four books and set and display their details from an appropriate main program. State is left to the writer.
(你的程序要建立四本书,用个适当的主程序设置和显示他们的具体的一些属性,属性可议自己设定。)
This is worth 10 marks:
下面是评分点:
1 for clear and appropriate comments,
清楚和适当的注释1分
4 for correct definition of the book class behaviours, (you get 1 mark for each one that's defined with a body)
正确定义书的类和方法,每个1分。
2 for the correct functions for name handling, (1 mark for each IF they work)
正确的函数针对设定书名的,一个1分,要求运行正常。
2 for the correct functions for page number handling, (1 mark for each IF they work)
正确的函数针对设定页数的,一个1分,要求运行正常。
1 for overall style, legibility and layout of your code. (Code that does not compile will not receive comments or style marks.)
书写代码格式什么的1分。
代码如下:
#include <iostream>
#include <vector>
// String set-up
#include <string>
#include <sstream>
// Need stdlib for random numbers.
#include <stdlib.h>
using namespace std;
class Book //define Book
{
public:
Book();
Book(string, int, string);
void setName();
string getName();
void setPageNum();
int getPageNum();
void setwriterName();
string getwriterName();
private:
string bookName;
int pNum;
string writerName;
};
// Here are the method bodies
Book::Book() :bookName("Not set"), pNum( 0 ), writerName("Not set") {}
Book::Book(string aName, int aPNum, string aWName):bookName(aName), pNum(aPNum),writerName(aWName) {}
void Book::setName()
{
cout << "setting!";
}
void Book::setPageNum()
{
cout << 6 << pNum;
}
void Book::setwriterName()
{
cout << "setting!";
}
string Book::getName()
{
return bookName;
}
int Book::getPageNum()
{
return pNum;
}
string Book::getwriterName()
{
return writerName;
}
//Now let's create a library using a vector.
int main()
{
vector<Book> library(3);
library.at(0) = Book("book1", 24, "H");
library.at(1) = Book("book2", 67, "Y");
library.at(2) = Book("book3", 78, "W");
library.at(3) = Book("book4", 125, "Z");
for (int i = 0; i < 3; i++)
{
cout << library.at(i).getName() << endl;
cout << library.at(i).getPageNum() << endl;
cout << library.at(i).getwriterName() << endl;
}
system("pause");
}
运行结果如下:
上面贴的有个地方错了,自己已经修正了:
int main()
{
vector<Book> library(4);
library.at(0) = Book("book1", 24, "H");
library.at(1) = Book("book2", 67, "Y");
library.at(2) = Book("book3", 78, "W");
library.at(3) = Book("book4", 125, "Z");
for (int i = 0; i < 4; i++) 展开
1个回答
展开全部
首先我不是大神!大概看了一下,不知道理解的对不对啊。
你的setName,setPageNum,setwriterName貌似写的不对啊!setXXX()函数的意思是让你对对象的某些属性进行修改。而不是输出他的属性值。
我觉得正确的大概是:
void Book::setName(string BookName){
if(BookName != NULL){
this.bookName = BookName;
}
}
其他的setXXX的函数应该类似吧。不明白可以再交流啊。
望给分!o(∩_∩)o
你的setName,setPageNum,setwriterName貌似写的不对啊!setXXX()函数的意思是让你对对象的某些属性进行修改。而不是输出他的属性值。
我觉得正确的大概是:
void Book::setName(string BookName){
if(BookName != NULL){
this.bookName = BookName;
}
}
其他的setXXX的函数应该类似吧。不明白可以再交流啊。
望给分!o(∩_∩)o
追问
谢谢,我试了,好像this 有点不对,
(BookName != NULL 这个也不对,他说string 不能变成int什么的
还有bookName和BookName有什么区别?。。。
追答
对的,c++中NULL只用来和指针判断,最近用的是java,这一块有点混了啊!对不起。
bookName是你当前对象的属性,而BookName是setXXX函数的实参。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询