#include <stdio.h>
#include <stdlib.h>
double out = 0.0;
class vector
{
private:
int n;
double *y;
public:
vector(); //constructor 1
vector(int m); //constructor 2
vector(const vector &);
~vector() {
delete y;
} //destructor
friend vector operator + (const vector & a, const vector & b);
friend vector operator - (const vector & a, const vector & b);
vector & operator = (const vector & a);
double & operator[](int i) const;
double & operator()(int i) const;
void set(double *z);
void print();
// Prefix inkrement
vector operator++();
// Postfix inkrement
vector operator++(int);
};
vector::vector() {
n = 10;
y = new double[n]; //constructor 1
for (int i = 0; i < n; i++) y[i] = 0.0;
}
vector::vector(int m) {
n = m;
y = new double[n]; //constructor 2
for (int i = 0; i < n; i++) y[i] = 0.0;
}
//operator[]
inline double & vector::operator[](int i) const {
if ((i >= n) || (i < 0)) {
printf("index exceeds vector dimensions");
return out;
}
return y[i];
}
vector::vector(const vector & a) {
n = a.n;
y = new double[n];
for (int i = 0; i < n; i++)y[i] = a[i];
}
//operator()
double & vector::operator()(int i) const {
if ((i > n) || (i < 1)) {
printf("index exceeds vector dimensions");
exit(1);
}
return *(y + i - 1);
}
//operator=
vector & vector::operator = (const vector & a){
delete y;
n = a.n;
y = new double[n];
for (int i = 0; i < n; i++) y[i] = a.y[i];
return (*this);
}
//operator++(int)
vector vector::operator++(int) {
vector t(n);
for (int i = 0; i < n; i++) {
t[i] = y[i];
y[i] += 1;
}
return t;
}
//operator++()
vector vector::operator++() {
for (int i = 0; i < n; i++) y[i] += 1;
return (*this);
}
//print()
void vector::print() {
for (int i = 0; i < n; i++) printf("%e\n", y[i]);
}
//operator+,operator-
vector operator + (const vector & a, const vector &b) {
vector t(a.n);
for (int i = 0; i < a.n; i++)t.y[i] = b.y[i] + a.y[i];
return (t);
}
vector operator - (const vector & a, const vector &b) {
vector t(a.n);
for (int i = 0; i < a.n; i++)t.y[i] = a.y[i] - b.y[i];
return (t);
}
//set()
void vector::set(double *z) {
for (int i = 0; i < n; i++) y[i] = z[i];
}
void main() {
vector a(5), b(5), c(5);
double x[5];
double d;
for (int i = 0; i < 5; i++) x[i] = 1;
a.set(x);
b.set(x);
printf("b \n");
b.print();
printf("c=a+b++ \n");
c = a + b++;
c.print();
printf("b++ \n");
b.print();
c = a - b;
printf("c=a-b\n");
c.print();
c[3] = 4;
d = c(4);
}
#include <stdlib.h>
double out = 0.0;
class vector
{
private:
int n;
double *y;
public:
vector(); //constructor 1
vector(int m); //constructor 2
vector(const vector &);
~vector() {
delete y;
} //destructor
friend vector operator + (const vector & a, const vector & b);
friend vector operator - (const vector & a, const vector & b);
vector & operator = (const vector & a);
double & operator[](int i) const;
double & operator()(int i) const;
void set(double *z);
void print();
// Prefix inkrement
vector operator++();
// Postfix inkrement
vector operator++(int);
};
vector::vector() {
n = 10;
y = new double[n]; //constructor 1
for (int i = 0; i < n; i++) y[i] = 0.0;
}
vector::vector(int m) {
n = m;
y = new double[n]; //constructor 2
for (int i = 0; i < n; i++) y[i] = 0.0;
}
//operator[]
inline double & vector::operator[](int i) const {
if ((i >= n) || (i < 0)) {
printf("index exceeds vector dimensions");
return out;
}
return y[i];
}
vector::vector(const vector & a) {
n = a.n;
y = new double[n];
for (int i = 0; i < n; i++)y[i] = a[i];
}
//operator()
double & vector::operator()(int i) const {
if ((i > n) || (i < 1)) {
printf("index exceeds vector dimensions");
exit(1);
}
return *(y + i - 1);
}
//operator=
vector & vector::operator = (const vector & a){
delete y;
n = a.n;
y = new double[n];
for (int i = 0; i < n; i++) y[i] = a.y[i];
return (*this);
}
//operator++(int)
vector vector::operator++(int) {
vector t(n);
for (int i = 0; i < n; i++) {
t[i] = y[i];
y[i] += 1;
}
return t;
}
//operator++()
vector vector::operator++() {
for (int i = 0; i < n; i++) y[i] += 1;
return (*this);
}
//print()
void vector::print() {
for (int i = 0; i < n; i++) printf("%e\n", y[i]);
}
//operator+,operator-
vector operator + (const vector & a, const vector &b) {
vector t(a.n);
for (int i = 0; i < a.n; i++)t.y[i] = b.y[i] + a.y[i];
return (t);
}
vector operator - (const vector & a, const vector &b) {
vector t(a.n);
for (int i = 0; i < a.n; i++)t.y[i] = a.y[i] - b.y[i];
return (t);
}
//set()
void vector::set(double *z) {
for (int i = 0; i < n; i++) y[i] = z[i];
}
void main() {
vector a(5), b(5), c(5);
double x[5];
double d;
for (int i = 0; i < 5; i++) x[i] = 1;
a.set(x);
b.set(x);
printf("b \n");
b.print();
printf("c=a+b++ \n");
c = a + b++;
c.print();
printf("b++ \n");
b.print();
c = a - b;
printf("c=a-b\n");
c.print();
c[3] = 4;
d = c(4);
}