lecture35-apr26 - Lecture 35 Assignment 7 Q&A OO design...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Lecture 35 – OO design tips – Matrix implementation is a 2D array of ints(dynamic) • Exam #3 – On Wed. May 5 • There will be a lecture on the last day of class and exams returned and course evals • Topics of the day – Finish class implementation issues – Inheritance; e.g. • a Dog is-a Mammal • a Springer Spaniel is-a Dog • a Square Matrix is-a Matrix Dog Springer Spaniel Is-a UML Model – Assignment 7 Matrix Data structures used for representing a matrix (hidden) •Add •Subtract •Multiply •… lots more Driver program •User interaction •declares and uses matrix variables to test the functions in Matrix •Reports results •Runs experiments in DEBUG mode Matrix class sketch class Matrix { private: int ** mat_array; // data structure for a dynamic matrix int rows; int cols; static int ** create(int, int); public: Matrix(int nRows, int nCols) // constructor { rows = nRows; cols = nCols; mat_array = create(rows, cols); } • // lots more public functions and operators Dynamic Array Creation in C++ static int ** create (int nrows, int ncols) { // dynamically create and return a nrowsXncols // matrix with elements initialized to random #s int **values; values = new int* [nrows]; for(int i=0; i < nrows; i++) *(values + i) = new int[ncols]; for(int i=0; i < nrows; i++) for(int j=0; i < ncols; i++) values[i][j] = rand( ) % MAXNUM +1; return values; } In the Matrix class constructor you can call create(n,m); Using Object Pointers int main() { ExampleClass objects[4] = {1,2,3,4}; ExampleClass *p; p = objects; // get address of objects cout << p->getVal() << endl; // which object is output? p = p +2; cout << p->getVal() << endl; // which object is output? return 0; } class ExampleClass { private: int i; public: ExampleClass(int j) { i=j; } int getVal() { return i; } }; for (int i=0; i<4; i++) {cout << objects[i].getVal() <<endl;} How to Rewrite Object Assignment Use the “operator=” function and a “copy constructor” Write two member functions: copy – set all your data members so that they are a copy of somebody else destroy – deallocate any local memory you use inside your class class Fred { public: // a better copy constructor
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 3

lecture35-apr26 - Lecture 35 Assignment 7 Q&amp;A OO design...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online