![]() |
#2
qq10235692232011-03-05 09:48
//book.h
#include<iostream> using namespace std; //定义book类 class book { char *bookname; double price; int number; public: book(char *a, double b, int c); //你的构造函数的参数怎么可以赋值? ~book(); void display(); void borrow(); void restore(); void set(char *e, double f, int g); }; //book.cpp,定义函数 #include<iostream> #include"book.h" using namespace std; book::book(char *a, double b, int c) { bookname=new char[40]; bookname=a; price=b; number=c; } book::~book() { bookname=""; } //delete只能删除用new申请的内存,你确定所有的bookname都是new申请的? void book::display() { cout<<"bookname:"<<bookname<<endl <<"price:"<<price<<endl <<"number"<<number<<endl; } void book::borrow() { number--; cout<<"number:"<<number<<endl; } void book::restore() { number++; cout<<"number:"<<number<<endl; } void book::set(char *e, double f, int g) { bookname=e; price=f; number=g; } //main.cpp #include<iostream> #include"book.cpp" using namespace std; int main() { int h; book A; A.display(); A.borrow(); cout<<"What are going to do?"<<endl; cin>>h; switch(h) { case 0: { char *e=new char[50]; double f; int g; cout<<"name:"; cin>>e; cout<<"price:"; cin>>f; cout<<"number:"; cin>>g; A.set(e,f,g); } break; case 1: break; case 2: break; default: { cout<<"error"<<endl; return -1; break; } } return 0; } //还有你的文件之间的相互包含关系要注意,哪一个前哪一个在后要分清! |
好头痛啊,俺是新手,求朋友们帮帮忙
下面是我写的程序,定义一个book类存储书籍信息,编译成功,但是在运行时却好像发生了错误,运行的最后没有像以前那样显示:“请按任意键继续”,而且发出‘嘭’的一声提示音,好像是发生了什么错误,然后关不掉窗口,只能是点窗口右上角的叉强行关掉它,不明白为什么啊,希望有朋友能帮帮忙
注:应该是switch那个地方错了,但我怎么改也是那样没变化
//book.h
#include<iostream>
using namespace std;
//定义book类
class book
{
char*bookname;
double price;
int number;
public:
book(char *a="nobook", double b=0, int c=0);
~book();
void display();
void borrow();
void restore();
void set(char *e, double f, int g);
};
//book.cpp,定义函数
#include<iostream>
#include"aa.h"
using namespace std;
book::book(char *a, double b, int c)
{bookname=new char[40];bookname=a;price=b;number=c;}
book::~book()
{delete []bookname;bookname=NULL;}
void book::display()
{
cout<<"bookname:"<<bookname<<endl
<<"price:"<<price<<endl
<<"number"<<number<<endl;
}
void book::borrow()
{
number--;
cout<<"number:"<<number<<endl;
}
void book::restore()
{
number++;
cout<<"number:"<<number<<endl;
}
void book::set(char *e, double f, int g)
{bookname=e;price=f;number=g;}
//main.cpp
#include<iostream>
#include"aa.h"
using namespace std;
int main()
{
int h;
book A;
A.display();
A.borrow();
cout<<"What are going ti do?"<<endl;
cin>>h;
switch(h)
{
case 0:
{
char* e=new char[50];double f;int g;
cout<<"name:"<<endl;
cin>>e;
cout<<"price"<<endl;
cin>>f;
cout<<"number:"<<endl;
cin>>g;
A.set(e,f,g);
}
case 1:
{break;}
case 2:
{break;}
default:
{cout<<"error"<<endl;return -1;}
}
}