//question1
//classic.h
#ifndef CLASSIC_H
#define CLASSIC_H
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);
};
class Classic :public Cd
{
private:
char main_song[50];
public:
Classic(char * s1, char * s2, char * s3, int n, double x);
Classic(const Classic & d);
Classic();
virtual ~Classic();
virtual void Report() const;
Classic & operator=(const Classic & d);
};
#endif
//classic.cpp
#include "classic.h"
#include <cstring>
#include <iostream>
Cd::Cd(char * s1, char * s2, int n, double x)
{
strncpy(performers, s1, 50);
performers[49] = '\0';
strncpy(label, s2, 20);
label[19] = '\0';
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()
{
strcpy(performers, "none");
strcpy(label, "none");
selections = 0;
playtime = 0;
}
Cd::~Cd()
{
}
void Cd::Report() const
{
std::cout << "Performers: " << performers << " ";
std::cout << "Label: " << label << " ";
std::cout << "Selections: " << selections << " ";
std::cout << "Playtime: " << playtime << "\n";
}
Cd & Cd::operator=(const Cd & d)
{
strcpy(performers, d.performers);
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(s2, s3, n, x)
{
strncpy(main_song, s1, 50);
main_song[49] = '\0';
}
Classic::Classic(const Classic & d): Cd(d)
{
strcpy(main_song, d.main_song);
}
Classic::Classic(): Cd()
{
strcpy(main_song, "none");
}
Classic::~Classic()
{
}
void Classic::Report() const
{
Cd::Report();
std::cout << "Main song: " << main_song << "\n";
}
Classic & Classic::operator=(const Classic & d)
{
Cd::operator=(d);
strcpy(main_song, d.main_song);
return *this;
}
//13_1.cpp
#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:\n";
Classic copy;
copy = c2;
copy.Report();
return 0;
}
void Bravo(const Cd & disk)
{
disk.Report();
}
//question2
//classic2.h
#ifndef CLASSIC2_H
#define CLASSIC2_H
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_song;
public:
Classic(char * s1, char * s2, char * s3, int n, double x);
Classic(const Classic & d);
Classic();
virtual ~Classic();
virtual void Report() const;
Classic & operator=(const Classic & d);
};
#endif
//classic2.cpp
#include "classic2.h"
#include <cstring>
#include <iostream>
Cd::Cd(char * s1, char * s2, int n, double x)
{
performers = new char[strlen(s1) + 1];
strcpy(performers, s1);
label = new char[strlen(s2) + 1];
strcpy(label, s2);
selections = n;
playtime = x;
}
Cd::Cd(const Cd & d)
{
performers = new char[strlen(d.performers + 1)];
strcpy(performers, d.performers);
label = new char[strlen(d.label) + 1];
strcpy(label, 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;
}
Cd::~Cd()
{
delete [] performers;
delete [] label;
}
void Cd::Report() const
{
std::cout << "Performers: " << performers << " ";
std::cout << "Label: " << label << " ";
std::cout << "Selections: " << selections << " ";
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(performers, d.performers);
label = new char[strlen(d.label) + 1];
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(s2, s3, n, x)
{
main_song = new char[strlen(main_song) + 1];
strcpy(main_song, s1);
}
Classic::Classic(const Classic & d): Cd(d)
{
main_song = new char[strlen(d.main_song) + 1];
strcpy(main_song, d.main_song);
}
Classic::Classic(): Cd()
{
main_song = new char[1];
main_song[0] = '\0';
}
Classic::~Classic()
{
delete [] main_song;
}
void Classic::Report() const
{
Cd::Report();
std::cout << "Main song: " << main_song << "\n";
}
Classic & Classic::operator=(const Classic & d)
{
if(this == &d)
return *this;
Cd::operator=(d);
delete [] main_song;
main_song = new char[strlen(d.main_song) + 1];
strcpy(main_song, d.main_song);
return *this;
}
//13_2.cpp
#include <iostream>
using namespace std;
#include "classic2.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:\n";
Classic copy;
copy = c2;
copy.Report();
return 0;
}
void Bravo(const Cd & disk)
{
disk.Report();
}
//question3
//dma.h
#ifndef DMA_H
#define DMA_H
#include <iostream>
class DMA
{
private:
char * label;
int rating;
protected:
char * Lbl() const {return label;}
int Rt() const {return rating;}
public:
DMA(const char * l = "null", int r = 0);
DMA(const DMA & rs);
virtual ~DMA();
DMA & operator=(const DMA & rs);
virtual void View() const = 0;
};
class baseDMA :public DMA
{
public:
baseDMA(const char * l = "null", int r = 0):DMA(l, r){}
baseDMA(const baseDMA & rs);
baseDMA & operator=(const baseDMA & rs);
virtual ~baseDMA();
friend std::ostream & operator<<(std::ostream & os, const baseDMA & rs);
virtual void View() const;
};
class lacksDMA :public DMA
{
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 DMA & rs);
virtual ~lacksDMA();
virtual void View() const;
};
class hasDMA :public DMA
{
private:
char * style;
public:
hasDMA(const char * s = "none", const char * l = "null", int r = 0);
hasDMA(const char * s, const DMA & rs);
hasDMA(const hasDMA & hs);
virtual ~hasDMA();
virtual void View() const;
};
#endif
//dma.cpp
#include "dma.h"
#include <cstring>
DMA::DMA(const char * l, int r)
{
label = new char[std::strlen(l) + 1];
std::strcpy(label, l);
rating = r;
}
DMA::DMA(const DMA & rs)
{
label = new char[std::strlen(rs.label) + 1];
std::strcpy(label, rs.label);
rating = rs.rating;
}
DMA::~DMA()
{
delete [] label;
}
DMA & DMA::operator=(const DMA & rs)
{
if (this == &rs)
return *this;
delete [] label;
label = new char[std::strlen(rs.label) + 1];
std::strcpy(label, rs.label);
rating = rs.rating;
return *this;
}
baseDMA::baseDMA(const baseDMA & rs): DMA(rs)
{
}
baseDMA::~baseDMA()
{
}
baseDMA & baseDMA::operator=(const baseDMA & rs)
{
if(this == &rs)
return *this;
DMA::operator=(rs);
return *this;
}
void baseDMA::View() const
{
std::cout << "Label: " << Lbl() << std::endl;
std::cout << "Rating: " << Rt() << std::endl;
}
lacksDMA::lacksDMA(const char * c, const char * l, int r): DMA(l, r)
{
std::strncpy(color, c, COL_LEN - 1);
color[COL_LEN - 1] = '\0';
}
lacksDMA::lacksDMA(const char * c, const DMA & rs): DMA(rs)
{
std::strncpy(color, c, COL_LEN - 1);
color[COL_LEN - 1] = '\0';
}
void lacksDMA::View() const
{
std::cout << "Label: " << Lbl() << std::endl;
std::cout << "Rating: " << Rt() << std::endl;
std::cout << "Color: " << color << std::endl;
}
lacksDMA::~lacksDMA()
{
}
hasDMA::hasDMA(const char * s, const char * l, int r) : DMA(l, r)
{
style = new char[std::strlen(s) + 1];
std::strcpy(style, s);
}
hasDMA::hasDMA(const char * s, const DMA & rs) : DMA(rs)
{
style = new char[std::strlen(s) + 1];
std::strcpy(style, s);
}
hasDMA::hasDMA(const hasDMA & hs) : DMA(hs)
{
style = new char[std::strlen(hs.style) + 1];
std::strcpy(style, hs.style);
}
hasDMA::~hasDMA()
{
delete [] style;
}
void hasDMA::View() const
{
std::cout << "Label: " << Lbl() << std::endl;
std::cout << "Rating: " << Rt() << std::endl;
std::cout << "Style: " << style << std::endl;
}
//13_3.cpp
#include <iostream>
#include <string>
#include <cstring>
#include "dma.h"
const int NUM = 4;
int main()
{
using std::cin;
using std::cout;
using std::endl;
DMA * p_dma[NUM];
int rt;
char kind;
char cl[40];
for (int i = 0; i < NUM; i++)
{
cout << "The " << i + 1 << " member: \n";
char * lb = new char[30];
cout << "Enter label: ";
cin >> lb;
cout << "Enter rating: ";
cin >> rt;
cin.get();
cout << "Enter 1 for baseDMA or 2 for lacksDMA or 3 for hasDMA: ";
while (cin >> kind && (kind != '1' && kind != '2' && kind != '3'))
cout << "Enter either 1 or 2 or 3: ";
if (kind == '1')
p_dma[i] = new baseDMA(lb, rt);
else if (kind == '2')
{
cout << "Enter color: ";
cin >> cl;
p_dma[i] = new lacksDMA(cl, lb, rt);
}
else
{
char * st = new char[30];
cout << "Enter style: ";
cin >> st;
p_dma[i] = new hasDMA(st, lb, rt);
delete [] st;
}
delete [] lb;
while (cin.get() != '\n')
continue;
}
cout << endl;
for (int i = 0; i < NUM; i++)
{
p_dma[i]->View();
cout << endl;
}
for (int i = 0; i < NUM; i++)
{
delete p_dma[i];
}
cout << "Done.\n";
return 0;
}
//question4
//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;}
VintagePort & operator=(const VintagePort & vp);
void Show() const;
friend ostream & operator<<(ostream & os, const VintagePort & vp);
};
#endif
//port.cpp
#include "port.h"
#include <cstring>
#include <iostream>
Port::Port(const char * br, const char * st, int b)
{
brand = new char[strlen(br) + 1];
strcpy(brand, br);
strncpy(style, st, 19);
style[19] = '\0';
bottles = b;
}
Port::Port(const Port & p)
{
brand = new char[strlen(p.brand) + 1];
strcpy(brand, p.brand);
strncpy(style, p.style, 19);
style[19] = '\0';
bottles = p.bottles;
}
Port & Port::operator=(const Port & p)
{
if(this == &p)
return *this;
delete [] brand;
brand = new char[strlen(p.brand) + 1];
strcpy(brand, p.brand);
strncpy(style, p.style, 19);
style[19] = '\0';
bottles = p.bottles;
return *this;
}
Port & Port::operator+=(int b)
{
bottles += b;
return *this;
}
Port & Port::operator-=(int b)
{
bottles -= b;
return *this;
}
void Port::Show() const
{
std::cout << "Brand: " << brand << std::endl;
std::cout << "Kind: " << style << std::endl;
std::cout << "Bottles: " << bottles << std::endl;
}
ostream & operator<<(ostream & os, const Port & p)
{
os << p.brand << ", " << p.style << ", " << p.bottles;
return os;
}
VintagePort::VintagePort() : Port()
{
nickname = new char[1];
nickname = '\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(nickname, nn);
year = y;
}
VintagePort::VintagePort(const VintagePort & vp) : Port(vp)
{
nickname = new char[strlen(vp.nickname) + 1];
strcpy(nickname, vp.nickname);
year = vp.year;
}
VintagePort & VintagePort::operator=(const VintagePort & vp)
{
if (this == & vp)
return *this;
Port::operator=(vp);
delete [] nickname;
nickname = new char[strlen(vp.nickname) + 1];
strcpy(nickname, vp.nickname);
year = vp.year;
}
void VintagePort::Show() const
{
Port::Show();
std::cout << "Nickname: " << nickname << std::endl;
std::cout << "Year: " << year << std::endl;
}
ostream & operator<<(ostream & os, const VintagePort & vp)
{
os << (const Port) vp;
os << vp.nickname << ", " << vp.year << std::endl;
}
//13_4.cpp
#include "port.h"
#include <iostream>
int main()
{
Port a("new", "ruby", 2);
std::cout << a << std::endl;
a.Show();
a += 10;
a.Show();
a -= 5;
a.Show();
VintagePort b("Old Velvet", "joy", 10, "twy", 1990);
b.Show();
VintagePort c;
c = b;
std::cout << c << std::endl;
return 0;
}