64,664
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <cstring>
using namespace std;
// ³ö°æÎï
class CPublication{
private:
char * p_title;
float price;
char date[20];
bool on_loan;
public:
CPublication(char *p_t, float p, char *p_d);
void set(char *p_t, float p, char *p_d);
bool borrowIt();
bool returnIt();
void print()const;
~CPublication() { delete [] p_title; }
};
CPublication::CPublication(char *p_t=NULL, float p=0.0, char *p_d=NULL){
p_title = new char [strlen(p_t)+1];
strcpy (p_title, p_t);
price = p;
strcpy (date, p_d);
on_loan = true;
}
void CPublication::set(char *p_t, float p, char *p_d){
delete [] p_title;
p_title = new char [strlen(p_t)+1];
strcpy (p_title, p_t);
price = p;
strcpy (date, p_d);
on_loan = true;
}
bool CPublication::borrowIt(){
if (on_loan == true){
on_loan = false;
return true;
}
else{
cout << "The book has been gone through." << endl;
return false;
}
}
bool CPublication::returnIt(){
on_loan = true;
return true;
}
void CPublication::print()const{
cout << "ÊéÃû£º" << p_title << endl;
cout << "¼Û¸ñ£º" << price << endl;
cout << "³ö°æÈÕÆÚ£º" << date << endl;
if (on_loan == true)
cout << "¿É¹©½è³ö" << endl;
else
cout << "Òѽè³ö" << endl;
}
int main(){
CPublication shu("ÓïÎĿα¾", 45.0, "2008.10.6");
shu.borrowIt();
shu.returnIt();
shu.print();
system("pause");
return 0;
}
出错在这:
CPublication::CPublication(char *p_t=NULL, float p=0.0, char *p_d=NULL){
p_title = new char [strlen(p_t)+1]; //这里strlen的参数应该是p_t
strcpy (p_title, p_t);
price = p;
strcpy (date, p_d);
on_loan = true;
}
class CPublication{
private:
char * p_title;
float price;
char date[20];
bool on_loan;
public:
CPublication(char *p_t, float p, char *p_d);
void set(char *p_t, float p, char *p_d);
bool borrowIt();
bool returnIt();
void print()const;
~CPublication() { delete [] p_title; }
};
#include <iostream>
#include <cstring>
using namespace std;
CPublication::CPublication(char *p_t=NULL, float p=0.0, char *p_d=NULL){
p_title = new char [strlen(p_t)+1]; // p_t写成p_title了,改过来就好了
strcpy (p_title, p_t);
price = p;
strcpy (date, p_d);
on_loan = true;
}
void CPublication::set(char *p_t, float p, char *p_d){
delete [] p_title;
p_title = new char [strlen(p_t)+1];
strcpy (p_title, p_t);
price = p;
strcpy (date, p_d);
on_loan = true;
}
bool CPublication::borrowIt(){
if (on_loan == true){
on_loan = false;
return true;
}
else{
cout << "The book has been gone through." << endl;
return false;
}
}
bool CPublication::returnIt(){
on_loan = true;
return true;
}
void CPublication::print()const{
cout << "书名:" << p_title << endl;
cout << "价格:" << price << endl;
cout << "出版日期:" << date << endl;
if (on_loan == true)
cout << "可供借出" << endl;
else
cout << "已借出" << endl;
}
int main(){
CPublication shu("语文课本", 45.0, "2008.10.6");
shu.borrowIt();
shu.returnIt();
shu.print();
system("pause");
return 0;
}