日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++PrimerPlus学习——第十三章编程练习

發(fā)布時間:2025/3/11 c/c++ 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++PrimerPlus学习——第十三章编程练习 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

13-1
注意char*前面加const,不然就會報錯
Classis.h

#ifndef CLASSIC_H_ #define CLASS_H_ #include <string> class Cd { private:char performers[50];char label[20];int selections;double playtime; public:Cd(const char* s1, const char* s2, int n, double x);Cd(const Cd&);Cd();virtual ~Cd();virtual void Report() const;virtual Cd& operator=(const Cd& d); }; class Classic:public Cd { private:char major[50]; public:Classic(const char* s1, const char* s2, const char* s3, int n, double x);Classic();~Classic();void Report();Classic& operator=(const Classic& d); }; #endif // !CLASSIC_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

#ifndef CLASSIC_H_ #define CLASSIC_H_ #include <string> class Cd { private:char* performers;char* label;int selections;double playtime; public:Cd(const char* s1, const char* s2, int n, double x);Cd(const Cd&);Cd();virtual ~Cd();virtual void Report() const;virtual Cd& operator=(const Cd& d); }; class Classic:public Cd { private:char* major; public:Classic(const char* s1, const char* s2, const char* s3, int n, double x);Classic();~Classic();void Report();Classic& operator=(const Classic& d); }; #endif // !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
雖然不是很理解,但是這個套路是明白了
dma.h

#ifndef DMA_H_ #define DMA_H_ #include <iostream>class ABC {char* fullname;int level; public:ABC(const char* f = "null", int I = 0);ABC(const ABC& ab);virtual ~ABC();ABC& operator=(const ABC& ab);virtual void show(); };class baseDMA: public ABC { private:char* label;int rating; public:baseDMA(const char* l = "null", int r = 0, const char* f = "null", int lv = 0);baseDMA(const baseDMA& rs);~baseDMA();baseDMA& operator=(const baseDMA& rs);virtual void show(); };class lacksDMA: public ABC { private:enum{ COL_LEN = 40 };char color[COL_LEN]; public:lacksDMA(const char* c = "blank", const char* l = "null", int r = 0);lacksDMA(const char* c, const baseDMA& rs);virtual void show(); };class hasDMA: public ABC { private:char* style; public:hasDMA(const char* s = "none", const char* l = "null", int r = 0);hasDMA(const char* s, const baseDMA& hs);hasDMA(const hasDMA& hs);~hasDMA();hasDMA& operator=(const hasDMA& rs);virtual void show(); }; #endif // !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

#ifndef PORT_H_ #define PORT_H_ #include <iostream> using namespace std;class Port { private:char* brand;char style[20];int bottles; public:Port(const char* br = "none", const char* st = "none", int b = 0);Port(const Port& p);virtual~Port() { delete[] brand; }Port& operator=(const Port& p);Port& operator+=(int b);Port& operator-=(int b);int BottleCount() const { return bottles; }virtual void Show() const;friend ostream& operator<<(ostream& os, const Port& p); };class VintagePort : public Port { private:char* nickname;int year; public:VintagePort();VintagePort(const char* br, const char* st, int b, const char* nn, int y);VintagePort(const VintagePort& vp);~VintagePort() { delete[] nickname; }void Show() const;friend ostream& operator<<(ostream& os, const VintagePort& vp); }; #endif // !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; }

總結

以上是生活随笔為你收集整理的C++PrimerPlus学习——第十三章编程练习的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。