2024年2月25日发(作者:)
//cdplayer.h
#ifndef CDPLAYER_H_
#define CDPLAYER_H_
#include
#include
using namespace std;
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);
1、
Cd(){}
virtual ~Cd();
virtual void Report()const;
Cd &operator=(const Cd &d);
};
class Classic :public Cd
{
private:
charmain_music[50];
public:
Classic(){}
Classic(char *m, char *s1, char *s2, int n, double x);
Classic(char *m, const Cd &c);
~Classic();
Classic &operator=(const Classic &c);
virtual void Report()const;
};
#endif
//
#include "cdplayer.h"
Cd::Cd(char *s1, 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, 50, mers);
strcpy_s(label, 20, );
selections = ions;
playtime = me;
}
Cd::~Cd()
{
}
void Cd::Report()const
{
cout<< "Performers: " << performers < cout<< "Label: " << label < cout<< "Selections: " << selections < cout<< "Playtime: " << playtime < } Cd &Cd::operator=(const Cd &d) { if (this == &d) return *this; strcpy_s(performers, 50, mers); strcpy_s(label, 20, ); selections = ions; playtime = me; return *this; } Classic::Classic(char *m, char *s1, char *s2, int n, double x) :Cd(s1, s2, n, x) { strcpy_s(main_music, strlen(m) + 1, m); } Classic::Classic(char *m, const Cd &c) : Cd(c) { strcpy_s(main_music, strlen(m) + 1, m); } Classic::~Classic() { } Classic &Classic::operator=(const Classic &c) { if (this == &c) return *this; Cd::operator=(c); strcpy_s(main_music, 50, _music); return *this; } void Classic::Report()const { Cd::Report(); cout<< "Main Music: " < } // #include "cdplayer.h" void Bravo(const Cd &disk); int main() { Cd c1("Beatles", "Capito", 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"; (); (); 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; (); system("pause"); return 0; } void Bravo(const Cd &disk) { (); } 2、 //cdplayer.h #ifndef CDPLAYER_H_ #define CDPLAYER_H_ #include #include using namespace std; class Cd { private: char *performers; char *label; 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); }; class Classic :public Cd { private: char *main_music; public: Classic(){} Classic(char *m, char *s1, char *s2, int n, double x); Classic(char *m, const Cd &c); ~Classic(); Classic &operator=(const Classic &c); virtual void Report()const; }; #endif // #include "cdplayer.h" Cd::Cd(char *s1, char *s2, int n, double x) { performers = new char[strlen(s1) + 1]; label = new char[strlen(s2) + 1]; strcpy_s(performers, strlen(s1) + 1, s1); strcpy_s(label, strlen(s2) + 1, s2); selections = n; playtime = x; } Cd::Cd(const Cd &d) { performers = new char[strlen(mers) + 1]; label = new char[strlen() + 1]; strcpy_s(performers, strlen(mers) + 1, mers); strcpy_s(label, strlen() + 1, ); selections = ions; playtime = me; } Cd::~Cd() { delete[]performers; delete[]label; } void Cd::Report()const { cout<< "Performers: " << performers < cout<< "Label: " << label < cout<< "Selections: " << selections < cout<< "Playtime: " << playtime < } Cd &Cd::operator=(const Cd &d) { if (this == &d) return *this; delete[]performers; delete[]label; performers = new char[strlen(mers) + 1]; label = new char[strlen() + 1]; strcpy_s(performers, strlen(mers) + 1, mers); strcpy_s(label, strlen() + 1, ); selections = ions; playtime = me; return *this; } Classic::Classic(char *m, char *s1, char *s2, int n, double x) :Cd(s1, s2, n, x) { main_music = new char[strlen(m) + 1]; strcpy_s(main_music, strlen(m) + 1, m); } Classic::Classic(char *m, const Cd &c) : Cd(c) { main_music = new char[strlen(m) + 1]; strcpy_s(main_music, strlen(m) + 1, m); } Classic::~Classic() { delete[]main_music; } Classic &Classic::operator=(const Classic &c) { if (this == &c) return *this; Cd::operator=(c); delete[]main_music; main_music = new char[strlen(_music) + 1]; strcpy_s(main_music, strlen(_music) + 1, _music); return *this; } void Classic::Report()const { Cd::Report(); cout<< "Main Music: " < } // #include "cdplayer.h" void Bravo(const Cd &disk); int main() { Cd c1("Beatles", "Capito", 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"; (); (); 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; (); system("pause"); return 0; } void Bravo(const Cd &disk) { (); } 3、 //dma.h #ifndef DMA_H_ #define DMA_H_ #include #include using namespace std; class ABC { private: char *fullname; int level; public: ABC(const char *f = "null", int l = 0); ABC(const ABC &ab); ABC &operator=(const ABC &ab); virtual ~ABC(); virtual void View() = 0; }; classbaseDMA :public ABC { private: char *label; int rating; public: baseDMA(const char *l = "null", int r = 0, const char *f = "null", int lv = 0); baseDMA(constbaseDMA&rs); ~baseDMA(); baseDMA&operator=(constbaseDMA&rs); virtual void View(); }; classlacksDMA :public ABC { private: enum{ COL_LEN = 40 }; char color[COL_LEN]; public: lacksDMA(const char *c = "blank", const char *f = "null", int lv = 0); lacksDMA(const char *c, constbaseDMA&rs); virtual void View(); }; classhasDMA :public ABC { private: char *style; public: hasDMA(const char *s = "none", const char *f = "null", int lv = 0); hasDMA(const char *s, const ABC &rs); hasDMA(consthasDMA&hs); ~hasDMA(); hasDMA&operator=(consthasDMA&hs); virtual void View(); }; #endif // #include "dma.h" ABC::ABC(const char *f, int lv) { fullname = new char[strlen(f) + 1]; strcpy_s(fullname, strlen(f) + 1, f); level = lv; } ABC::ABC(const ABC &ab) { fullname = new char[strlen(me) + 1]; strcpy_s(fullname, strlen(me) + 1, me); level = ; } ABC::~ABC() { delete[]fullname; } ABC &ABC::operator=(const ABC &ab) { if (this == &ab) return *this; delete[]fullname; fullname = new char[strlen(me) + 1]; strcpy_s(fullname, strlen(me) + 1, me); level = ; return *this; } void ABC::View() { cout<< "Fullname: " < cout<< "Level: " << level < } 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(constbaseDMA&rs) :ABC(rs) { label = new char[strlen() + 1]; strcpy_s(label, strlen() + 1, ); rating = ; } baseDMA::~baseDMA() { delete[]label; } baseDMA&baseDMA::operator=(constbaseDMA&rs) { if (this == &rs) return *this; ABC::operator=(rs); delete[]label; label = new char[strlen() + 1]; strcpy_s(label, strlen() + 1, ); rating = ; return *this; } voidbaseDMA::View() { ABC::View(); cout<< "Label: " << label < cout<< "Rating: " << rating < } lacksDMA::lacksDMA(const char *c, const char *f, int lv) :ABC(f, lv) { strncpy_s(color, c, 39); color[39] = '0'; } lacksDMA::lacksDMA(const char *c, constbaseDMA&rs) : ABC(rs) { strncpy_s(color, c, COL_LEN - 1); color[COL_LEN - 1] = '0'; } voidlacksDMA::View() { ABC::View(); cout<< "Color: " << color < } hasDMA::hasDMA(const char *s, const char *f, int lv) :ABC(f, lv) { style = new char[strlen(s) + 1]; strcpy_s(style, strlen(s) + 1, s); } hasDMA::hasDMA(const char *s, const ABC &ab) :ABC(ab) { style = new char[strlen(s) + 1]; strcpy_s(style, strlen(s) + 1, s); } hasDMA::hasDMA(consthasDMA&hs) : ABC(hs) { style = new char[strlen() + 1]; strcpy_s(style, strlen() + 1, ); } hasDMA::~hasDMA() { delete[]style; } hasDMA&hasDMA::operator=(consthasDMA&hs) { if (this == &hs) return *this; ABC::operator=(hs); delete[]style; style = new char[strlen() + 1]; strcpy_s(style, strlen() + 1, ); return *this; } voidhasDMA::View() { ABC::View(); cout<< "Style: " << style < } // #include "dma.h" int main() { baseDMA shirt("Portabelly", 8, "Jack", 1); lacksDMA balloon("red", "Blimpo", 4); hasDMA map("Mercator", "buffalo Keys", 5); cout<< "Displaying baseDMA object:n"; (); cout<< "Displaying lacksDMA object:n"; (); cout<< "Displaying hasDMA object:n"; (); lacksDMA balloon2(balloon); cout<< "Result of lacksDMA copy:n"; (); hasDMA map2; map2 = map; cout<< "Result of hasDMA assignment:n"; (); system("pause"); return 0; } 4、 //port.h #ifndef PORT_H_ #define PORT_H_ #include #include 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); intBottleCount()const{ return bottles; } virtual void Show()const; friendostream&operator<<(ostream&os, const Port &p); }; classVintagePort :public Port { private: char *nickname; int year; public: VintagePort(); VintagePort(const char *br, const char *st, int b, const char *nn, int y); VintagePort(constVintagePort&vp); ~VintagePort(){ delete[]nickname; } VintagePort&operator=(constVintagePort&vp); void Show()const; friendostream&operator<<(ostream&os, constVintagePort&vp); }; #endif // #include "port.h" 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, strlen(st) + 1, st); bottles = b; } Port::Port(const Port &p) { brand = new char[strlen() + 1]; strcpy_s(brand, strlen() + 1, ); strcpy_s(style, strlen() + 1, ); bottles = s; } Port &Port::operator=(const Port &p) { if (this == &p) return *this; delete[]brand; brand = new char[strlen() + 1]; strcpy_s(brand, strlen() + 1, ); strcpy_s(style, strlen() + 1, ); bottles = s; return *this; } Port &Port::operator+=(int b) { bottles += b; return *this; } Port &Port::operator-=(int b) { bottles -= b; return *this; } void Port::Show()const { cout<< "Brand: " << brand < cout<< "Kind: " << style < cout<< "Bottles: " << bottles < } ostream&operator<<(ostream&os, const Port &p) { os<<<< ", " <<<< ", " < returnos; } 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(constVintagePort&vp) :Port(vp) { nickname = new char[strlen(me) + 1]; strcpy_s(nickname, strlen(me) + 1, me); year = ; } VintagePort&VintagePort::operator=(constVintagePort&vp) { if (this == &vp) return *this; delete[]nickname; Port::operator=(vp); nickname = new char[strlen(me) + 1]; strcpy_s(nickname, strlen(me) + 1, me); year = ; return *this; } voidVintagePort::Show()const { Port::Show(); cout<< "Nickname: " << nickname < cout<< "Year: " << year < } ostream&operator<<(ostream&os, constVintagePort&vp) { os<< (const Port &)vp; os<< ", " < returnos; } // #include "port.h" int main() { Port wine1("Gallo", "tawny", 20); VintagePortwine2("Romane Conti", "vintage", 10, "The Noble", 1876); VintagePortwine3("Merlot", "ruby", 30, "Old Velvet", 1888); cout<< "Displaying Port object:n"; (); cout<< wine1 < cout<< "Displaying VintagePort object:n"; (); cout<< wine2 < cout<< "Displaying VintagePort object:n"; (); cout<< wine3 < cout<< "Gallo add ten bottles:n"; wine1 += 10; cout<< wine1 < cout<< "Romane Conti add ten bottles:n"; wine2 += 10; cout<< wine2 < cout<< "Merlot minus ten bottles:n"; wine3 -= 10; cout<< wine3 < Port wine4(wine2); cout<< "Result of Port copy:n"; cout<< wine4 < VintagePort wine5; wine5 = wine3; cout<< "Result of VintagePort assignment:n"; cout<< wine5 < system("pause"); return 0; }


发布评论