第十三章编程练习(2)
来源:互联网 发布:抽风解说 知乎 编辑:程序博客网 时间:2024/05/29 23:21
Classic.h
#pragma once#ifndef Classic_H_#define Classic_H_class Cd {private:char * performers;char * label;int selections;double playtime;public:Cd(char * s1="blank", char * s2="null", int n=0, double x=0.0<span style="color:#000000;">);//注意默认构造函数的初始化</span>explicit Cd(const Cd & d);//使用explicit关键字禁止隐式转换,可以让错误出现在编译过程而不是运行过程virtual ~Cd();//为了先调用派生类的析构函数再调用基类的,基类析构函数必须声明为虚virtual void Report()const; Cd & operator = (const Cd & cd);};class Classic :public Cd {private:char * name;public:Classic(char * s1="blank", char *s2="null", char *s3="none", int n=0, double x=0.0);~Classic();explicit Classic(const Classic & c);void Report()const;Classic &operator=(const Classic & c);};#endif
Classic.cpp
#include "Classic.h"#include <iostream>Cd::Cd(char * s1, char * s2, int n, double x){performers = new char[std::strlen(s1) + 1];std::strcpy(performers, s1);label = new char[std::strlen(s2) + 1];std::strcpy(label, s2);selections = n;playtime = x;}Cd::Cd(const Cd & d){performers = new char[std::strlen(d.performers) + 1];std::strcpy(performers, d.performers);label = new char[std::strlen(d.label) + 1];std::strcpy(label, d.label);selections = d.selections;playtime = d.playtime;}Cd::~Cd(){delete[]performers;delete[]label;}void Cd::Report() const{using std::cout;using std::endl;cout << "Performers: " << performers << endl<< "Label: " << label << endl<< "Selections: " << selections << endl<< "Playtime: " << playtime << endl;}Cd & Cd::operator=(const Cd & d){if (this == &d)return *this;delete[]performers;delete[]label;performers = new char[std::strlen(d.performers) + 1];std::strcpy(performers, d.performers);label = new char[std::strlen(d.label) + 1];std::strcpy(label, d.label);selections = d.selections;playtime = d.playtime;return *this;}Classic::Classic(char * s1, char * s2, char * s3, int n, double x) :Cd(s1, s2, n, x){name = new char[std::strlen(s3) + 1];std::strcpy(name, s3);}Classic::~Classic(){delete[]name;}Classic::Classic(const Classic & c):Cd(c){name = new char[std::strlen(c.name) + 1];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);delete[] name;name = new char[std::strlen(c.name) + 1];std::strcpy(name, c.name);return *this;}
main.cpp
#include <iostream>#include "Classic.h"void Bravo(const Cd & disk);using namespace std;int main(){Cd c1("Beatles", "Capitol", 14, 35.5);//Classic c2 = Classic("Piano Sonata in B flat, Fantasia in C",//"Alfred Brendel", "Philips", 2, 7.17); # 因为使用了explicit关键字,所以这段代码不能匹配到复制构造函数
Classic c2("Piano Sonata in B flat, Fantasia in C", "Alfred Brendel", "Philips", 2, 7.17);Cd * pcd = &c1;cout << "Using object directly:\n";c1.Report();cout << endl;c2.Report();cout << endl;cout << "Using type cd * pointer to object:\n";pcd->Report();cout << endl;pcd = &c2;pcd->Report();cout << "\nCalling a function with a Cd reference argument:\n";Bravo(c1);Bravo(c2);cout << "Testing assignment: \n";Classic copy;copy = c2;copy.Report();cin.get();return 0;}void Bravo(const Cd & disk){disk.Report();std::cout << endl;}
0 0
- 第十三章编程练习(2)
- 第十三章 编程练习
- python 核心编程2 第十三章练习
- 第十三章编程练习(3)
- 第十三章编程练习(4)
- 第十三章编程练习(1)
- (一四五)第十三章编程练习
- 编程练习(第十三周)
- C primer plus(第五版)编程练习第十三章
- C++ Primer Plus第六版 第十三章 编程练习答案
- C Primer Plus 第十三章 编程练习 1-13题
- C ++ Primer Plus 第六版 第十三章编程练习答案
- 第十三章 13.1.2节练习
- 第十三章 13.2.2节练习
- 第十三章 13.6.2节练习
- 第十三章上机练习
- 第十三章练习
- simply scheme 第十三章 练习
- Carmichael Numbers uva10006
- Android远程服务——AIDL
- Windows-Git和Github管理自己代码
- Oracle db 重做日志
- /etc/profile 和 ~/.profile 区别是全部用户,和单一用户
- 第十三章编程练习(2)
- 【bzoj2561】最小生成树 最小割
- 使用EA完成数据库设计
- [iOS]防止数组越界及添加空值的crash的扩展
- hdu3111 Sudoku(DFS水)
- wamp2.5的配置以及一些常见问题的解决
- Python笔记
- Apriori算法实现
- 程序员这样优化简历,一投制胜