C++PrimerPlus学习——第十三章编程练习
生活随笔
收集整理的這篇文章主要介紹了
C++PrimerPlus学习——第十三章编程练习
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
13-1
注意char*前面加const,不然就會(huì)報(bào)錯(cuò)
Classis.h
Classic.cpp
#include <iostream> #include "Classic.h"Cd::Cd(const char* s1, const char* s2, int n, double x) {strcpy_s(performers, strlen(s1) + 1, s1);strcpy_s(label, strlen(s2) + 1, s2);selections = n;playtime = x; } Cd::Cd(const Cd& d) {strcpy_s(performers, strlen(d.performers) + 1, d.performers);strcpy_s(label, strlen(d.label) + 1, d.label);selections = d.selections;playtime = d.playtime; } Cd::Cd() {performers[0] = '\0';label[0] = '\0';selections = 0;playtime = 0.0; } Cd::~Cd() {} void Cd::Report() const {std::cout << "Preformers: " << performers << '\n';std::cout << "Label: " << label << '\n';std::cout << "selectiongs: " << selections << '\n';std::cout << "playtime: " << playtime << '\n'; } Cd& Cd::operator=(const Cd& d) {if (this == &d)return *this;strcpy_s(performers, strlen(d.performers) + 1, d.performers);strcpy_s(label, strlen(d.label) + 1, d.label);selections = d.selections;playtime = d.playtime;return *this; } Classic::Classic(const char* s1, const char* s2, const char* s3, int n, double x):Cd(s2, s3, x, n) {strcpy_s(major, strlen(s1)+1, s1); } Classic::Classic():Cd() {major[0] = '\0'; } Classic::~Classic() {} void Classic::Report() {Cd::Report();std::cout << "Major: " << major << '\n'; } Classic& Classic::operator=(const Classic& d) {if (this == &d)return *this;Cd::operator=(d);strcpy_s(major, strlen(d.major) + 1, d.major);return *this; }main.cpp
#include <iostream> #include "Classic.h" using std::cout; 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 << "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(); }13-2
主函數(shù)仍然不變
Classic.h
Classic.cpp
#include <iostream> #include "Classic.h"Cd::Cd(const char* s1, const char* s2, int n, double x) {performers = new char[strlen(s1) + 1];strcpy_s(performers, strlen(s1) + 1, s1);label = new char[strlen(s2) + 1];strcpy_s(label, strlen(s2) + 1, s2);selections = n;playtime = x; } Cd::Cd(const Cd& d) {performers = new char[strlen(d.performers) + 1];strcpy_s(performers, strlen(d.performers) + 1, d.performers);label = new char[strlen(d.label) + 1];strcpy_s(label, strlen(d.label) + 1, d.label);selections = d.selections;playtime = d.playtime; } Cd::Cd() {performers = new char[1];performers[0] = '\0';label = new char[1];label[0] = '\0';selections = 0;playtime = 0.0; } Cd::~Cd() {delete[] performers;delete[] label; } void Cd::Report() const {std::cout << "Preformers: " << performers << '\n';std::cout << "Label: " << label << '\n';std::cout << "selectiongs: " << selections << '\n';std::cout << "playtime: " << playtime << '\n'; } Cd& Cd::operator=(const Cd& d) {if (this == &d)return *this;delete[] performers;delete[] label;performers = new char[strlen(d.performers) + 1];strcpy_s(performers, strlen(d.performers) + 1, d.performers);label = new char[strlen(d.label) + 1];strcpy_s(label, strlen(d.label) + 1, d.label);selections = d.selections;playtime = d.playtime;return *this; } Classic::Classic(const char* s1, const char* s2, const char* s3, int n, double x):Cd(s2, s3, x, n) {major = new char[strlen(s1) + 1];strcpy_s(major, strlen(s1)+1, s1); } Classic::Classic():Cd() {major = new char[1];major[0] = '\0'; } Classic::~Classic() {delete[] major; } void Classic::Report() {Cd::Report();std::cout << "Major: " << major << '\n'; } Classic& Classic::operator=(const Classic& d) {if (this == &d)return *this;Cd::operator=(d);delete[] major;major = new char[strlen(d.major) + 1];strcpy_s(major, strlen(d.major) + 1, d.major);return *this; }13-3
雖然不是很理解,但是這個(gè)套路是明白了
dma.h
dma.cpp
#include<cstring> #include<string> #include "dma.h"ABC::ABC(const char* a, int b) {fullname = new char[strlen(a) + 1];strcpy_s(fullname, strlen(a) + 1, a);level = b; } ABC::ABC(const ABC& ab) {fullname = new char[strlen(ab.fullname) + 1];strcpy_s(fullname, strlen(ab.fullname) + 1, ab.fullname);level = ab.level; } ABC::~ABC() {delete[] fullname; } ABC& ABC::operator=(const ABC& ab) {if (this == &ab)return *this;delete[] fullname;fullname = new char[strlen(ab.fullname) + 1];strcpy_s(fullname, strlen(ab.fullname) + 1, ab.fullname);level = ab.level;return *this; } void ABC::show() {std::cout << "fullname: " << fullname << std::endl;std::cout << "level: " << level << std::endl; }baseDMA::baseDMA(const char* l, int r, const char* f, int lv):ABC(f, lv) {label = new char[strlen(l) + 1];strcpy_s(label, strlen(l) + 1, l);rating = r; } baseDMA::baseDMA(const baseDMA& rs):ABC(rs) {label = new char[strlen(rs.label) + 1];strcpy_s(label, strlen(rs.label) + 1, rs.label);rating = rs.rating; } baseDMA::~baseDMA() {delete[] label; } baseDMA& baseDMA::operator=(const baseDMA& rs) {if (this == &rs)return *this;ABC::operator=(rs);delete[] label;label = new char[strlen(rs.label) + 1];strcpy_s(label, strlen(rs.label) + 1, rs.label);rating = rs.rating;return *this; } void baseDMA::show() {ABC::show();std::cout << "label: " << label << std::endl;std::cout << "rating: " << rating << std::endl; } lacksDMA::lacksDMA(const char* c, const char* l, int r):ABC(l, r) {strcpy_s(color, strlen(c) + 1, c); } lacksDMA::lacksDMA(const char* c, const baseDMA& rs):ABC(rs) {strcpy_s(color, strlen(c) + 1, c); } void lacksDMA::show() {ABC::show();std::cout << "color: " << color << std::endl; } hasDMA::hasDMA(const char* s, const char* l, int r):ABC(l, r) {style = new char[strlen(s) + 1];strcpy_s(style, strlen(s) + 1, s); } hasDMA::hasDMA(const char* s, const baseDMA& hs):ABC(hs) {style = new char[strlen(s) + 1];strcpy_s(style, strlen(s) + 1, s); } hasDMA::hasDMA(const hasDMA& hs):ABC(hs) {style = new char[strlen(hs.style) + 1];strcpy_s(style, strlen(hs.style) + 1, hs.style); } hasDMA::~hasDMA() {delete[] style; } hasDMA& hasDMA::operator=(const hasDMA& hs) {if (this == &hs)return *this;ABC::operator=(hs);delete[] style;style = new char[strlen(hs.style) + 1];strcpy_s(style, strlen(hs.style) + 1, hs.style);return *this; } void hasDMA::show() {ABC::show();std::cout << "style: " << style << std::endl; }main.cpp
#include <iostream> #include "dma.h" using std::cout;int main() {baseDMA a1("asdfadsf", 9, "dsfadfa", 1);lacksDMA a2("dfafda", "daf d", 2);hasDMA a3("sdfadfas", "dfqwedsa", 3);cout << "baseDMA:\n";a1.show();cout << "lacksDMA:\n";a2.show();cout << "hasDMA:\n";a3.show();baseDMA a4 = a1;lacksDMA a5 = a2;hasDMA a6 = a3;return 0; }13-4
Port.h
Port.cpp
#include "Port.h" #include <cstring> Port::Port(const char* br, const char* st, int b) {brand = new char[strlen(br) + 1];strcpy_s(brand, strlen(br) + 1, br);strcpy_s(style, 20, st);bottles = b; }Port::Port(const Port& p) {brand = new char[strlen(p.brand) + 1];strcpy_s(brand, strlen(p.brand) + 1, p.brand);strcpy_s(style, 20, p.style);bottles = p.bottles; }Port& Port::operator=(const Port& p) {if (this == &p)return *this;delete[] brand;brand = new char[strlen(p.brand) + 1];strcpy_s(brand, strlen(p.brand) + 1, p.brand);strcpy_s(style, p.style);bottles = p.bottles;return *this;return *this;// TODO: 在此處插入 return 語句 }Port& Port::operator+=(int b) {bottles += b;return *this;// TODO: 在此處插入 return 語句 }Port& Port::operator-=(int b) {bottles -= b;return *this; // TODO: 在此處插入 return 語句 }void Port::Show() const {std::cout << "Brand: " << brand << std::endl;std::cout << "King: " << style << std::endl;std::cout << "Bottles: " << bottles << std::endl; }ostream& operator<<(ostream& os, const Port& p) {std::cout << p.brand << ", " << p.style << ", " << p.bottles << std::endl;return os;// TODO: 在此處插入 return 語句 }ostream& operator<<(ostream& os, const VintagePort& vp) {os << (const Port&)vp;std::cout << vp.nickname << ", " << vp.year << std::endl;return os;// TODO: 在此處插入 return 語句 }VintagePort::VintagePort() {nickname = new char[1];nickname[0] = '\0';year = 0; }VintagePort::VintagePort(const char* br, const char* st, int b, const char* nn, int y): Port(br, st, b) {nickname = new char[strlen(nn) + 1];strcpy_s(nickname, strlen(nn) + 1, nn);year = y; }VintagePort::VintagePort(const VintagePort& vp) {nickname = new char[strlen(vp.nickname) + 1];strcpy_s(nickname, strlen(vp.nickname), vp.nickname);year = vp.year; }void VintagePort::Show() const {Port::Show();std::cout << "Nickname: " << nickname << std::endl;std::cout << "Year: " << year << std::endl; }main.cpp
#include <iostream> #include "Port.h" using std::cout;int main() {Port wine1("Gallo", "tawny", 20);VintagePort wine2("Romance Conti", "vintage", 10, "The Noble", 1876);VintagePort wine3("Merlot", "ruby", 30, "Old Velvet", 1888);cout << "Show Port object:\n";wine1.Show();cout << wine1 << endl;cout << "Show VintagePort object:\n";wine2.Show();cout << wine2 << endl;cout << "Show VintagePort object:\n";wine3.Show();cout << wine3 << endl;Port(wine4) = wine1;cout << "Show VintagePort object:\n";wine4.Show();cout << wine4<< endl;return 0; }總結(jié)
以上是生活随笔為你收集整理的C++PrimerPlus学习——第十三章编程练习的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oracle19c的版本号_升级到 or
- 下一篇: h5禁用浏览器下载视频_Flash正式被