---
jupytext:
formats: md:myst
text_representation:
extension: .md
format_name: myst
kernelspec:
display_name: Python 3
language: python
name: python3
---A Class Vect¶
Adapted from: “Object-Oriented Programming Using C++” by Ira Pohl (Addison - Wesley)
Program that demonstrates a dynamic array or vector in C++¶
In file vect1.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 "vect1.h"
using namespace std; // Added. MK
int main()
{
vect a(60), b[20];
b[1].element(5) = 7;
cout << b[1].element(5) << " 1 element 5\n";
for (int i = 0; i <= a.ub(); ++i) {
a.element(i) = i;
cout << a.element(i) << "\t";
}
cout << endl; // Added for output clarity
}In file vect1.h
/*********************************************************************
Filename: vect1.h
Chapter: 6 Object Creation and Destruction
Section: 6.5 A class vect
Compiler: Borland C++ Version 5.0 Summer 1996
Object Oriented Programming Using C++, Edition 2 By Ira Pohl
*********************************************************************/
//Implementation of a safe array type vect
#include <iostream> // changed iostream.h to iostream. MK
#include <assert.h>
//Implementation of a safe array type vect
class vect {
public:
explicit vect(int n = 10);
~vect() { delete []p; }
int& element(int i); //access p[i]
int ub() const {return (size - 1);} //upper bound
private:
int* p;
int size;
};
vect::vect(int n) : size(n)
{
assert(n > 0);
p = new int[size];
assert(p != 0);
}
int& vect::element(int i)
{
assert (i >= 0 && i < size);
return p[i];
}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_6_5_A_Class_Vect"os.chdir(code_dir)build_command = os.system("g++ vect1.cpp -w -o vect1")Execution Process¶
exec_status = os.system("./vect1")7 1 element 5
0 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