1.
//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();
virtual ~Cd();
virtual void Report() const;
};
class Classic:public Cd{
private:
char mainWork[100];
public:
Classic(char * m, char *s1, char * s2, int n, double x);
Classic();
virtual void Report() const;
};
#endif
//classic.cpp
#include<iostream>
#include "classic.h"
using namespace std;
Cd::Cd(char * s1, char * s2, int n, double x){
strcpy(performers, s1);
strcpy(label, s2);
selections = n;
playtime = x;
}
Cd::Cd(){
performers[0] = '\0';
label[0] = '\0';
selections = 0;
playtime = 0.0;
}
Cd::~Cd(){}
void Cd::Report() const{
cout << "performers: " << performers << endl;
cout << "label: " << label << endl;
cout << "seledtions: " << selections << endl;
cout << "playtime: " << playtime << endl;
cout << endl;
}
Classic::Classic(char * m, char *s1, char * s2, int n, double x) :Cd(s1, s2, n, x){
strcpy(mainWork, m);
}
Classic::Classic() : Cd(){
mainWork[0] = '\0';
}
void Classic :: Report() const{
cout << "mainWork: " << mainWork << endl;
Cd::Report();
}
//main.cpp
#include <iostream>
#include "classic.h"
using namespace std;
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", "Philios", 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();
cin.get();
return 0;
}
void Bravo(const Cd & disk)
{
disk.Report();
}
2.
//classic.h
#ifndef CLASSIC_H_
#define CLASSIC_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 * mainWork;
public:
Classic(char * m, char *s1, char * s2, int n, double x);
Classic(const Classic & d);
Classic();
~Classic();
virtual void Report() const;
Classic & operator=(const Classic & d);
};
#endif
//classic.cpp
#include<iostream>
#include "classic.h"
using namespace std;
Cd::Cd(char * s1, char * s2, int n, double x){
int len1 = strlen(s1);
int len2 = strlen(s2);
performers = new char[len1+1];
label = new char[len2+1];
strcpy(performers, s1);
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];
label = new char[1];
performers[0] = '\0';
label[0] = '\0';
selections = 0;
playtime = 0.0;
}
Cd::~Cd(){
delete [] performers;
delete [] label;
}
void Cd::Report() const{
cout << "performers: " << performers << endl;
cout << "label: " << label << endl;
cout << "seledtions: " << selections << endl;
cout << "playtime: " << playtime << endl;
cout << endl;
}
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 * m, char *s1, char * s2, int n, double x) :Cd(s1, s2, n, x){
int len = strlen(m);
mainWork = new char[len + 1];
strcpy(mainWork, m);
}
Classic::Classic(const Classic & d):Cd(d){
mainWork = new char[strlen(d.mainWork) + 1];
strcpy(mainWork, d.mainWork);
}
Classic::Classic() : Cd(){
mainWork = new char[1];
mainWork[0] = '\0';
}
Classic::~Classic(){
delete [] mainWork;
}
void Classic :: Report() const{
cout << "mainWork: " << mainWork << endl;
Cd::Report();
}
Classic & Classic::operator=(const Classic & d){
if (this == &d)
return *this;
Cd::operator=(d);
delete [] mainWork;
mainWork = new char[strlen(d.mainWork) + 1];
strcpy(mainWork, d.mainWork);
return *this;
}
//main.cpp
#include <iostream>
#include "classic.h"
using namespace std;
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", "Philios", 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();
cin.get();
return 0;
}
void Bravo(const Cd & disk)
{
disk.Report();
}
3.
//dma.h
#ifndef DMA_H_
#define DMA_H_
class dmaABC{
private:
char *label;
int rating;
public:
dmaABC(const char * l = "NULL", int r = 0);
dmaABC(const dmaABC & d);
virtual ~dmaABC();
dmaABC & operator=(const dmaABC & d);
virtual void View() const = 0;
};
class baseDMA :public dmaABC{
public:
baseDMA(const char * l = "NULL", int r = 0) :dmaABC(l, r){}
virtual void View() const;
};
class lacksDMA :public dmaABC{
public:
enum {COL_LEN=40};
private:
char color[COL_LEN];
public:
lacksDMA(const char * c = "blank", const char * l = "null", int r = 0);
lacksDMA(const char *c, const dmaABC & d);
virtual void View() const;
};
class hasDMA :public dmaABC{
private:
char *style;
public:
hasDMA(const char * s = "none", const char * l = "null", int r = 0);
hasDMA(const char *s , const dmaABC & d);
hasDMA(const hasDMA & d);
~hasDMA();
hasDMA & operator=(const hasDMA & d);
virtual void View() const;
};
#endif
//dma.cpp
#include<iostream>
#include "dma.h"
using namespace std;
dmaABC::dmaABC(const char * l, int r){
label = new char[strlen(l) + 1];
strcpy(label, l);
rating = r;
}
dmaABC::dmaABC(const dmaABC & d){
label = new char[strlen(d.label) + 1];
strcpy(label, d.label);
rating = d.rating;
}
dmaABC::~dmaABC(){
delete [] label;
}
dmaABC & dmaABC::operator=(const dmaABC & d){
if (this == &d)
return *this;
delete [] label;
label = new char[strlen(d.label) + 1];
strcpy(label, d.label);
rating = d.rating;
return *this;
}
void dmaABC::View() const{
cout << "label: " << label << endl;
cout << "rating: " << rating << endl;
cout << endl;
}
void baseDMA::View() const{
dmaABC::View();
}
lacksDMA::lacksDMA(const char * c, const char * l, int r) :dmaABC(l, r){
strncpy(color, c, 39);
color[39] = '\0';
}
lacksDMA::lacksDMA(const char *c, const dmaABC & d):dmaABC(d){
strncpy(color, c, 39);
color[39] = '\0';
}
void lacksDMA::View() const{
cout << "color: " << color << endl;
dmaABC::View();
}
hasDMA::hasDMA(const char * s, const char * l , int r):dmaABC(l,r){
style = new char[strlen(s) + 1];
strcpy(style, s);
}
hasDMA::hasDMA(const char *s, const dmaABC & d) : dmaABC(d){
style = new char[strlen(s) + 1];
strcpy(style, s);
}
hasDMA::hasDMA(const hasDMA & d) : dmaABC(d){
style = new char[strlen(d.style) + 1];
strcpy(style, d.style);
}
hasDMA::~hasDMA(){
delete [] style;
}
hasDMA & hasDMA::operator=(const hasDMA & d){
if (this == &d)
return *this;
dmaABC::operator=(d);
delete [] style;
style = new char[strlen(d.style) + 1];
strcpy(style, d.style);
return *this;
}
void hasDMA::View() const{
cout << "style: " << style << endl;
dmaABC::View();
}
//main.cpp
#include <iostream>
#include <string>
#include "dma.h"
using namespace std;
int main(){
dmaABC * p[4];
string l;
int r;
char kind;
for (int i = 0; i < 4; i++){
cout << "Enter label: ";
getline(cin,l);
cout << "Enter rating: ";
cin >> r;
cout << "Enter 1 for baseDMA or 2 for lacksDMA or 3 for hasDMA: ";
while (cin >> kind && (kind != '1'&&kind != '2'&&kind != '3'))
cout << "Enter 1,2,or 3: ";
while (cin.get() != '\n')
continue;
if (kind == '1')
p[i] = new baseDMA(&l[0], r);
if (kind == '2')
{
char color[lacksDMA::COL_LEN];
cout << "Enter color: ";
cin.getline(color, lacksDMA::COL_LEN);
p[i] = new lacksDMA(color, &l[0], r);
}
if(kind=='3')
{
string s;
cout << "Enter style: ";
getline(cin, s);
p[i] = new hasDMA(&s[0], &l[0], r);
}
}
cout << endl;
for (int i = 0; i < 4; i++){
p[i]->View();
}
for (int i = 0; i < 4; i++)
delete p[i];
cout << "Done!" << endl;
cin.get();
return 0;
}
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; }
VintagePort & operator=(const VintagePort & vp);
void show() const;
friend ostream & operator<<(ostream & os, const VintagePort & vp);
};
#endif
//port.cpp
#include<iostream>
#include "port.h"
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, 20);
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, 20);
style[19] = '\0';
bottles = p.bottles;
return *this;
}
Port & Port::operator+=(int b){
bottles += b;
return *this;
}
Port & Port::operator-=(int b){
if (bottles < b){
cout << "wrong!" << endl;
return *this;
}
bottles -= b;
return *this;
}
void Port::show() const{
cout << "Brand: " << brand << endl;
cout << "Kind: " << style << endl;
cout << "Bottles: " << bottles << endl;
}
ostream & operator<<(ostream & os, const Port & p){
os << p.brand << ", " << p.style << ", " << p.bottles;
return os;
}
VintagePort::VintagePort():Port(){
nickname = new char[5];
strcpy(nickname, "NULL");
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);
nickname = new char[strlen(vp.nickname) + 1];
strcpy(nickname, vp.nickname);
year = vp.year;
return *this;
}
void VintagePort::show() const{
Port::show();
cout << "Nickname: " << nickname << endl;
cout << "Year: " << year << endl;
}
ostream & operator << (ostream & os, const VintagePort & vp){
os << (const Port &) vp;
os << vp.nickname << ", " << vp.year;
return os;
}
//main.cpp
#include <iostream>
#include <string>
#include "port.h"
using namespace std;
int main(){
Port a("Gallo", "tawny", 20);
a.show();
Port b;
b.show();
b = a;
b += 20;
cout << b << endl;
VintagePort c("Gallo", "tawny", 20, "Old Velvet", 1982);
Port * p = &a;
p->show();
p = &c;
p->show();
(*p) -= 10;
cout << p->BottleCount();
cin.get();
return 0;
}