免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1573 | 回复: 3
打印 上一主题 下一主题

[C++] 类中字符串赋值 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-05-07 13:55 |只看该作者 |倒序浏览
a.h头文件
#ifndef A_H_
#define A_H_
class Cd
{
private:
        static const int NUM=50;
        char performers[NUM];
        char label[NUM];
        int  selections;
        double playtime;
public:
        Cd(const char * s1,const char * s2,int n,double x);
        Cd(const Cd & d);
        Cd();
        ~Cd();
        virtual void Report()const;
        Cd & operator=(const Cd & d);

};
class Classic :public Cd
{
  private:
         static const int NUM1=10;
         char Name[NUM1];
  public:
        Classic(const char * s1,const char * s2,const char *s3,int a,double b);
        virtual void Report()const;

};
#endif
#include<iostream>
using namespace std;
#include"a.h"
#include<cstring>


Cd::Cd(const char * s1,const char * s2,int n,double x)
{
   strncpy(performers,s1,strlen(s1));
   performers[strlen(s1)+1]='\0';
   strcpy(label,s2);
   selections=n;
   playtime=x;
}
Cd::Cd(const Cd & d)
{
    for(int i=0;i<NUM;i++)
    {
      performers=d.performers;
      label=d.label;
    }
    selections=d.selections;
    playtime=d.playtime;
}
Cd::Cd()
{
  strcpy(performers,"NULL";
  strcpy(label,"NULL";
  selections=0;
  playtime=0;

}
Cd::~Cd()
{
  cout<<"invoke"<<endl;
}
void Cd::Report()const
{
   cout<<"performances:"<<performers<<endl;
   cout<<"label:"<<label<<endl;
   cout<<selections<<endl;
   cout<<playtime<<endl;
}
            cout<<playtime<<endl;
}
Cd & Cd:perator=(const Cd & d)
{
     for(int i=0;i<NUM;i++)
    {
      performers=d.performers;
      label=d.label;
    }
    selections=d.selections;
    playtime=d.playtime;
}

Classic::Classic(const char * s0,const char * s1,const char *s2,int n,double x):Cd(s1,s2,n,x)
{
   strcpy(Name,s0);

}
void Classic::Report()const
{
   cout<<"Name:"<<Name<<endl;
}

驱动程序:#include<iostream>
using namespace std;
#include"a.h"
//void Bravo(const Cd & disk);
int main()
{
  Cd c1("Beatles","Captiol",14,35.5);
  c1.Report();
  Classic c2=Classic("iano Sonata in B flat,Fantasia in C","alfred brendel","philips",2,57.17);
  Cd *pcd=&c1;
  cout<<"using object directly:"<<endl;
  c1.Report();
  c2.Report();
  cout<<"using type cd * pointer to objects"<<endl;
  //pcd->Report();
  //pcd=&c2;
// pcd->Report();
cout<<"calling a function with a Cd reference argument"<<endl;
//  Bravo(c1);
//  Bravo(c2);
//  cout<<"testing assignment";
  return 0;

}
//void Bravo(const Cd & disk)
//{
//   disk.Report();
//}
结果::
label:Captiol
14
35.5
using object directly:
performances:B flat,Fantasia in C 这个出错了???
label:Captiol
14
35.5
Nameiano Sonata in B flat,Fantasia in C ???
using type cd * pointer to objects
calling a function with a Cd reference argument
invoke
invoke


[ 本帖最后由 xueyuyanghf2006 于 2009-5-7 14:03 编辑 ]

论坛徽章:
3
金牛座
日期:2014-06-14 22:04:062015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:45
2 [报告]
发表于 2009-05-07 15:26 |只看该作者
发这种贴,要么用[code][/code]括起来,要么禁止smile吧,太难看了

论坛徽章:
0
3 [报告]
发表于 2009-05-07 15:27 |只看该作者

回复 #2 dreamice 的帖子

[code][/code]括起来也要禁smile,不然还会出现的。

论坛徽章:
0
4 [报告]
发表于 2009-05-07 15:32 |只看该作者
楼上的改签名了,装B的已经消失了吗?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP