- 论坛徽章:
- 0
|
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
Name iano 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 编辑 ] |
|