Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Vector+-OpLoad
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
//g++ 7.4.0 //Vector : overloading addition and subtraction operator //code credits for default constructor,copy constructor,stream insertion and extraction operators,destructor,assignment operator go to John R. Hubbard; codes for addition and subtraction operators and test driver are created by Rezaul Hoque on September 08,2021; contact: jewelmrh@yahoo.com //note: codes shared by Rezaul Hoque on rextester are not for sale; they are created and shared to facilitate the algorithm learning process; many like Hoque use this platform to practice programming ;Rezaul hopes his contribution helps others to fine tune their learning; #include <iostream> class Vector { friend std::ostream& operator<<(std::ostream &, const Vector &);//stream extraction operator friend std::istream& operator>>(std::istream &, Vector &);//stream insertion operator friend Vector operator+(const Vector &, const Vector&);//binary addition operator friend Vector operator-(const Vector &, const Vector&);//binary subtraction operator public: Vector (int ,double );//default constructor Vector (const Vector &);//copy constructor ~Vector();//destructor const Vector & operator=(const Vector &);//assignment operator double& operator[](int) const;//subscript operator int size; double * data; }; std::ostream& operator<<(std::ostream& ostr,const Vector& v) { int l; ostr<<"( "; for(l=0;l<v.size-1;l++){ ostr<<v[l]<<","; if((l+1)%8 == 0) std::cout<<"\n"; } return ostr<<v[l]<<")\n"; } std::istream& operator>>(std::istream& istr,const Vector& v) { for(int l=0;l<=v.size-1;l++) { std::cout<<l<<":"; istr>>v[l]; } return istr; } Vector::Vector(int sz=1,double t=0.0): size(sz) { data = new double[size]; for(int l=0;l<size;l++) data[l]=t; } Vector:: Vector(const Vector & v): size(v.size) { data = new double[v.size]; for(int l=0;l<v.size;l++) data[l]=v.data[l]; } Vector::~Vector() { delete [] data; data = NULL; size=0; } const Vector& Vector::operator=(const Vector & v) { if(&v != this) { delete [] data; size = v.size; data = new double[size]; for(int l=0;l<size;l++) data[l]=v.data[l]; } return *this; } double& Vector:: operator[](int I) const { return data[I]; } Vector operator+(const Vector & v,const Vector& w) { Vector z(4,0.0); for(int l=0;l<z.size;l++){ z.data[l] = v.data[l] + w.data[l];} return z; } Vector operator-(const Vector & v,const Vector& w) { Vector z(4,0.0); for(int l=0;l<z.size;l++){ z.data[l] = v.data[l] - w.data[l];} return z; } int main() { Vector vec1(4,3.0),vec2(4,2.0),vec3(4),vec4(4); vec3=vec1+vec2; std::cout<<" vec3:\n"; std::cout<<vec3; std::cout<<" vec4:\n"; vec4=vec1-vec2; std::cout<<vec4; return 0; }
g++
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
Compilation time: 0.76 sec, absolute running time: 0.24 sec, cpu time: 0.02 sec, memory peak: 5 Mb, absolute service time: 1,14 sec
edit mode
|
history
|
discussion
vec3: ( 5,5,5,5) vec4: ( 1,1,1,1)