刚入门C++,请问下面这个问题怎么解决,下面是源代码
#include<iostream>usingnamespacestd;classBook{private:charbookname;doubleprice;intnum...
#include<iostream>using namespace std;class Book{private: char bookname; double price; int number;public: void book(char a,double b,int c) { this->bookname=a; this->price=b; this->number=c; } void dispaly() { cout<<bookname<<" "<<price<<" "<<number<<endl; } void borrow() { this->number--; } void restore() { this->number++; }}int main(){ char name[20]="C++"; Book book1(name,23.5,3); strcpy(name,"Data Structure"); Book book2(name,28.8,7); book1.borrow(); book1.display(); book2.restore(); book2.display(); return 0;}
展开
4个回答
展开全部
同意以上代码
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<iostream>
using namespace std;
class Book
{
private:
string bookname;
double price;
int number;
public:
Book(string a,double b,int c)
{
this->bookname=a;
this->price=b;
this->number=c;
}
void display()
{
cout<<bookname<<" "<<price<<" "<<number<<endl;
}
void borrow()
{
this->number--;
}
void restore()
{
this->number++;
}
};
int main()
{
string name = "C++";
Book book1(name,23.1,3);
name+="Data Structure";
//strcpy(name,"Data Structure");
Book book2(name,28.8,7);
book1.borrow();
book1.display();
book2.restore();
book2.display();
cout << name << endl;
return 0;
}
记得给个赞同、推荐
更多追问追答
追答
#include<iostream>
#include <cstring>
using namespace std;
class Book
{
private:
char * bookname;
double price;
int number;
public:
Book(char * a,double b,int c)
{
this->bookname=a;
this->price=b;
this->number=c;
}
void display()
{
cout<<bookname<<" "<<price<<" "<<number<<endl;
}
void borrow()
{
this->number--;
}
void restore()
{
this->number++;
}
};
int main()
{
char name[20] = "C++";
Book book1(name,23.1,3);
//name+="Data Structure";
strcpy(name,"Data Structure");
Book book2(name,28.8,7);
book1.borrow();
book1.display();
book2.restore();
book2.display();
//cout << name << endl;
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<iostream>
#include<cstring>
using namespace std;
class Book
{
public:
Book(char *nm,float pr,int num):price(pr),number(num)
{
bookname=new char[strlen(nm)+1];
strcpy(bookname,nm);
}
~Book()
{
if(bookname)
delete [] bookname;
}
void borrow()
{
if(number>0)
{
number--;
cout<<number<<endl;
}
else
cout<<"当前存书数量已为0"<<endl;
}
void restore()
{
number++;
cout<<number<<endl;
}
void display() const
{
cout<<bookname<<" "<<price<<" "<<number<<endl;
}
private:
char *bookname;
float price;
int number;
};
int main()
{
char name[20]="C++";
Book book1(name,23.5,3);
strcpy(name,"Data Structure");
Book book2(name,28.8,7);
book1.borrow();
book1.display();
book2.restore();
book2.display();
return 0;
}
#include<cstring>
using namespace std;
class Book
{
public:
Book(char *nm,float pr,int num):price(pr),number(num)
{
bookname=new char[strlen(nm)+1];
strcpy(bookname,nm);
}
~Book()
{
if(bookname)
delete [] bookname;
}
void borrow()
{
if(number>0)
{
number--;
cout<<number<<endl;
}
else
cout<<"当前存书数量已为0"<<endl;
}
void restore()
{
number++;
cout<<number<<endl;
}
void display() const
{
cout<<bookname<<" "<<price<<" "<<number<<endl;
}
private:
char *bookname;
float price;
int number;
};
int main()
{
char name[20]="C++";
Book book1(name,23.5,3);
strcpy(name,"Data Structure");
Book book2(name,28.8,7);
book1.borrow();
book1.display();
book2.restore();
book2.display();
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
有如下错误:
①每个打印(print)函数少了右大括号}。
②定义的字符串要初始化。
③Postgrad p(d);Data d(na);Student s(i);Teacher t(sa);这句话中的Data d(na)的对象d的名称与字符串变量重名
①每个打印(print)函数少了右大括号}。
②定义的字符串要初始化。
③Postgrad p(d);Data d(na);Student s(i);Teacher t(sa);这句话中的Data d(na)的对象d的名称与字符串变量重名
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询