/////////////////////////////////////////////////////////////////////// //typeError.cpp int main(){ x = 3; return 0; } /////////////////////////////////////////////////////////////////////// int main(){ int x, y; x = 0; y = 0; int z = x + y; return 0; } /////////////////////////////////////////////////////////////////////// //hello.cpp #include #include using namespace std; int main(){ string s; s = "Hi ya'll!"; cout << s << endl; return 0; } /////////////////////////////////////////////////////////////////////// //scope.cpp #include using namespace std; int main(){ int x = 3; cout << "x = " << x << endl; { int x = 4; int y = 7; cout << "x = " << x << " y = " << y << endl; } cout << "x = " << x << " y = " << y << endl; return 0; } /////////////////////////////////////////////////////////////////////// //forWhileEx.cpp #include #include using namespace std; int main(int argc, char* argv[]){ int start = atoi(argv[1]); int bound = atoi(argv[2]); //compute the sum with a for loop int sum = 0; for(int i = start; sum <= bound; i++) sum += i; cout << "The sum from the for loop is " << sum << endl; //compute the sum with a while loop sum = 0; int i = start; while(sum <= bound){ sum += i; i++; } cout << "The sum from the while loop is " << sum << endl; return 0; } /////////////////////////////////////////////////////////////////////// //breakEx.cpp #include using namespace std; int main(){ for(int i = 0; i < 3; i++) for(int j= 0; j < 10; j++){ if(j == 2) break; cout << j << " "; } cout << endl; return 0; } /////////////////////////////////////////////////////////////////////// //simplePointer.cpp #include using namespace std; int main(){ int* p = 0; int i = 1; p = &i; cout << "Address of i = " << &i << endl; cout << "Address of p = " << &p << endl; cout << "Value of p = " << p << endl; cout << "Value stored in p = " << *p << endl; cout << "Value stored in p = " << p[0] << endl; return 0; } /////////////////////////////////////////////////////////////////////// //constPointer.cpp int main(){ int x = 1, y = 1; const int* p1 = &x; int* const p2 = &x; p1 = &y; p2 = &y;//error p1[0] += 1;//error! p2[0] += 1; return 0; } /////////////////////////////////////////////////////////////////////// //matrixPointer.cpp #include using namespace std; int main(){ int nRows = 2, nCols = 3; double** pM = 0; pM = new double*[nRows]; for(int i = 0; i < nRows; i++) pM[i] = new double[nCols]; cout << "The value of pM is " << pM << endl; for(int i = 0; i < nRows; i++) cout << "The address for pM[" << i << "] is "<< &pM[i] << endl; for(int i = 0; i < nRows; i++){ cout << "The value of pM[" << i << "] is " << pM[i] << endl; for(int j = 0; j < nCols; j++){ cout << " The address of pM[" << i << "][" << j << "] is "<< &pM[i][j] << endl; } cout << endl; } for(int i = 0; i < nRows; i++){ for(int j = 0; j < nCols; j++){ pM[i][j] = i*j; cout << "pM[" << i << "][" << j << "]=" << pM[i][j] << " "; } cout << endl; } //memory clean up for(int i = 0; i < nRows; i++) delete[] pM[i]; delete[] pM; return 0; } /////////////////////////////////////////////////////////////////////// //square.cpp #include using namespace std; double square(double x); int main(){ double x; cout << "Enter a number "; cin >> x; cout << square(x) << endl; return 0; } double square(double x){ return x*x; } /////////////////////////////////////////////////////////////////////// //passByVal.cpp #include using namespace std; void f(int i){ i += 1; } int main(int argc, char* argv[]){ int i = 0; f(i); cout << "The value of i is " << i < using namespace std; void f(int* pi){ pi[0] += 1; } int main(int argc, char* argv[]){ int i = 0; int* pi = &i; f(pi); cout << "The value of i is " << i < using namespace std; void f(int& j){ j++; cout << &j << endl; } int main(){ int i = 2; cout << &i << endl; f(i); cout << i << endl; int& j = i; int k = j; cout << i << " " << j << " " << k << endl; cout << &i << " " << &j << " " << &k << endl; k++; j = k; cout << i << " " << j << " " << k << endl; cout << &i << " " << &j << " " << &k << endl; return 0; } /////////////////////////////////////////////////////////////////////// //squareOverload.cpp #include using namespace std; double square(double x); int square(int x); int main(){ double x; cout << "Enter a number "; cin >> x; cout << square(x) << endl; int y; cout << "Enter an integer "; cin >> y; cout << square(y) << endl; return 0; } double square(double x){ cout << "From double version" << endl; return x*x; } int square(int x){ cout << "From int version" << endl; return x*x; } /////////////////////////////////////////////////////////////////////// //power.cpp #include using namespace std; class Power{ int K;//integer power public: Power(int k); double xToTheK(double x); }; Power::Power(int k){ if(k >= 1){ K = k; } else{ cout << "The power must be an integer >= 1!" << endl; } } double Power::xToTheK(double x){ double y = 1.; for(int i = 1; i <= K; i++) y = x*y; return y; } int main(){ int k; double x; cout << "Enter an integer power "; cin >> k; Power* pPow = new Power(k); cout << "Enter a number to raise to this power "; cin >> x; cout << pPow->xToTheK(x) << endl; cout << "Enter another number to raise to this power "; cin >> x; cout << pPow->xToTheK(x) << endl; delete pPow; return 0; } /////////////////////////////////////////////////////////////////////// //powerClass.h #ifndef POWER_H #define POWER_H class Power{ int K;//integer power public: Power(){K = 1;}//default constructor Power(int k); double xToTheK(double x); }; #endif //powerClass.cpp #include #include "powerClass.h" using namespace std; Power::Power(int k){ if(k >= 1){ K = k; } else{ cout << "The power must be an integer >= 1!" << endl; } } double Power::xToTheK(double x){ double y = 1.; for(int i = 1; i <= K; i++) y = x*y; return y; } //powerClassDriver.cpp #include #include "powerClass.h" using namespace std; int main(){ int k; double x; cout << "Enter an integer power "; cin >> k; Power powObj(k); cout << "Enter a number to raise to this power "; cin >> x; cout << powObj.xToTheK(x) << endl; return 0; } powerClass : powerClassDriver.o powerClass.o g++ -Wall powerClassDriver.o powerClass.o -o powerClass powerClass.o : powerClass.cpp powerClass.h g++ -c -Wall powerClass.cpp powerClassDriver.o : powerClassDriver.cpp powerClass.h g++ -c -Wall powerClassDriver.cpp /////////////////////////////////////////////////////////////////////// //structEx.cpp #include using namespace std; struct X{ int K; }; int main(){ X x; x.K = 1; cout << x.K << endl; return 0; } /////////////////////////////////////////////////////////////////////// //thisEx.cpp #include using namespace std; class X{ public: int K; X(){}; X(int k){K = k;} X* getThis(){return this;} X& getThisRef(){return *this;} }; int main(){ X w(1); cout << "w address = " << &w << endl; cout << "w.K = " << w.K << endl; X* x = w.getThis(); cout << "x value = " << x << endl; x->K += 1; cout << "w.K = " << w.K << endl; X& y = w.getThisRef(); cout << "y address = " << &y << endl; y.K += 1; cout << "w.K = " << w.K << endl; X z = *w.getThis(); cout << "z address = " << &z << endl; z.K =+ 1; cout << "w.K = " << w.K << endl; return 0; } /////////////////////////////////////////////////////////////////////// //constRef.cpp #include ; using namespace std; struct X{ int K; void printK(){ cout << K << endl; } }; void g(const X& x){ x.K = 1; } int main(){ X x; g(x); x.printK(); return 0; } /////////////////////////////////////////////////////////////////////// //constPToP.cpp #include using namespace std; void f(const int* const* y){ int* z = new int[1]; *z = 2; //*y = z; cout << **y << endl; //**y += 1; } int main(){ int k = 0; int* x = &k; int** y = &x; f(y); return 0; } /////////////////////////////////////////////////////////////////////// //constEx1.cpp #include using namespace std; struct X{ const int I; int J; X(int i, int j) : I(i) { J = j; } int getI(){return I;} int getJ(){return J;} //void setI(int i){I = i;} }; int main(){ X x(1, 2); cout << x.getI() << " " << x.getJ() << endl; x.J = 3; //x.I = 4; return 0; } /////////////////////////////////////////////////////////////////////// //constEx2.cpp #include using namespace std; class X{ const int I; static const int J = 2; public: X(int i) : I(i) {} int getI(){return I;} int getJ(){return J;} }; int main(){ X x1(1); cout << x1.getI() << " " << x1.getJ() << endl; X x2(2); cout << x2.getI() << " " << x2.getJ() << endl; return 0; } /////////////////////////////////////////////////////////////////////// //constEx3.cpp #include using namespace std; struct X{ int I; X(int i) : I(i) {} int getI(){return I;} int getIConst() const {return I;} }; int main(){ X x1(1); const X x2(2); cout << x1.getI() << endl; cout << x1.getIConst() << endl; cout << x2.getIConst() << endl; //cout << x2.getI() << endl; return 0; } /////////////////////////////////////////////////////////////////////// //constEx4.cpp #include using namespace std; struct X{ int I, J; public: X(int i, int j) : I(i), J(j) {} X(const X& x){ I = x.I; J = x.J; } X operator*(int a); X operator*(int a) const; }; X X::operator*(int a){ X x(a*I, a*J); return x; } X X::operator*(int a) const { X x(a*I, a*J); cout << "const version of *" << endl; return x; } X operator*(int a, const X& x){ return x*a; } int main(){ X x(1, 2); cout << (x*2).I << " " << (x*2).J << endl; cout << (2*x).I << " " << (2*x).J << endl; return 0; } /////////////////////////////////////////////////////////////////////// //circular.cpp struct Y;//forward declaration struct X{ Y* A; X(){} X(Y* a){A = a;} Y& funcX(Y& y){return y;}; }; struct Y{ X B; Y(){} Y(X b){B = b;} X funcY(X x){return x;}; }; int main(){ X x; Y y; return 0; } /////////////////////////////////////////////////////////////////////// //stringEx1.cpp #include #include using namespace std; int main(){ string s1; s1 = "Hi "; string s2("y'all"); s1 += s2; cout << s1 + "!" << endl; cin >> s1; //cin.get(); getline(cin, s2); cout << s1 + " " + s2 + "!" << endl; return 0; } /////////////////////////////////////////////////////////////////////// //stringEx2.cpp #include #include using namespace std; int main(){ string s; getline(cin, s); cout << "The length of s is " << s.size() << endl; cout << "The fifth element of s is " << s[4] << endl; cout << "The letter a first occurs at " << s.find("a") << endl; cout << "The last a occurs at " << s.find_last_of("a") << endl; cout << "The letter z first occurs at " << s.find("z") << endl; const char* str; str = s.c_str(); for(int i = 0; i < s.size(); i++) cout << str[i]; cout << endl; if(str[s.size()] == '\0') cout << "str is a C-style string" << endl; return 0; } /////////////////////////////////////////////////////////////////////// //namespaceEx.cpp #include ; using std::cout; using std::endl; int main(){ cout << "You don't need the entire standard namespace to use cout " << "or end a line." << endl; return 0; } /////////////////////////////////////////////////////////////////////// //exitEx.cpp #include #include using std::cout; using std::endl; using std::cin; int main(){ int n; cout << "Enter an integer < 5" << endl; cin >> n; if(n >= 5){ cout << "Please follow instruction! " << endl; exit(1); } cout << "n is " << n << endl; return 0; } /////////////////////////////////////////////////////////////////////// //assertEx.cpp #include #include using std::cout; using std::endl; using std::cin; int main(){ int n; cout << "Enter an integer < 5" << endl; cin >> n; assert(n <= 5); cout << "n is " << n << endl; return 0; } /////////////////////////////////////////////////////////////////////// //exitEx.cpp #include #include using std::cout; using std::endl; using std::cin; int main(){ int n; std::cout << "Enter an integer < 5" << endl; cin >> n; if(n >= 5){ cout << "Please follow instruction! " << endl; cout << "The error occurred on line " << (__LINE__ - 2) << " of file " << __FILE__ << " in function " << __FUNCTION__ << endl; exit(1); } cout << "n is " << n << endl; return 0; } /////////////////////////////////////////////////////////////////////// //tryCatchEx1.cpp #include #include using std::cout; using std::endl; using std::cin; void intCheck(const int i){ if(i >= 5) throw std::string("bad intCheck() integer >= 5 is not allowed"); cout << "Your integer is " << i << endl; cout << "Enter a new integer < 5 (q to quit)" << endl; } int main(){ int n; cout << "Enter an integer < 5 (q to quit)" << endl; while(cin >> n){ try{ intCheck(n); } catch(std::string s){ cout << s << endl; cout << "Enter a new integer < 5 (q to quit)" << endl; continue; } } cout << "All done" << endl; return 0; } /////////////////////////////////////////////////////////////////////// //tryCatchEx2.cpp #include #include #include using std::cout; using std::endl; using std::string; void intCheck(const int i) throw(std::out_of_range){ if(i >= 5) throw std::out_of_range("intCheck"); cout << "Your integer is " << i << endl; } int main(){ string s; s = "Hi ya'll!"; int n; std::cin >> n; try{ cout << s.insert(n, "there ") << endl; intCheck(n); } catch(std::out_of_range e){ cout << "Out of range " << e.what() << endl; } cout << "All done" << endl; return 0; } /////////////////////////////////////////////////////////////////////// //clockEx.cpp #include #include using std::cout; using std::endl; int main(){ double x, sec; clock_t b = clock(); while(x < 1000) x += .00001; sec = (double)(clock() - b)/(double)CLOCKS_PER_SEC; cout << "The while loop took " << sec << " seconds to compute x = " << x << endl; return 0; } /////////////////////////////////////////////////////////////////////// //simpleMatrix.h #ifndef SIMPLEMATRIX_H #define SIMPLEMATRIX_H class Matrix{ //class members int nRows, nCols; double** pA; void pointerCheck() const; void pointerCheck(int i) const; public: //constructors and destructor Matrix(int nrows = 0, int ncols = 0, double a = 0.); Matrix(int nrows, int ncols, const double* const* pa); ~Matrix(); void printMatrix() const; int getnRows() const {return nRows;} int getnCols() const {return nCols;} }; #endif //simpleMatrix.cpp #include #include #include #include "simpleMatrix.h" using std::cout; using std::endl; Matrix:: Matrix(int nrows, int ncols, double a){ nRows = nrows; nCols = ncols; //set pA to null pointer in default case if(nRows == 0 || nCols == 0){ pA = 0; return; } pA = new(std::nothrow) double*[nRows]; pointerCheck(); for(int i = 0; i < nRows; i++){ pA[i] = new(std::nothrow) double[nCols]; pointerCheck(i); for(int j = 0; j < nCols; j++){ if(i == j) pA[i][j] = a; else pA[i][j] = 0.; } } } Matrix:: Matrix(int nrows, int ncols, const double* const* pa){ nRows = nrows; nCols = ncols; pA = new(std::nothrow) double*[nRows]; pointerCheck(); for(int i = 0; i < nRows; i++){ pA[i] = new(std::nothrow) double[nCols]; pointerCheck(i); } for(int i = 0; i < nRows; i++) for(int j = 0; j < nCols; j++) pA[i][j] = pa[i][j]; } Matrix::~Matrix(){ if(pA != 0){ for(int i = 0; i < nRows; i++) delete[] pA[i]; delete[] pA; } } void Matrix::printMatrix() const { for(int i = 0; i < nRows; i++){ for(int j = 0; j < nCols; j++) cout << " " << pA[i][j] << " "; cout << endl; } } void Matrix::pointerCheck() const { if(pA == 0){ cout << "Memory allocation for pA failed" << endl; exit(1); } } void Matrix::pointerCheck(int i) const { if(pA[i] == 0){ cout << "Memory allocation for pA[" << i << "] failed" << endl; exit(1); } } //simpleMatDriver.cpp #include "simpleMatrix.h" #include using std::cout; using std::endl; int main(){ Matrix m1(2, 2, 1); cout << "Here is an identity matrix " << endl; m1.printMatrix(); double** p = new double*[3]; for(int i = 0; i < 3; i++){ p[i] = new double[2]; for(int j = 0; j < 2; j++) p[i][j] = (double)(i + 1)*(j + 1); } Matrix* m2 = new Matrix(3, 2, p); cout << "Here is a matrix initialized by a pointer-to-pointer" << endl; m2->printMatrix(); return 0; } /////////////////////////////////////////////////////////////////////// //copyEx.cpp #include "simpleMatrix.h" #include using std::cout; using std::endl; void g(Matrix A){}; int main(){ Matrix m(2, 2, 1.); g(m); cout << "The destructor will now be called." << endl; return 0; } /////////////////////////////////////////////////////////////////////// //equalEx.cpp #include "simpleMatrix.h" #include using std::cout; using std::endl; int main(){ Matrix m1(1., 2, 2); Matrix m2; m2 = m1; cout << "The destructor will now be called." << endl; return 0; } /////////////////////////////////////////////////////////////////////// //simpleMatrix.h #ifndef SIMPLEMATRIX_H #define SIMPLEMATRIX_H #include "simpleVector.h" class Matrix{ //class members int nRows, nCols; double** pA; void pointerCheck() const; void pointerCheck(int i) const; public: //constructors and destructor Matrix(int nrows = 0, int ncols = 0, double a = 0.); Matrix(int nrows, int ncols, const double* const* pa); Matrix(const Matrix& A); ~Matrix(); Matrix& operator=(const Matrix& B); Matrix operator+(const Matrix& B) const; Matrix& operator+=(const Matrix& B); Matrix operator*(const Matrix& B) const; Matrix operator*(double b) const; Vector operator*(const Vector& v) const; const double* operator[](int i) const {return pA[i];} Matrix trans() const; void printMatrix() const; int getnRows() const {return nRows;} int getnCols() const {return nCols;} }; Matrix operator*(double, const Matrix& A); #endif //simpleMatrix.cpp #include #include #include #include "simpleMatrix.h" using std::cout; using std::endl; Matrix:: Matrix(int nrows, int ncols, double a){ nRows = nrows; nCols = ncols; //set pA to null pointer in default case if(nRows == 0 || nCols == 0){ pA = 0; return; } pA = new(std::nothrow) double*[nRows]; pointerCheck(); for(int i = 0; i < nRows; i++){ pA[i] = new(std::nothrow) double[nCols]; pointerCheck(i); for(int j = 0; j < nCols; j++){ if(i == j) pA[i][j] = a; else pA[i][j] = 0.; } } } Matrix:: Matrix(int nrows, int ncols, const double* const* pa){ nRows = nrows; nCols = ncols; pA = new(std::nothrow) double*[nRows]; pointerCheck(); for(int i = 0; i < nRows; i++){ pA[i] = new(std::nothrow) double[nCols]; pointerCheck(i); } for(int i = 0; i < nRows; i++) for(int j = 0; j < nCols; j++) pA[i][j] = pa[i][j]; } Matrix::Matrix(const Matrix& A){ nRows = A.nRows; nCols = A.nCols; pA = new(std::nothrow) double*[nRows]; pointerCheck(); for(int i = 0; i < nRows; i++){ pA[i] = new(std::nothrow) double[nCols]; pointerCheck(i); } for(int i = 0; i < nRows; i++) for(int j = 0; j < nCols; j++) pA[i][j] = A.pA[i][j]; } Matrix::~Matrix(){ if(pA != 0){ for(int i = 0; i < nRows; i++) delete[] pA[i]; delete[] pA; } } Matrix& Matrix::operator=(const Matrix& A){ if(this == &A)//avoid self assignment return *this; //if dimensions match we can just overwrite; otherwise.... if(nRows != A.nRows||nCols != A.nCols){ if(pA != 0){//check first before releasing memory for(int i = 0; i < nRows; i++)//release current memory delete[] pA[i]; delete[] pA; } //define/redefine object's members nRows = A.nRows; nCols = A.nCols; pA = new(std::nothrow) double*[nRows]; pointerCheck(); for(int i = 0; i < nRows; i++){ pA[i] = new(std::nothrow) double[nCols]; pointerCheck(i); } } for(int i = 0; i < nRows; i++) for(int j = 0; j < nCols; j++) pA[i][j] = A.pA[i][j]; return *this; } Matrix Matrix::operator+(const Matrix& B) const { if(nRows != B.nRows || nCols != B.nCols){ cout << "Bad row and/or column dimensions in +!" << endl; exit(1); } Matrix temp(nRows, nCols); for(int i = 0; i < nRows; i++) for(int j = 0; j < nCols; j++) temp.pA[i][j] = this->pA[i][j] + B.pA[i][j]; return temp; } Matrix& Matrix::operator+=(const Matrix& B){ if(nRows != B.nRows || nCols != B.nCols){ cout << "Bad row and/or column dimensions in +=!" << endl; exit(1); } for(int i = 0; i < nRows; i++) for(int j = 0; j < nCols; j++) pA[i][j] += B.pA[i][j]; return *this; } Matrix Matrix::operator*(const Matrix& B) const { if(nCols != B.nRows){ cout << "Bad row and column dimensions in *!" << endl; exit(1); } Matrix C(nRows, B.nCols);//matrix of all 0s for(int i = 0; i < nRows; i++) for(int j = 0; j < B.nCols; j++) for(int k = 0; k < B.nRows; k++) C.pA[i][j] += pA[i][k]*B.pA[k][j]; return C; } Matrix Matrix::operator*(double b) const { Matrix C(nRows, nCols);//matrix of all 0s for(int i = 0; i < nRows; i++) for(int j = 0; j < nCols; j++) C.pA[i][j] = b*pA[i][j]; return C; } Matrix operator*(double b, const Matrix& A){ return A*b; } Vector Matrix::operator*(const Vector& v) const { double temp; Vector c(nRows);//vector of all 0s for(int i = 0; i < nRows; i++){ temp = 0; for(int j = 0; j < nCols; j++) temp += pA[i][j]*v[j]; c.pA[i]=temp; } return c; } Matrix Matrix::trans() const { Matrix B(nCols, nRows, 0.); for(int i = 0; i < nCols; i++) for(int j = 0; j < nRows; j++) B.pA[i][j] = pA[j][i]; return B; } void Matrix::printMatrix() const { for(int i = 0; i < nRows; i++){ for(int j = 0; j < nCols; j++) cout << " " << pA[i][j] << " "; cout << endl; } } void Matrix::pointerCheck() const { if(pA == 0){ cout << "Memory allocation for pA failed" << endl; exit(1); } } void Matrix::pointerCheck(int i) const { if(pA[i] == 0){ cout << "Memory allocation for pA[" << i << "] failed" << endl; exit(1); } } //matDriver.cpp #include "simpleMatrix.h" #include using std::cout; using std::endl; void g(const Matrix& A){ cout << "The function g was called" << endl; }; int main(){ double** pMat = new double*[2]; for(int i = 0; i < 2; i++){ pMat[i] = new double[2]; for(int j = 0; j < 2; j++) pMat[i][j] = (double)((i + 1)*(j + 1) + j); } Matrix m1(2, 2, pMat); cout << "A matrix initialized from a pointer-to-pointer" << endl; m1.printMatrix(); for(int i = 0; i < 2; i++) delete[] pMat[i]; delete[] pMat; g(m1); Matrix m2; m2 = m1; cout << "A copy of the matrix obtained with =" << endl; m2.printMatrix(); cout << "The sum of the last 2 matrices is" << endl; (m1 + m2).printMatrix(); cout << "The product of the first matrix and its transpose is" << endl; (m1*m1.trans()).printMatrix(); cout << "The first matrix left multiplied by 2 is" << endl; (2*m1).printMatrix(); cout << "The (1, 1) element of the first matrix is " << m1[1][1] << endl; return 0; } /////////////////////////////////////////////////////////////////////// //simpleVector.h #ifndef VECTOR_H #define VECTOR_H class Vector{ double* pA; int nRows; void pointerCheck() const; public: Vector(int nrows = 0, double b = 0.); Vector(int nrows, const double* pa); Vector(const Vector& v); ~Vector(); Vector& operator=(const Vector& v); double operator[](int i) const {return pA[i];} void printVec() const; int getnRows() const {return nRows;} //friends friend class Matrix; }; #endif //simpleVector.cpp #include #include #include #include "simpleVector.h" using std::cout; using std::endl; Vector::Vector(int nrows, double b){ nRows = nrows; if(nRows == 0){ pA = 0; return; } pA = new(std::nothrow) double[nRows]; pointerCheck(); for(int i = 0; i < nRows; i++) pA[i] = b; } Vector::Vector(int nrows, const double* pa){ nRows = nrows; pA = new(std::nothrow) double[nRows]; pointerCheck(); for(int i = 0; i < nRows; i++) pA[i] = pa[i]; } Vector::Vector(const Vector& v){ nRows = v.nRows; pA = new(std::nothrow) double[nRows]; pointerCheck(); for(int i = 0; i < nRows; i++) pA[i] = v.pA[i]; } Vector::~Vector(){ if(pA != 0) delete[] pA; } Vector& Vector::operator=(const Vector& v){ if(this == &v)//avoid self assignment return *this; //define/redefine object's members if(nRows != v.nRows){ if(pA != 0) delete[] pA; nRows = v.nRows; pA = new(std::nothrow) double[nRows]; pointerCheck(); } for(int i = 0; i < nRows; i++) pA[i] = v.pA[i]; return *this; } void Vector::pointerCheck() const { if(pA == 0){ cout << "Memory allocation for pA failed" << endl; exit(1); } } void Vector::printVec() const { for(int i = 0; i < nRows; i++) cout << " " << pA[i] << " " << endl; } //matVecDriver.cpp #include #include "simpleVector.h" #include "simpleMatrix.h" using std::cout; using std::endl; int main(){ double** pMat = new double*[2]; double* pVec = new double[3]; for(int i = 0; i < 2; i++){ pMat[i] = new double[3]; for(int j = 0; j < 3; j++){ pMat[i][j] = (double)((i + 1)*(j + 1) + j); pVec[j] = (double)(j + 1); } } Matrix m(2, 3, pMat); cout << "The matrix m" << endl; m.printMatrix(); Vector v(3, pVec); cout << "The vector v" << endl; v.printVec(); cout << "The product of m and v is" << endl; (m*v).printVec(); return 0; } /////////////////////////////////////////////////////////////////////// //simpleOut.cpp #include #include int main(int argc, char** argv){ std::ofstream outFile; outFile.open(argv[1]); for(int i = 0; i < atoi(argv[2]); i++){ for(int j = 0; j < atoi(argv[3]); j++) outFile << (i + 1)*(j + 1) << " "; outFile << std::endl; } outFile.close(); outFile.open(argv[1], std::ios_base::app); outFile << "End of file" << std::endl; outFile.close(); return 0; } /////////////////////////////////////////////////////////////////////// //simpleTemp.cpp #include using std::cout; using std::endl; using std::cin; template class simpleTemp{ T x; public: simpleTemp(){ cout << "Enter a value for x " << endl; cin >> x; } T getX(); }; template T simpleTemp::getX() { return x; } int main (){ simpleTemp y; cout << sizeof(y.getX()) << " " << y.getX() << endl; simpleTemp z; cout << sizeof(z.getX()) << " " << z.getX() << endl; return 0; } /////////////////////////////////////////////////////////////////////// //fileOut.cpp #include #include #include template class fileOut{ char* fName; bool App; public: fileOut(char* fileName, bool app = 0); ~fileOut(){}; void write(int nrows, int ncols, const T* const* pData) const; }; //method definitions template fileOut::fileOut(char* fileName, bool app){ fName = fileName; App = app; } template void fileOut::write(int nrows, int ncols, const T* const* pData) const { if(App == 0){ std::ofstream outFile; outFile.exceptions(std::ofstream::failbit | std::ofstream::badbit); try{ outFile.open(fName); } catch(std::ofstream::failure e){ std::cout << "Error opening output file: " << e.what() << std::endl; } for(int i = 0; i < nrows; i++){ for(int j = 0; j < ncols; j++) outFile << " " << pData[i][j] << " "; outFile << std::endl; } outFile.close(); } else{ std::ofstream outFile(fName, std::ios::app); if(!outFile.is_open()){ std::cout << "Error opening output file!" << std::endl; exit(1); } for(int i = 0; i < nrows; i++){ for(int j = 0; j < ncols; j++) outFile << " " << pData[i][j] << " "; outFile << std::endl; } outFile.close(); } } int main(int argc, char** argv){ int** pI = new int*[2]; double** pD = new double*[2]; for(int i = 0; i < 2; i++){ pI[i] = new int[3]; pD[i] = new double[3]; for(int j = 0; j < 3; j++){ pI[i][j] = (i + 1)*(j + 1); pD[i][j] = (double)(i + 1)/(double)(j + 1); } } fileOut outInt(argv[1], 0); outInt.write(2, 3, pI); //Now append to argv[2] fileOut<> outDouble(argv[2], 1); outDouble.write(2, 3, pD); return 0; } /////////////////////////////////////////////////////////////////////// //funTemp.cpp #include #include template void write(const T* pT, int nrows, int ncols, char* fName, bool app){ if(app){ std::ofstream fileOut(fName, std::ios::app); for(int i = 0; i < nrows; i++){ for(int j = 0; j < ncols; j++) fileOut << pT[i][j] << " "; fileOut << std::endl; } fileOut.close(); } else{ std::ofstream fileOut(fName); for(int i = 0; i < nrows; i++){ for(int j = 0 ; j < ncols; j++) fileOut << pT[i][j] << " "; fileOut << std::endl; } fileOut.close(); } } int main(int argc, char** argv){ int** pI = new int*[2]; for(int i = 0; i < 2; i++){ pI[i] = new int[2]; for(int j = 0; j < 2; j++) pI[i][j] = (i + 1)*(j + 1); } write(pI, 2, 2, argv[1], 0); double** pD = new double*[2]; for(int i = 0; i < 2; i++){ pD[i] = new double[3]; for(int j = 0; j < 3; j++) pD[i][j] = (double)((i + 1)*(j + 1))/6.; } write(pD, 2, 2, argv[1], 1); for(int i = 0; i < 2; i++){ delete[] pI[i]; delete[] pD[i]; } delete[] pI; delete[] pD; return 0; }