CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  C/C++ >  C++ 语言

各位大虾来帮帮忙啊!!(痛哭流涕)

楼主gcyds2001(捣蛋)2004-12-04 14:18:58 在 C/C++ / C++ 语言 提问

小弟近日看到类继承这部分内容,实在是迷惘,请大家指教。  
  另外下面有个程序,是练习题,题型是补充题。程序我只是凭自己怎么想就怎么补的。请大家看看有什么毛病,谢谢拉。  
  //创建"pt.h",这是课本上的题目给出的程序。  
  #ifndef   PT_H_  
  #define   PT_H_  
  #include   <iostream>  
  using   namespace   std;  
  class   Port  
  {  
  private:  
  char*   brand;  
  char     style[20];  
  int       bottles;  
  public:  
  Port(const   char*   br   =   "none",const   char*   st   =   "none",int   b   =   0);  
  Port(const   Port   &   p);  
  virtual   ~Port(){   delete   []   brand;}  
  Port   &   operator=(const   Port   &   p);  
  Port   &   operator+=(int   b);  
  Port   &   operator-=(int   b);  
  int   BottleCount()const{   return   bottles;}  
  virtual   void   Show()const;  
  friend   ostream   &   operator<<(ostream   &   os,const   Port   &   p);  
  };  
   
  class   VintagePort:public   Port  
  {  
  private:  
  char*   nickname;  
  int   year;  
  public:  
  VintagePort();  
  VintagePort(const   char*   br,int   b,const   char*   nn,int   y);  
  VintagePort(const   VintagePort   &   vp);  
  ~VintagePort(){   delete   []   nickname;}  
  VintagePort   &   operator=(const   VintagePort   &   vp);  
  void   Show()const;  
  friend   ostream   &   operator<<(ostream   &   os,const   VintagePort   &   vp);  
  };  
   
  //给方法定义,是我补充的。pt.cpp  
  #include   "pt.h"  
  #include   <cstring>  
   
  Port::Port(const   char*   br,const   char*   st,int   b)  
  {  
  brand   =   new   char[strlen(br)   +   1];  
  strcpy(brand,br);  
  strncpy(style,st,19);  
  style(19)   =   '\0';  
  bottles   =   b;  
  }  
  Port::Port(const   Port   &   p)  
  {  
  brand   =   new   char[strlen(p.brand)   +   1];  
  strcpy(brand,p.brand);  
  strncpy(style,p.style,19);  
  bottles   =   p.bottles;  
  }  
  Port   &   Port::operator=(const   Port   &   p)  
  {  
  if(this   ==   &p)  
  return   *   this;  
  delete   []   brand;  
  brand   =   new   char[strlen(p.brand)   +   1];  
  strcpy(brand,p.brand);  
  strncpy(style,p.style,19);  
  bottles   =   p.bottles;  
  return   *   this;  
  }  
  Port   &   Port::operator+=(int   b)  
  {  
  bottles+=b;  
  }  
  Port   &   Port::operator-=(int   b)  
  {  
  bottles-=b;  
  }  
  void   Port::Show()const  
  {  
  cout<<"Brand:   "<<   brand<<endl;  
  cout<<"Kind:   "<<style<<endl;  
  cout<<"Bottles:   "<<   bottles<<endl;  
  }  
  ostream   &   operator<<(ostream   &   os,const   Port   &   p)  
  {  
  os   <<p.brand<<",   "<<p.style<<",   "<<p.bottles;  
  return   os;  
  }  
   
  VintagePort::VintagePort():Port(){}  
  VintagePort::VintagePort(const   char*   br,int   b,const   char*   nn,int   y):Port(br,b)  
  {  
  nickname   =   new   char[strlen(nn)   +   1];  
  strcpy(nickname,nn);  
  year   =   y;  
  }  
  VintagePort::VintagePort(const   VintagePort   &   vp):Port(vp)  
  {  
  nickname   =   new   char[strlen(vp.nickname)   +   1];  
  strcpy(nickname,vp.nickname);  
  year   =   vp.year;  
  }  
  VintagePort   &   VintagePort::operator=(const   VintagePort   &   vp)  
  {  
  if(this   ==   &   vp)  
  return   *   this;  
  Port::operator=(vp);  
  nickname   =   new   char[strlen(vp.nickname)   +   1];  
  strcpy(nickname,vp.nickname);  
  return   *   this;  
  }  
  void   VintagePort::Show()const  
  {  
  cout<<"Brand:   "<<   brand<<endl;  
  cout<<"Kind:   "<<style<<endl;  
  cout<<"Bottles:   "<<   bottles<<endl;  
  cout<<"Nickname:   "<<nickname<<endl;  
  cout<<"Year:   "<<year<<endl;  
  }  
  ostream   &   operator<<(ostream   &   os,const   VintagePort   &   vp)  
  {  
  os<<(const   Port   &)vp;  
  os<<"Brand:   "<<brand<<"Kind:   "<<style<<"Bottles:   "  
  <<"Nickname:   "<<nickname<<"Year:   "<<year;  
  return   os;  
  }  
  //这是我另外补充的main()。usept.cpp  
  #include   <iostream>  
  using   namespace   std;  
  #include   "pt.h"  
  int   main()  
  {  
  Port   pt("Gallo",tawny,20);  
  VintagePort   ptj("Glo",38);  
  cout<<pt<<endl;  
  cout<<ptj<<endl;  
  return   0;  
  }  
   
   
  我在vc++.net2003里编译时,出现了两个错误:  
  fatal   error   C1070:   文件“c:\documents   and   settings\gcy\my   documents\visual   studio   projects\ \pt.h  
  pt.cpp  
   
  fatal   error   C1070:   文件“c:\documents   and   settings\gcy\my   documents\visual   studio   projects\ \pt.h  
  正在生成代码...  
  问题点数:100、回复次数:8Top

1 楼carylin(林石)回复于 2004-12-04 14:43:29 得分 0

n个错误,自己改改好了Top

2 楼kypfos(不在寻梦)回复于 2004-12-04 15:36:31 得分 0

错误太多了,还是从简单一点的来吧,如没有#endif,参数没有定义,调用方法错等等Top

3 楼UPCC(杂食动物)回复于 2004-12-04 17:01:21 得分 100

1:最好在#include   <cstring>后加   using   namespace   std;(可能没有影响)  
  2:style(19)   =   '\0';是错误的,style是数组不是函数,改成style[19]   =   '\0';  
  3:派生类的函数要访问基类的数据,所以基类的数据成员要在protected声明里。(当然不要public)  
  4:  
  ostream   &   operator<<(ostream   &   os,const   VintagePort   &   vp)  
  {  
  os<<(const   Port   &)vp;  
  os<<"Brand:   "<<brand<<"Kind:   "<<style<<"Bottles:   "  
  <<"Nickname:   "<<nickname<<"Year:   "<<year;  
  return   os;  
  }  
  每一个变量都是VintagePort   的。所以要改成vp.brand,vp.style,vp.nickname,vp.year  
   
  5:在面里要为tawny定义  
   
  6:VintagePort::VintagePort(const   char*   br,int   b,const   char*   nn,int   y):Port(br,b)  
  中的变量b是int类型的,要改成char*类型的,比如nn变量  
   
  7:  
  main()里的VintagePort   ptj("Glo",38);是错误的,因为你的VintagePort没有这样的构造函数  
  可以这样的VintagePort   ptj("Glo",38,tawny,2);  
  8:  
  Port   &   Port::operator+=(int   b)  
  {  
  bottles+=b;  
  return   *this;  
  }  
  Port   &   Port::operator-=(int   b)  
  {  
  bottles-=b;  
  return   *this;  
  }  
  这两个函数要求返会值  
   
  9:这一点最重要的。  
  在定义类Port前面加上  
  class   Port;  
  ostream   &   operator<<(ostream   &   os,const   Port   &   p);  
  在定义类VintagePort前面加上  
  class   VintagePort;  
  ostream   &   operator<<(ostream   &   os,const   VintagePort   &   vp);  
  这是因为编译器接口和函数原型的问题  
   
   
  下面是我修改后编译通过的代码,(没有意义的),你根据我告诉你的应该注意的地方自己改进有意义的信息  
   
  #include   <iostream>  
  #include   <cstring>  
  using   namespace   std;  
  class   Port;  
  ostream   &   operator<<(ostream   &   os,const   Port   &   p);  
  class   Port  
  {  
  protected:  
  char*   brand;  
  char     style[20];  
  int       bottles;  
  public:  
  Port(const   char*   br   =   "none",const   char*   st   =   "none",int   b   =   0);  
  Port(const   Port   &   p);  
  virtual   ~Port(){   delete   []   brand;}  
  Port   &   operator=(const   Port   &   p);  
  Port   &   operator+=(int   b);  
  Port   &   operator-=(int   b);  
  int   BottleCount()const{   return   bottles;}  
  virtual   void   Show()const;  
  friend   ostream   &   operator<<(ostream   &   os,const   Port   &   p);  
  };  
  class   VintagePort;  
  ostream   &   operator<<(ostream   &   os,const   VintagePort   &   vp);  
  class   VintagePort:public   Port  
  {  
  private:  
  char*   nickname;  
  int   year;  
  public:  
  VintagePort();  
  VintagePort(const   char*   br,int   b,const   char*   nn,int   y);  
  VintagePort(const   VintagePort   &   vp);  
  ~VintagePort(){   delete   []   nickname;}  
  VintagePort   &   operator=(const   VintagePort   &   vp);  
  void   Show()const;  
  friend   ostream   &   operator<<(ostream   &   os,const   VintagePort   &   vp);  
  };  
  Port::Port(const   char*   br,const   char*   st,int   b)  
  {  
  brand   =   new   char[strlen(br)   +   1];  
  strcpy(brand,br);  
  strncpy(style,st,19);  
  style[19]   =   '\0';  
  bottles   =   b;  
  }  
  Port::Port(const   Port   &   p)  
  {  
  brand   =   new   char[strlen(p.brand)   +   1];  
  strcpy(brand,p.brand);  
  strncpy(style,p.style,19);  
  bottles   =   p.bottles;  
  }  
  Port   &   Port::operator=(const   Port   &   p)  
  {  
  if(this   ==   &p)  
  return   *   this;  
  delete   []   brand;  
  brand   =   new   char[strlen(p.brand)   +   1];  
  strcpy(brand,p.brand);  
  strncpy(style,p.style,19);  
  bottles   =   p.bottles;  
  return   *   this;  
  }  
  Port   &   Port::operator+=(int   b)  
  {  
  bottles+=b;  
  return   *this;  
  }  
  Port   &   Port::operator-=(int   b)  
  {  
  bottles-=b;  
  return   *this;  
  }  
  void   Port::Show()const  
  {  
  cout<<"Brand:   "<<   brand<<endl;  
  cout<<"Kind:   "<<style<<endl;  
  cout<<"Bottles:   "<<   bottles<<endl;  
  }  
  ostream   &   operator<<(ostream   &   os,const   Port   &   p)  
  {  
  os   <<p.brand<<",   "<<p.style<<",   "<<p.bottles;  
  return   os;  
  }  
   
  VintagePort::VintagePort():Port(){}  
  VintagePort::VintagePort(const   char*   br,int   b,const   char*   nn,int   y):Port(br,nn)  
  {  
  nickname   =   new   char[strlen(nn)   +   1];  
  strcpy(nickname,nn);  
  year   =   y;  
  }  
  VintagePort::VintagePort(const   VintagePort   &   vp):Port(vp)  
  {  
  nickname   =   new   char[strlen(vp.nickname)   +   1];  
  strcpy(nickname,vp.nickname);  
  year   =   vp.year;  
  }  
  VintagePort   &   VintagePort::operator=(const   VintagePort   &   vp)  
  {  
  if(this   ==   &   vp)  
  return   *   this;  
  Port::operator=(vp);  
  nickname   =   new   char[strlen(vp.nickname)   +   1];  
  strcpy(nickname,vp.nickname);  
  return   *   this;  
  }  
  void   VintagePort::Show()const  
  {  
  cout<<"Brand:   "<<   brand<<endl;  
  cout<<"Kind:   "<<style<<endl;  
  cout<<"Bottles:   "<<   bottles<<endl;  
  cout<<"Nickname:   "<<nickname<<endl;  
  cout<<"Year:   "<<year<<endl;  
  }  
  ostream   &   operator<<(ostream   &   os,const   VintagePort   &   vp)  
  {  
  os<<(const   Port   &)vp;  
  os<<"Brand:   "<<vp.brand<<"Kind:   "<<vp.style<<"Bottles:   "  
  <<"Nickname:   "<<vp.nickname<<"Year:   "<<vp.year;  
  return   os;  
  }  
  int   main()  
  {  
  char*   tawny   =   "eee";  
  Port   pt("Gallo",tawny,20);  
  VintagePort   ptj("Glo",38,tawny,2);  
  cout<<pt<<endl;  
  cout<<ptj<<endl;  
  return   0;  
  }  
  Top

4 楼goodluckyxl(被人遗忘的狗)回复于 2004-12-04 17:01:41 得分 0

只有一个字  
   
  晕   有时间给你调调吧Top

5 楼greenteanet(扎扎实实打基础,保持一颗平常心。)回复于 2004-12-04 17:11:23 得分 0

UPCC(杂食动物)   说的太详细啦Top

6 楼Flood1984(峰子)回复于 2004-12-04 17:35:06 得分 0

杂食动物  
  挺佩服你的,看的这么详细  
  编译器啊你Top

7 楼clin003(走尽天涯路)回复于 2004-12-04 17:55:40 得分 0

UPCC(杂食动物)   好人撒Top

8 楼gcyds2001(捣蛋)回复于 2004-12-04 18:37:53 得分 0

多谢UPCC(杂食动物)!  
  同样谢谢大家。  
  Top

相关问题

  • 千刀万剐,小偷黑心贼;痛哭流涕,我的文曲星!!!为它哀吊3秒,呜呼哀哉...谨防小偷!!!
  • 偶有一个好东东,<中华诗词库>,收录了上万首诗词(古代的现代的),支持分类和检索,以后写情书一定有用!从里面找出几首情诗出来,让你的MM感动的痛哭流涕!!,
  • 烤鸡翅膀问:如何得到wParam的高位和低位?已经控件ID,如何断定它是EDIT?请朋友帮忙啊,我好可怜啊,痛哭流涕状态中...
  • ◆◆◆◆◆◆●已经上传完成★★★★★★偶有一个好东东,<中华诗词库>,收录了上万首诗词(古代的现代的),支持分类和检索,以后写情书一定有用!从里面找出几首情诗出来,让你的MM感动的痛哭流涕!!,
  • 请教各位各位!!
  • 请问各位?
  • 各位小心
  • 各位大哥.....
  • 各位老大:
  • 各位累吗?

关键词

  • bottles
  • brand
  • port
  • strncpy
  • ostream
  • operator
  • pt
  • 类
  • 程序
  • style

得分解答快速导航

  • 帖主:gcyds2001
  • UPCC

相关链接

  • C/C++ Blog
  • C/C++类图书
  • C/C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo