Sunteți pe pagina 1din 45

1.

Start with the following class declaration:


// base class
class Cd { // represents a CD disk
private:
char performers[50];
char label[20];
int selections; // number of selections
double playtime; // playing time in minutes
public:
Cd(char * s1, char * s2, int n, double x);
Cd(const Cd & d);
Cd();
~Cd();
void Report() const; // reports all CD data
Cd & operator=(const Cd & d);
};
Derive a Classic class that adds an array of char members that will hold a string
identifying
the primary work on the CD. If the base class requires that any functions be
virtual,
modify the base-class declaration to make it so. If a declared method is not
needed,
remove it from the definition. Test your product with the following program:
#include
using namespace std;
#include classic.h // which will contain #include cd.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(); // use Cd method
c2.Report(); // use Classic method
cout << Using type cd * pointer to objects:\n;
pcd->Report(); // use Cd method for cd object
pcd = &c2;
pcd->Report(); // use Classic method for classic object
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();
}
My Answer:
cd.h
1 #ifndef CD_H_
2 #define CD_H_
3
4 // base class
5 class Cd { // represents a CD disk
6 private:
7

char performers[50];

char label[20];

int selections; // number of selections

10

double playtime; // playing time in minutes

11 public:
12

Cd(char * s1, char * s2, int n, double x);

13

// Cd(const Cd & d); removed, there are no pointers or dynamic allocation

14

Cd();

15

virtual ~Cd() {} ;

16

virtual void Report() const; // reports all CD data

17

// Cd & operator=(const Cd & d); removed, not needed

18 };
19
20 #endif
cd.cpp
1

#include <iostream>

#include "cd.h"

3
4

Cd::Cd(char *s1, char *s2, int n, double x)

std::strncpy(performers,s1,49);

performers[49] = '\0';

std::strncpy(label,s2,19);

label[19] = '\0';

10

selections = n;

11

playtime = x;

12 }
13
14 Cd::Cd()
15 {
16

performers[0] = '\0';

17

label[0] = '\0';

18

selections = 0;

19

playtime = 0;

20 }
21
22 void Cd::Report() const
23 {
24

std::cout << "\nPerformer: " << performers;

25

std::cout << "\nLabel: " << label;

26

std::cout << "\nSelections: " << selections;

27

std::cout << "\nPlaytime: " << playtime << std::endl;

28 }
classic.h
1

#ifndef CLASSIC_H_

#define CLASSIC_H_

3
4

#include "cd.h"

5
6

class Classic : public Cd

private:

char primarywork[60];

10 public:
11

Classic(char * pr, char * s1, char * s2, int n, double x);

12

Classic();

13

~Classic() {};

14

void Report() const;

15 };
16
17 #endif
classic.cpp
1 #include "classic.h"
2 #include <iostream>
3
4
5 Classic::Classic(char *pr, char *s1, char *s2, int n, double x) : Cd(s1,s2,n,x)
6 {
7

std::strncpy(primarywork,pr,59);

primarywork[59] = '\0';

9 }
10
11 Classic::Classic() : Cd()
12 {
13

primarywork[0] = '\0';

14 }
15
16 void Classic::Report() const
17 {
18

std::cout << "\nPrimary Work: " << primarywork;

19

Cd::Report();

20 }
cp13ex1.cpp
1 #include <iostream>
2 #include "classic.h" // which will contain #include cd.h

3
4 using namespace std;
5 void Bravo(const Cd & disk);
6
7 int main()
8 {
9

Cd c1("Beatles", "Capitol", 14, 35.5);

1
Classic c2 = Classic("Piano Sonata in B flat, Fantasia in C","Alfred Brendel",
0 "Philips", 2, 57.17);
1
1
1
2
1
3

Cd *pcd = &c1;
cout << "\nUsing object directly:\n";
c1.Report(); // use Cd method
c2.Report(); // use Classic method
cout << "\nUsing type cd * pointer to objects:\n";

1
4

pcd->Report(); // use Cd method for cd object

1
5

pcd->Report(); // use Classic method for classic object

pcd = &c2;

1
6

cout << "\nCalling a function with a Cd reference argument:\n";

1
7

Bravo(c2);

1
8

Classic copy;

1
9

Bravo(c1);

cout << "\nTesting assignment: \n";

copy = c2;
copy.Report();

2
0
2
1
2
2

std::cin.get();
std::cin.get();
return 0;

}
2
3 void Bravo(const Cd & disk)
2 {
4
disk.Report();

2
5
2
6
2
7
2
8
2
9
3
0

3
1
3
2
3
3
3
4

2. Do Programming Exercise 1, but use dynamic memory allocation instead of


fixed-size
arrays for the various strings tracked by the two classes.
My Answer:
cd2.h
1

#ifndef CD2_H_

#define CD2_H_

3
4

// base class

class Cd { // represents a CD disk

private:

char * performers;

char * label;

int selections; // number of selections

10

double playtime; // playing time in minutes

11 public:
12

Cd(char * s1, char * s2, int n, double x);

13

Cd(const Cd & d);

14

Cd();

15

virtual ~Cd();

16

virtual void Report() const; // reports all CD data

17

Cd & operator=(const Cd & d);

18 };
19
20 #endif
cd2.cpp
1

#include <iostream>

#include "cd2.h"

3
4

Cd::Cd(char *s1, char *s2, int n, double x)

performers = new char [std::strlen(s1)+1];

std::strcpy(performers,s1);

label = new char [std::strlen(s2)+1];

std::strcpy(label,s2);

10

selections = n;

11

playtime = x;

12 }
13
14 Cd::Cd()
15 {
16

performers = NULL;

17

label = NULL;

18

selections = 0;

19

playtime = 0;

20 }

21
22 Cd::Cd(const Cd & d)
23 {
24

performers = new char [std::strlen(d.performers)+1];

25

std::strcpy(performers,d.performers);

26

label = new char [std::strlen(d.label)+1];

27

std::strcpy(label,d.label);

28

selections = d.selections;

29

playtime = d.playtime;

30 }
31
32 Cd & Cd::operator=(const Cd & d)
33 {
34

if (this == &d)

35

return *this;

36

delete [] performers;

37

delete [] label;

38

performers = new char [std::strlen(d.performers)+1];

39

std::strcpy(performers,d.performers);

40

label = new char [std::strlen(d.label)+1];

41

std::strcpy(label,d.label);

42

selections = d.selections;

43

playtime = d.playtime;

44

return *this;

45 }
46
47 void Cd::Report() const
48 {
49

std::cout << "\nPerformer: " << performers;

50

std::cout << "\nLabel: " << label;

51

std::cout << "\nSelections: " << selections;

52

std::cout << "\nPlaytime: " << playtime << std::endl;

53 }
54
55 Cd::~Cd()
56 {
57

delete [] performers;

58

delete [] label;

59 }
classic2.h
1

#ifndef CLASSIC2_H_

#define CLASSIC2_H_

3
4

#include "cd2.h"

5
6

class Classic : public Cd

private:

char * primarywork;

10 public:
11

Classic(char * pr, char * s1, char * s2, int n, double x);

12

Classic();

13

Classic(const Classic & d);

14

~Classic() { delete [] primarywork; };

15

void Report() const;

16

Classic & operator=(const Classic & d);

17 };
18
19 #endif
classic2.cpp
1 #include "classic2.h"
2 #include <iostream>
3
4

5 Classic::Classic(char *pr, char *s1, char *s2, int n, double x) : Cd(s1,s2,n,x)


6 {
7

primarywork = new char [std::strlen(pr)+1];

std::strcpy(primarywork,pr);

9 }
10
11 Classic::Classic() : Cd()
12 {
13

primarywork = NULL;

14 }
15
16 Classic::Classic(const Classic & d) : Cd(d)
17 {
18

primarywork = new char [std::strlen(d.primarywork)+1];

19

std::strcpy(primarywork,d.primarywork);

20 }
21
22 Classic & Classic::operator =(const Classic &d)
23 {
24

if (this == &d)

25

return *this;

26

Cd::operator=(d);

27

delete [] primarywork;

28

primarywork = new char [std::strlen(d.primarywork)+1];

29

std::strcpy(primarywork,d.primarywork);

30

return *this;

31 }
32
33 void Classic::Report() const
34 {
35

std::cout << "\nPrimary Work: " << primarywork;

36

Cd::Report();

37}
cp13ex2.cpp
1 #include <iostream>
2 #include "classic2.h" // which will contain #include cd.h
3
4 using namespace std;
5 void Bravo(const Cd & disk);
6
7 int main()
8 {
9

Cd c1("Beatles", "Capitol", 14, 35.5);

1
Classic c2 = Classic("Piano Sonata in B flat, Fantasia in C","Alfred Brendel",
0 "Philips", 2, 57.17);
1
1
1
2
1
3

Cd *pcd = &c1;
cout << "\nUsing object directly:\n";
c1.Report(); // use Cd method
c2.Report(); // use Classic method
cout << "\nUsing type cd * pointer to objects:\n";

1
4

pcd->Report(); // use Cd method for cd object

1
5

pcd->Report(); // use Classic method for classic object

pcd = &c2;

1
6

cout << "\nCalling a function with a Cd reference argument:\n";

1
7

Bravo(c2);

1
8

Classic copy;

1
9

Bravo(c1);

cout << "\nTesting assignment: \n";

copy = c2;
copy.Report();

2
0
2
1
2
2

std::cin.get();
std::cin.get();
return 0;

2
3
2
4
2
5
2
6
2
7 }
2 void Bravo(const Cd & disk)
8
{
2
disk.Report();
9
3 }
0
3
1
3
2
3
3
3
4

3. Revise the baseDMA-lacksDMA-hasDMA class hierarchy so that all three


classes are derived
from an ABC. Test the result with a program similar to the one in Listing 13.10.
That is,
it should feature an array of pointers to the ABC and allow the user to make
runtime
decisions as to what types of objects are created. Add virtual View() methods to
the
class definitions to handle displaying the data.
My Answer:
dma.h
1 // dma.h -- inheritance and dynamic memory allocation
2 #ifndef DMA_H_
3 #define DMA_H_

4 #include <iostream>
5
6 class ABC_DMA
7{
8 private:
9

char * label;

1
0

int rating;
public:

1
1

ABC_DMA(const char *l = "null", int r = 0);


ABC_DMA(const ABC_DMA & rs);

1
2

virtual ~ABC_DMA();

1
3
1
4

ABC_DMA & operator=(const ABC_DMA & rs);


friend std::ostream & operator<<(std::ostream & os, const ABC_DMA & rs);
virtual void View() const = 0; // pure virtual function, thus making ABC_DMA
an abstract base class

1
5 };
1
6

1 class baseDMA : public ABC_DMA


7
{
1
8 public:
baseDMA(const char * l = "null", int r = 0);

1
9

void View() const { ABC_DMA::View(); }

2
0
2
1

};

2
2

class lacksDMA : public ABC_DMA


2
3{
2 private:
4
enum { COL_LEN = 40};
2
5

char color[COL_LEN];

2 public:
6
lacksDMA(const char * c = "blank", const char * l = "null", int r = 0);
2
lacksDMA(const char * c, const ABC_DMA & rs);
7
void View() const;
2
8 };
2
9
3
0 class hasDMA : public ABC_DMA
3{
1 private:
3
2
3
3
3
4
3
5
3
6

char * style;
public:
hasDMA(const char * s = "none", const char * l = "null", int r = 0);
hasDMA(const char * s, const ABC_DMA & rs);
hasDMA(const hasDMA & hs);
~hasDMA(); // own special destructor is needed for DMA item style
hasDMA & operator=(const hasDMA & rs);
void View() const;

3 };
7
#endif
3
8
3
9
4
0
4
1
4
2
4
3
4
4
4

5
4
6
4
7
4
8
4
9
5
0
5
1
5
2
5
3
5
4
dma.cpp
1 // dma.cpp --dma class methods
2 #include "dma.h"
3 #include <cstring>
4
5 ABC_DMA::ABC_DMA(const char *l, int r)
6 {
7

label = new char [std::strlen(l) + 1];

std::strcpy(label,l);

rating = r;

10 }
11
12 ABC_DMA::ABC_DMA(const ABC_DMA & rs)
13 {
14

label = new char[std::strlen(rs.label) + 1];

15

std::strcpy(label, rs.label);

16

rating = rs.rating;

17 }
18
19 ABC_DMA::~ABC_DMA()
20 {
21

delete [] label;

22 }
23
24 ABC_DMA & ABC_DMA::operator=(const ABC_DMA & rs)
25 {
26

if (this == &rs)

27

return *this;

28

delete [] label;

29

label = new char[std::strlen(rs.label) + 1];

30

std::strcpy(label, rs.label);

31

rating = rs.rating;

32

return *this;

33 }
34
35 std::ostream & operator<<(std::ostream & os, const ABC_DMA & rs)
36 {
37

rs.View();

38

return os;

39 }
40
41 void ABC_DMA::View() const
42 {
43

std::cout << "\nLabel: " << label;

44

std::cout << "\nRating: " << rating;

45 }
46
47 baseDMA::baseDMA(const char * l, int r) : ABC_DMA(l,r)

48 {
49
50 }
51
52 // lacksDMA methods
53 lacksDMA::lacksDMA(const char * c, const char * l, int r) : ABC_DMA(l, r)
54 {
55

std::strncpy(color, c, 39);

56

color[39] = '\0';

57 }
58 lacksDMA::lacksDMA(const char * c, const ABC_DMA & rs) : ABC_DMA(rs)
59 {
60

std::strncpy(color, c, 39);

61

color[39] = '\0';

62 }
63
64 void lacksDMA::View() const
65 {
66

ABC_DMA::View();

67

std::cout << "\nColor: " << color;

68 }
69
70 // hasDMA methods
71 hasDMA::hasDMA(const char * s, const char * l, int r) : ABC_DMA(l, r)
72 {
73

style = new char[std::strlen(s) + 1];

74

std::strcpy(style, s);

75 }
76 hasDMA::hasDMA(const char * s, const ABC_DMA & rs) : ABC_DMA(rs)
77 {
78

style = new char[std::strlen(s) + 1];

79

std::strcpy(style, s);

80
81

82

hasDMA::hasDMA(const hasDMA & hs) : ABC_DMA(hs) // invoke base class


83 copy constructor
84 {
85

style = new char[std::strlen(hs.style) + 1];

86

std::strcpy(style, hs.style);

87 }
88 hasDMA::~hasDMA()
89 {
delete [] style;

90
91 }

92 hasDMA & hasDMA::operator=(const hasDMA & hs)


93 {
if (this == &hs)

94

return *this;

95
96

ABC_DMA::operator=(hs); // copy base portion

97

delete [] style;

98

style = new char[std::strlen(hs.style) + 1];

99

std::strcpy(style, hs.style);

10
0

return *this;
}

10
void hasDMA::View() const
1
{
10
2
ABC_DMA::View();
10
3

std::cout << "\nStyle: " << style;


}

10
4
cp13ex3.cpp
1 #include <iostream>
2 #include "dma.h"

3
4 const int LEN = 40; // max lenght of Label, Rating, Color, and Style strings
5 const int ITEMS = 4; // total of 4 items to be entered
6
7 int main()
8{
9
1
0
1
1
1
2
1
3
1
4
1
5

using namespace std;

char templabel[LEN], tempcolor[LEN], tempstyle[LEN], choice;


int temprating = 0;

ABC_DMA * pt[ITEMS]; // array of abstract base class pointers

for (int i = 0; i < ITEMS; i++)


{
cout << "\nEnter 1 for Shirt, 2 for Balloon, 3 for Map: ";

while ((cin >> choice).get() && (choice != '1' && choice != '2' && choice !
1 = '3')) // make sure choice is correct
6
{
1
7
1
8

cout << "Enter either 1, 2, or 3. : ";


}

cout << "\nItem #" << i + 1 << " Label: "; // Label and Rating are for all
1
three
classes
9
2
0
2
1
2
2
2
3

cin.getline(templabel,LEN);
cout << "Item #" << i + 1 << " Rating: ";
(cin >> temprating).get();

switch(choice)
{

case '1': pt[i] = new baseDMA (templabel, temprating); // if Shirt is


2 chosen, baseDMA class
4
break;

2
5

case '2': cout << "Item #" << i + 1 << " Color: "; // if Balloon is
2 chosen, lacksDMA class
6
cin.getline(tempcolor,LEN);
2
pt[i] = new lacksDMA(tempcolor, templabel, temprating);
7
break;
2
8
2
case '3': cout << "Item #" << i + 1 << " Style: "; // if Map is chosen,
9 hasDMA class
3
0

cin.getline(tempstyle,LEN);

3
1

break;

pt[i] = new hasDMA(tempstyle,templabel, temprating);

3
2
3
3

3
4

cout << "\nDisplaying entered items:";

3
5

for (int i = 0; i < ITEMS; i++)


{

3
6
3
7
3
8
3
9
4
0
4
1
4
2}
4
3
4

cout << "\n--------";


pt[i]->View();
}

for (int i = 0; i < ITEMS; i++)


delete pt[i]; // deleting all dynamic pointers from array

std::cin.get();
std::cin.get();
return 0;

4
4
5
4
6
4
7
4
8
4
9
5
0
5
1
5
2
5
3
5
4
5
5
5
6
5
7
5
8
5
9
6
0

4. The Benevolent Order of Programmers maintains a collection of bottled port.


To describe
it, the BOP Portmaster has devised a Port class, as declared here:
#include
using namespace std;
class Port

{
private:
char * brand;
char style[20]; // i.e., tawny, ruby, vintage
int bottles;
public:
Port(const char * br = none, const char * st = none, int b = 0);
Port(const Port & p); // copy constructor
virtual ~Port() { delete [] brand; }
Port & operator=(const Port & p);
Port & operator+=(int b); // adds b to bottles
Port & operator-=(int b); // subtracts b from bottles, if
available
int BottleCount() const { return bottles; }
virtual void Show() const;
friend ostream & operator<<(ostream & os, const Port & p);
};
The Show() method presents information in the following format:
Brand: Gallo
Kind: tawny
Bottles: 20
The operator<<() function presents information in the following format (with no
newline
character at the end):
Gallo, tawny, 20
The Portmaster completed the method definitions for the Port class and then
derived
the VintagePort class as follows before being relieved of his position for
accidentally
routing a bottle of 45 Cockburn to someone preparing an experimental barbecue
sauce:
class VintagePort : public Port // style necessarily = vintage
{
private:
char * nickname; // i.e., The Noble or Old Velvet, etc.
int year; // vintage year
public:
VintagePort();
VintagePort(const char * br, 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);
};
You get the job of completing the VintagePort work.
a. Your first task is to re-create the Port method definitions because the former
Portmaster immolated his upon being relieved.
b. Your second task is to explain why certain methods are redefined and others
are not.

c. Your third task is to explain why operator=() and operator<<() are not virtual.
d. Your fourth task is to provide definitions for the VintagePort methods.
My Answer:
Note: One way to do this exercise is to place style member into
protected section, this way it can be modified and set to vintage by
VintagePort class methods in all cases of copying or in any advanced
uses of this class. However, since this is an exercise for beginners
book, I find it unnecessary to complicate the code in this case, so style
is left unmodified in private section of Port class
port.h
1 #ifndef PORT_H_
2 #define PORT_H_
3
4 #include <iostream>
5
6 using namespace std;
7 class Port
8 {
9 private:
10

char * brand;

11

int bottles;

12

char style[20]; // i.e., tawny, ruby, vintage

13 public:
14

Port(const char * br = "none", const char * st = "none", int b = 0);

15

Port(const Port & p); // copy constructor

16

virtual ~Port() { delete [] brand; }

17

Port & operator=(const Port & p);

18

Port & operator+=(int b); // adds b to bottles

19

Port & operator-=(int b); // subtracts b from bottles, if available

20

int BottleCount() const { return bottles; }

21

virtual void Show() const;

22

friend std::ostream & operator<<(std::ostream & os, const Port & p);

23 };

24
25 #endif
port.cpp
1 #include "port.h"
2
3 Port::Port(const char * br, const char * st, int b)
4 {
5

brand = new char [std::strlen(br)+1];

std::strcpy(brand,br);

std::strncpy(style,st,19);

style[19] = '\0';

bottles = b;

10 }
11
12 Port::Port(const Port & p)
13 {
14

brand = new char [std::strlen(p.brand)+1];

15

std::strcpy(brand,p.brand);

16

std::strncpy(style,p.style,19);

17

style[19] = '\0';

18

bottles = p.bottles;

19 }
20
21 Port & Port::operator=(const Port & p)
22 {
23

if (this == &p)

24

return *this;

25

delete [] brand;

26

brand = new char [std::strlen(p.brand)+1];

27

std::strcpy(brand,p.brand);

28

std::strncpy(style,p.style,19);

29

style[19] = '\0';

30

bottles = p.bottles;

31

return *this;

32 }
33
34 Port & Port::operator+=(int b)
35 {
36

bottles += b;

37

return *this;

38 }
39
40 Port & Port::operator-=(int b)
41 {
42

bottles -= b;

43

return *this;

44 }
45
46 void Port::Show() const
47 {
48

std::cout << "\nBrand: " << brand;

49

std::cout << "\nKind: " << style;

50

std::cout << "\nBottles: " << bottles;

51 }
52
53 std::ostream & operator<<(std::ostream & os, const Port & p)
54 {
55

os << p.brand << ", " << p.style << ", " << p.bottles;

56

return os;

57 }
vintage.h
1#ifndef VINTAGE_H_
2#define VINTAGE_H_
3

4#include "port.h"
5
6class VintagePort : public Port // style necessarily = vintage
7{
8private:
9 char * nickname; // i.e., The Noble or Old Velvet, etc.
1 int year; // vintage xxvcfyear
0
public:
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9
2
0
2
1

VintagePort();
VintagePort(const char * br, 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
vintage.cpp
1 #include "vintage.h"
2
3 VintagePort::VintagePort() : Port("none","vintage",0)
4 {
5

nickname = new char [std::strlen("nonick")+1];

std::strcpy(nickname,"nonick");

year = 0;

8 }

9
1 VintagePort::VintagePort(const char * br, int b, const char * nn, int y) :
0 Port(br,"vintage",b)
1 {
1
1
2

nickname = new char [std::strlen(nn)+1];


std::strcpy(nickname,nn);

1
3 }

year = y;

1
4

VintagePort::VintagePort(const VintagePort & vp) : Port(vp)


1
5 {
1
6

nickname = new char [std::strlen(vp.nickname)+1];

1
7

year = vp.year;

1
8

std::strcpy(nickname,vp.nickname);

1 VintagePort & VintagePort::operator =(const VintagePort & vp)


9
{
2
if (this == &vp)
0
return *this;
2
1
Port::operator=(vp);
2
2
2
3
2
4

delete [] nickname;
nickname = new char [std::strlen(vp.nickname)+1];
std::strcpy(nickname,vp.nickname);
year = vp.year;
return *this;

2 }
5
2
6 void VintagePort::Show() const
2 {
7
std::cout << "\nNickname: " << nickname;
2
8

std::cout << "\nYear: " << year;

2
9

Port::Show();
}

3
0
std::ostream & operator<<(ostream & os, const VintagePort & vp)
3
1 {
3
2

os << vp.nickname << ", " << vp.year << ", ";

3
3

return os;

3 }
4
3
5
3
6
3
7
3
8
3
9
4
0
4
1
4
2
4
3
4
4
4
5
4
6
4
7
4

os << (const Port &) vp;

8
cp13ex4.cpp
1 #include <iostream>
2 #include "port.h"
3 #include "vintage.h"
4
5 int main()
6 {
7

using namespace std;

8
9

Port sample1;

1
0

Port sample2("Kings","Bright",30);

1
1
1
2

Port sample3("NewOne","DarkRed",15);
VintagePort sample4("Valley",10,"Family",1957);
Port sample5(sample4);

1
3

sample4 += 10;

1
4

sample1 = sample5;

1
5
1
6
1
7

sample3 -= 5;

Port * pts[5] = {&sample1,&sample2,&sample3,&sample4,&sample5};

for (int i = 0; i < 5; i++)


{
cout << "Sample#" << i+1 << ":" << endl;

1
8

pts[i]->Show();

1
9
2
0
2
1
2

cout << endl << endl;


}

cout << endl;


VintagePort sample6("BlueCastle",20,"NewType",1920);

2
2
3
2
4
2
5
2
6
2
7
2
8

cout << sample2 << endl;

2
9

cout << sample6 << endl;

3
0

cout << "\nBottle counts:\n";

3
1

for (int i = 0; i < 5; i++)

3
3

cin.get();

cout << "Sample #" << i + 1 << ": " << pts[i]->BottleCount() << "
3 bottles " << endl;
2

3
4
3 }
5
3
6
3
7
3
8
3
9
4
0
4
1

cin.get();
return 0;

b) Methods that werent redefined deal with non-dynamic data of base class and
could be automatically applied to any newly derived class without additional
operations or instructions to make the compatible with derived class. Such
methods include: operator+=(int b), Port & operator-=(int b), int BottleCount().
In these methods none of the dynamically allocated variables are used,
therefore, these methods could be applied to derived classes with no additional
instructions.
Methods were redefined that deal with dynamically allocated data or class
constructors that cant be inherited, or redefined copy constructors to
accommodate instructions for new dynamic data members such as nickname.
c) Assignment operator ( operator =() ) is not inherited, and it will have to be
redefined to accommodate for new nickname member which is dynamically
allocated. Operator<<() is not virtual because it is used as part of a friend
function, and since friend function is not a member of a class, it cannot be used
as virtual to invoke it by use of pointer-to-base class pointers, in other words,
dynamic binding cannot be used since it closely works with inherited methods,
not friendly non-class-member functions.

S-ar putea să vă placă și