Chapter 7.7: Overloading Assignment and Subscripting Operators#
Adapted from: “Object-Oriented Programming Using C++” by Ira Pohl (Addison - Wesley)
Program that demonstrates overloading assignment and subscripting operators in C++#
In file vect2.cpp
/*********************************************************************
Filename: vect1.cpp
Chapter: 6 Object Creation and Destruction
Section: 6.7 A Class vect
Compiler: Borland C++ Version 5.0 Summer 1996
Object Oriented Programming Using C++, Edition 2 By Ira Pohl
*********************************************************************/
//test of safe array type vect
#include "vect2.h"
using namespace std; // Added. MK
int main()
{
const int ARR_SIZE = 20;
vect a(10), b(5), c, d(20), e_from_arr(ARR_SIZE);
int arr[ARR_SIZE];
int i;
cout << "The upper bound of vector a is : " << a.ub() << endl;
cout << "The upper bound of vector b is : " << b.ub() << endl;
cout << endl;
cout << "Initialize vect a...." << endl;
for (i = 0; i <= a.ub(); ++i)
a[i] = i;
cout << "The vector a contains : ";
a.print();
cout << "The vector b contains : ";
b.print();
cout << endl;
cout << "Perform b = a (vect to vect assignment)..." << endl;
b = a;
cout << "The vector b contains : ";
b.print();
cout << endl;
// Default constructor
cout << "The vector c contains : ";
c.print();
cout << "Perform c = b (vect to vect assignment)..." << endl;
c = b;
cout << endl;
cout << "The vector d contains : ";
d.print();
cout << "Perform d = a + c (vect to vect addition)..." << endl;
cout << "The vector a contains : ";
a.print();
cout << "The vector c contains : ";
c.print();
d = a + c;
cout << "The vector d contains : ";
d.print();
cout << endl;
cout << "The vector e_from_arr contains : ";
e_from_arr.print();
cout << "Initialize array arr..." << endl;
for (i = 0; i < ARR_SIZE; ++i) {
arr[i] = i + 10;
}
cout << "Array arr contains : " << endl;
for (i = 0; i < ARR_SIZE; ++i) {
cout << arr[i] << " ";
}
cout << endl;
cout << "Convert array arr into vect e_from_arr..." << endl;
e_from_arr = vect(arr, ARR_SIZE);
cout << "The vector e_from_arr contains : ";
e_from_arr.print();
}
In file vect2.h
/*********************************************************************
Filename: vect2.h
Chapter: 7 Ad Hoc Polymorphism
Section: 7.7 Friend Functions
Compiler: Borland C++ Version 5.0 Summer 1996
Object Oriented Programming Using C++, Edition 2 By Ira Pohl
*********************************************************************/
class matrix; //forward reference
#include <iostream> // Changed iostream.h to iostream. MK
#include <assert.h>
using namespace std; // Added. MK.
class vect {
public:
vect() { size = 10; p = new int[size]; }
explicit vect(int n);
vect(const vect& v);
vect(const int a[], int n); //initialize by array
~vect() { delete []p; }
int ub() const { return (size - 1); } //upper bound
vect& operator=(const vect& v); //overload assignment
void print() const;
int& operator[](int i) ; //range checked
vect operator+(const vect& v);
private:
int* p;
int size;
friend vect mpy(const vect& v, const matrix& m);
};
vect::vect(int n) : size(n)
{
assert(n > 0);
p = new int[size];
assert(p != 0);
}
vect::vect(const int a[], int n) : size(n)
{
assert(n > 0);
p = new int[size];
assert(p != 0);
for (int i = 0; i < size; ++i)
p[i] = a[i];
}
vect::vect(const vect& v) : size(v.size)
{
p = new int[size];
assert(p != 0);
for (int i = 0; i < size; ++i)
p[i] = v.p[i];
}
int& vect::operator[](int i)
{
assert(i >= 0 && i < size);
return p[i];
}
vect& vect::operator=(const vect& v)
{
int s = (size < v.size) ? size : v.size;
if (v.size != size)
cerr << "[*] copying different size arrays "
<< size << " and " << v.size << endl;
for (int i = 0; i < s; ++i)
p[i] = v.p[i];
return (*this);
}
void vect::print() const
{
for (int i = 0; i <= (size-1); ++i)
cout << p[i] << " ";
cout << endl;
}
vect vect::operator+(const vect& v)
{
assert(size == v.size);
vect sum(size);
for (int i = 0; i < size; ++i)
sum.p[i] = p[i] + v.p[i];
return sum;
}
Compilation Process#
The above program is compiled and run using Gnu Compiler Collection (g++):
import os
root_dir = os.getcwd()
code_dir = root_dir + "/" + \
"Cpp_Code/Chapter_7_7_Overloading_Assignment_and_Subscripting_Operators"
os.chdir(code_dir)
build_command = os.system("g++ vect2.cpp -w -o vect2")
Execution Process#
exec_status = os.system("./vect2")
The upper bound of vector a is : 9
The upper bound of vector b is : 4
Initialize vect a....
The vector a contains : 0 1 2 3 4 5 6 7 8 9
The vector b contains : 0 0 0 0 0
Perform b = a (vect to vect assignment)...
The vector b contains : 0 1 2 3 4
The vector c contains : 0 0 0 0 0 0 0 0 0 0
Perform c = b (vect to vect assignment)...
The vector d contains : 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Perform d = a + c (vect to vect addition)...
The vector a contains : 0 1 2 3 4 5 6 7 8 9
The vector c contains : 0 1 2 3 4 0 0 0 0 0
The vector d contains : 0 2 4 6 8 5 6 7 8 9 0 0 0 0 0 0 0 0 0 0
The vector e_from_arr contains : 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Initialize array arr...
Array arr contains :
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
Convert array arr into vect e_from_arr...
The vector e_from_arr contains : 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29