新手小白结合《C++ primer plus》1-13章制作了C++基础知识体系框架导图,拿来重新回顾C++的基础知识点或者复习大一的期末考试应该是没问题。因本人能力有限,不可避免可能会有少量错误,如发现,欢迎指正勘误,谢谢大家啦!
实例
//头文件
#ifndef CD_H_
#define CD_H_
//Brass Cd 类
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();
virtual ~Cd();
virtual void Report() const;
Cd & operator=(const Cd & d);
};
//Brass Classic 类
class Classic : public Cd
{
private:
char name[20];
public:
Classic(char * s1, char * s2, char * s3, int n, double x);
Classic(const Classic & d, char * c);
Classic(const Classic & d);
Classic();
virtual void Report() const;
Classic & operator=(const Classic & d);
};
#endif
//类的实现
#include <iostream>
#include <cstring>
#include "classic.h"
using std::cout;
using std::endl;
using std::strcpy;
//Cd 实现方法
Cd::Cd(char * s1, char * s2, int n, double x)
{
strcpy(performers, s1);
strcpy(label, s2);
selections = n;
playtime = x;
}
Cd::Cd(const Cd & d)
{
strcpy(performers, d.performers);
strcpy(label, d.label);
selections = d.selections;
playtime = d.playtime;
}
Cd::Cd()
{
performers[0] = '\0';
label[0] = '\0';
selections = 0;
playtime = 0;
}
Cd::~Cd()
{
}
void Cd::Report() const
{
cout << "performers: "<< performers << endl;
cout << "label: " << label << endl;
cout << "number of selections: " << selections << endl;
cout << "playing time in minutes: " << playtime << endl;
cout << endl;
}
Cd & Cd::operator=(const Cd & d)
{
if (this == &d)
return *this;
strcpy(performers, d.performers);
strcpy(label, d.label);
selections = d.selections;
playtime = d.playtime;
return *this;
}
//Classic 实现方法
//成员初始化列表
Classic::Classic(char * s1, char * s2, char * s3, int n, double x) : Cd(s1, s2, n, x)
{
strcpy(name, s3);
}
Classic::Classic(const Classic & d, char * c) : Cd(d)
{
strcpy(name, c);
}
Classic::Classic()
{
}
//重新定义report
void Classic::Report() const
{
cout << "name: " << name << endl;
Cd::Report();
}
Classic & Classic::operator=(const Classic & d)
{
if (this == &d)
return *this;
Cd::operator=(d);
strcpy(name, d.name);
return *this;
}
//主程序
#include <iostream>
using namespace std;
#include "classic.h"
void Bravo(const Cd & disk);
int main()
{
Cd c1("Beatles", "Capitol", 14, 35.5);
Classic c2 = Classic("Piano Sonata in B flat, Fantasia in C",
"Alfred Brendel", "Philips", 2, 57.17);
Cd *pcd = &c1;
cout << "Using object directly:\n";
c1.Report();
c2.Report();
cout << "Using type cd * pointer to objects:\n";
pcd->Report();
pcd = &c2;
pcd->Report();
cout << "Calling a function with a Cd reference argument:\n";
Bravo(c1);
Bravo(c2);
cout << "Testing assignment: ";
Classic copy;
copy = c2;
copy.Report();
return 0;
}
void Bravo(const Cd & disk)
{
disk.Report();
}