注册 登录
编程论坛 C++教室

用"类名"作为类型有什么实际作用?return 究竟返回的是什么东西?问题见注释。

jaytse 发布于 2014-01-09 01:18, 425 次点击
以下程序是网上的一个例子,我照着做,有几点不懂:
#include<iostream>
using namespace std;

class book
{
public:
int num;
float price;
book *next;   //这行不懂。
};
book *head=NULL;  //这行为什么要用book作为类型?
book *creat()  ////这行为什么要用book作为类型?把函数creat定义为指针有什么好处?
{
book*p1,*p2;  //这行为什么要用book作为类型?为什么不是int之类的?
p1=new book;
head=p1;
p2=p1;
cout<<"Please enter the number of the book,and end of '0':"<<endl;
cin>>p1->num;
if(p1->num!=0)
{
cout<<"Please enter the price of the book:"<<endl;
cin>>p1->price;
}
else
{
delete p1;p2=NULL;p2->next=NULL;head=NULL;
                return head;//这里的"return head"返回的究竟是什么?起什么作用?
}
while(p1->num!=0)
{
p2=p1;
p1=new book;
cout<<"Please enter the number of the book,and end of '0':"<<endl;
cin>>p1->num;
if(p1->num!=0)
{
cout<<"Please enter the price of the book:"<<endl;
cin>>p1->price;
}
p2->next=p1;
}
delete p1;
p2->next=NULL;
return head;//这里的"return head"返回的究竟是什么?起什么作用?
}
int main()
{
creat();
return 0;
}
2 回复
#2
yuccn2014-01-09 08:40
去学习下基础先吧,这个问题好像在问:
1+1=2

为什么 “1” ?
什么是+?为什么“+”?
什么是 “=”?

"2" 有什么好处?起什么作用?

陈景润都不知道怎么回答你的问题
#3
peach54602014-01-09 12:21
以下是引用yuccn在2014-1-9 08:40:49的发言:

去学习下基础先吧,这个问题好像在问:
1+1=2

为什么 “1” ?
什么是+?为什么“+”?
什么是 “=”?

"2" 有什么好处?起什么作用?

陈景润都不知道怎么回答你的问题

+1
1