C++面向对象——一些基础例题及代码

Kurotani Takeo Lv1

注意看……

这是一篇自用文章,可能对你没有什么帮助。
为了节约你的时间,也许你可以跳过这篇文章: )

在此存放一些个人的 C++作业例题以及自己的解答。

过于简单,以至于这篇文章归于“自用”分类下,也许对你并没有太大的帮助(

随缘更新 uwu

1. Destructure Function and Overloaded Operator.

The Question:

Give the definitions for the member function addValue, the copy constructor, the overloaded assignment operator, the overloaded operator<<, and the destructor for the following class. This class is intended to be a class for a partially filled array. The member variable numberUsed contains the number of array positions currently filled. The other constructor definition is given to help you get started.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cstdlib>
using namespace std;

class PartFilledArray {
public:
PartFilledArray(int arraySize);
PartFilledArray(const PartFilledArray& object);
~PartFilledArray();
void operator=(const PartFilledArray& rightSide);
friend ostream& operator<<(ostream& outs, const PartFilledArray& object);
void addValue(double newEntry);
private:
double *a;
int maxNumber;
int numberUsed;
};

PartFilledArray::PartFilledArray(int arraySize) : maxNumber(arraySize), numberUsed(0)
{
a = new double[maxNumber];
}

My Solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream>
#include <cstdlib>
using namespace std;

class PartFilledArray
{
public:
PartFilledArray(int arraySize);
PartFilledArray(const PartFilledArray &object);
~PartFilledArray();
void operator=(const PartFilledArray &rightSide);
friend ostream &operator<<(ostream &outs, const PartFilledArray &object);
void addValue(double newEntry);

private:
double *a;
int maxNumber;
int numberUsed;
};

PartFilledArray::PartFilledArray(int arraySize) : maxNumber(arraySize), numberUsed(0)
{
a = new double[maxNumber];
}

// Member function addValue
void PartFilledArray::addValue(double newEntry) {
if (numberUsed < maxNumber) {
a[numberUsed] = newEntry;
numberUsed++;
} else {
cout << "Error: Adding value to a full array.\n";
exit(EXIT_FAILURE);
}
}

// Copy constructor
PartFilledArray::PartFilledArray(const PartFilledArray& object) : maxNumber(object.maxNumber), numberUsed(object.numberUsed) {
a = new double[maxNumber];
for (int i = 0; i < numberUsed; i++) {
a[i] = object.a[i];
}
}

// Overloaded assignment operator
void PartFilledArray::operator =(const PartFilledArray& rightSide) {
if (this != &rightSide) {
delete [] a;
maxNumber = rightSide.maxNumber;
numberUsed = rightSide.numberUsed;
a = new double[maxNumber];
for (int i = 0; i < numberUsed; i++) {
a[i] = rightSide.a[i];
}
}
}

// Overloaded operator<<
ostream& operator<<(ostream& outs, const PartFilledArray& object) {
for (int i = 0; i < object.numberUsed; i++) {
outs << object.a[i] << " ";
}
return outs;
}

// Destructor
PartFilledArray::~PartFilledArray() {
delete [] a;
}

int main() {
// Create a PartFilledArray object of size 5
PartFilledArray pfa1(5);

// Add values to pfa1
pfa1.addValue(1.1);
pfa1.addValue(2.2);
pfa1.addValue(3.3);

cout << "pfa1: " << pfa1 << endl;

// Create a new PartFilledArray object using the copy constructor
PartFilledArray pfa2(pfa1);

cout << "pfa2: " << pfa2 << endl;

// Create a PartFilledArray object of size 10
PartFilledArray pfa3(10);

// Add values to pfa3
for (int i = 0; i < 10; i++) {
pfa3.addValue(i * 1.1);
}

cout << "pfa3: " << pfa3 << endl;

// Use the assignment operator to assign the value of pfa3 to pfa1
pfa1 = pfa3;

cout << "pfa1: " << pfa1 << endl;

return 0;
}

2. (Continue to the previous question)

The Question:

Define a class called PartFilledArrayWMax that is a derived class of the class PartFilledArray. The class PartFilledArrayWMax has one additional member variable named maxValue that holds the maximum value stored in the array. Define a member accessor function named getMax that returns the maximum value stored in the array. Redefine the member function addValue and define two constructors, one of which has an int argument for the maximum number of entries in the array, another is a copy constructor. Also define an overloaded assignment operator, and a destructor.

1
2
3
4
5
6
7
8
9
10
11
12
13
class PartFilledArrayWMax : public PartFilledArray {
public:
PartFilledArrayWMax(int arraySize);
PartFilledArrayWMax(const PartFilledArrayWMax& object);
~PartFilledArrayWMax();
void operator= (const PartFilledArrayWMax& rightSide);
void addValue(double newEntry);
double getMax();

private:
double maxValue;

};

My Solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream>
#include <cstdlib>
using namespace std;

class PartFilledArray
{
public:
PartFilledArray(int arraySize);
PartFilledArray(const PartFilledArray &object);
~PartFilledArray();
void operator=(const PartFilledArray &rightSide);
friend ostream &operator<<(ostream &outs, const PartFilledArray &object);
void addValue(double newEntry);

private:
double *a;
int maxNumber;
int numberUsed;
};

PartFilledArray::PartFilledArray(int arraySize) : maxNumber(arraySize), numberUsed(0)
{
a = new double[maxNumber];
}

class PartFilledArrayWMax : public PartFilledArray
{

public:
PartFilledArrayWMax(int arraySize);
PartFilledArrayWMax(const PartFilledArrayWMax &object);
~PartFilledArrayWMax();
void operator=(const PartFilledArrayWMax &rightSide);
void addValue(double newEntry);
double getMax();

private:
double maxValue;
};

double PartFilledArrayWMax::getMax()
{
return maxValue;
}

void PartFilledArrayWMax::addValue(double newEntry)
{
PartFilledArray::addValue(newEntry);
maxValue = max(maxValue, newEntry);
}

PartFilledArrayWMax::PartFilledArrayWMax(const PartFilledArrayWMax &object) : PartFilledArray(object), maxValue(object.maxValue)
{
}

PartFilledArrayWMax::PartFilledArrayWMax(int arraySize) : PartFilledArray(arraySize), maxValue(0)
{
}

void PartFilledArrayWMax::operator=(const PartFilledArrayWMax &rightSide)
{
PartFilledArray::operator=(rightSide);
maxValue = rightSide.maxValue;
}

PartFilledArray::~PartFilledArray()
{
}

PartFilledArrayWMax::~PartFilledArrayWMax()
{
}

void PartFilledArray::addValue(double newEntry)
{
if (numberUsed < maxNumber)
{
a[numberUsed] = newEntry;
numberUsed++;
}
}

PartFilledArray::PartFilledArray(const PartFilledArray &object)
: maxNumber(object.maxNumber), numberUsed(object.numberUsed)
{
a = new double[maxNumber];
std::copy(object.a, object.a + object.numberUsed, a);
}

void PartFilledArray::operator=(const PartFilledArray &rightSide)
{
if (this != &rightSide)
{
delete[] a;

maxNumber = rightSide.maxNumber;
numberUsed = rightSide.numberUsed;

a = new double[maxNumber];
std::copy(rightSide.a, rightSide.a + rightSide.numberUsed, a);
}
}

int main()
{
PartFilledArrayWMax arr(10);

arr.addValue(5.0);
cout << "Max Value: " << arr.getMax() << endl;

arr.addValue(10.0);
cout << "Max Value: " << arr.getMax() << endl;

arr.addValue(3.0);
cout << "Max Value: " << arr.getMax() << endl;

return 0;
}

  • Title: C++面向对象——一些基础例题及代码
  • Author: Kurotani Takeo
  • Created at : 2023-11-22 01:19:44
  • Updated at : 2024-09-20 14:26:13
  • Link: https://krtk.top/2023/11/22/C-面向对象——一些基础例题及代码/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments