C++基础知识体系导图

新手小白结合《C++ primer plus》1-13章制作了C++基础知识体系框架导图,拿来重新回顾C++的基础知识点或者复习大一的期末考试应该是没问题。因本人能力有限,不可避免可能会有少量错误,如发现,欢迎指正勘误,谢谢大家啦!

1
2
在这里插入图片描述
实例

//头文件
#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();
}
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值