今天下午体测 GGGGGGGGGGGG
//第一题
//main.cpp
#include "cd.h"
void Bravo(const Cd &disk);
int main()
{
Cd c1("Beatles", "Capitol", 14, 35.5);
Classic c2 = Classic("Piano Sonata in Bflat, Fantasia in C", "Alfred Brendel", "Philips", 2, 57.17);
Cd *pcd = &c1;
std::cout << "Using object directly:\n";
c1.Report();
c2.Report();
std::cout << "Using type cd *pointer to object:\n";
pcd->Report();
pcd = &c2;
pcd->Report();
std::cout << "Testing assignment: \n";
Classic copy;
copy = c2;
copy.Report();
return 0;
}
void Bravo(const Cd &disk)
{
disk.Report();
}
//cd.h
#ifndef CD_H_
#define CD_H_
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
class Cd
{
private:
char performers[50];
char label[20];
int selections;
double playtime;
public:
Cd(char *s1, 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:
char name[100];
public:
Classic(char *na, char *s1, char *s2, int n, double x) : Cd(s1, s2, n, x) { std::strcpy(name, na); }
Classic(char *na, const Cd &d) : Cd(d) { std::strcpy(name, na); }
Classic(const Classic &c);
Classic() {}
virtual void Report() const;
Classic &operator=(const Classic &c);
};
#endif
//cd.cpp
#include "cd.h"
Cd::Cd(char * s1, char * s2, int n, double x)
{
std::strcpy(performers, s1);
std::strcpy(label, s2);
selections = n;
playtime = x;
}
Cd::Cd(const Cd & d)
{
std::strcpy(performers, d.performers);
std::strcpy(label, d.label);
selections = d.selections;
playtime = d.playtime;
}
void Cd::Report() const
{
std::cout << "performers: " << performers << std::endl;
std::cout << "label: " << label << std::endl;
std::cout << "selections: " << selections << std::endl;
std::cout << "playtime: " << playtime << std::endl;
}
Cd & Cd::operator=(const Cd & d)
{
if (this == &d)
return *this;
std::strcpy(performers, d.performers);
std::strcpy(label, d.label);
selections = d.selections;
playtime = d.playtime;
return *this;
}
Classic::Classic(const Classic & c) : Cd(c)
{
std::strcpy(name, c.name);
}
void Classic::Report() const
{
std::cout << "name: " << name << std::endl;
Cd::Report();
}
Classic & Classic::operator=(const Classic & c)
{
if (this == &c)
return *this;
Cd::operator=(c);
std::strcpy(name, c.na