Class operators. class polynomial class polynomialpolynomial class polynomial { protected: int n; double *a; public: polynomial(){}; …..; // constructor.

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

Chapter 18 Vectors and Arrays
Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Template Implicit function overload. Function overload Function overload double ssqq(double & a, double & b) { return(a*b);} float ssqq(float & a, float.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Introduction to Programming Lecture 39. Copy Constructor.
Introduction to Programming Lecture 31. Operator Overloading.
指標 Pointers.
Operator Overloading Fundamentals
Introduction to Java Programming Lecture 13 Classes I OO Programming.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 10: Pointers by.
Multidimensional Array Outline Two-Dimensional Arrays Lengths of Two-Dimensional Arrays Multidimension Arrays [Sample code]
Introduction to Java Programming Lecture 10 Array I Declaring, Creating, and Initializing Arrays.
Classes Separating interface from implementation
A[0] a[1] pa ???? *pa ppa *ppa Address:4 byte Double:8 byte.
Introduction to Java Programming Lecture 11 Array II Multidimensional Arrays.
Visual C++重點複習.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
Class Byteline Ustyugov Dmitry MDSP November, 2009.
CMSC 202 Lesson 13 Pointers & Dynamic Memory. Warmup Overload the subtraction operator on two Money objects as a FRIEND! class Money { public: _______________.
Copy Constructors Fall 2008 Dr. David A. Gaitros
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
Data Structures (1st Exam). 1.[5] Suppose there are only two constructors for a class, one that passes in a single integer parameter called amount, for.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 4 – August 30, 2001.
CMSC 202 Lesson 14 Copy and Assignment. Warmup Write the constructor for the following class: class CellPhone { public: CellPhone(int number, const string&
1 Inside the Vector Class: with additional needed methods CPS212CPS212 Gordon College.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Dynamically Allocated Arrays December 4, Skip the Rest of this PowerPoint.
The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Data Structures Using C++1 Chapter 3 Pointers Dr. Liu.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
What happens... l When a function is called that uses pass by value for a class object like our dynamically linked stack? StackType MakeEmpty Pop Push.
資料結構(計財系).
C++ 程式語言. 2 C++ 程式語言 – 大綱 1. 指標 2. 結構與類別 3. 標準函式庫 (STL)
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
1 Memory as byte array Pointers Arrays relationship to pointers Operator ‘new’ Operator ‘delete’ Copy ctor Assignment operator ‘this’ const pointer Allocating.
1 Linked Lists Assignment What about assignment? –Suppose you have linked lists: List lst1, lst2; lst1.push_front( 35 ); lst1.push_front( 18 ); lst2.push_front(
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Learners Support Publications Constructors and Destructors.
Constructors and Destructors
Pointers and Dynamic Arrays
Learning Objectives Pointers as dada members
Linked Lists Chapter 6 Section 6.4 – 6.6
CISC181 Introduction to Computer Science Dr
Lesson 14 Copy and Assignment
Class Operations Pointer and References with class types
Class: Special Topics Copy Constructors Static members Friends this
LinkedList Class.
Memberwise Assignment / Initialization
This pointer, Dynamic memory allocation, Constructors and Destructor
group work #hifiTeam
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Foundational Data Structures
Stacks as Linked lists top_node typedef Stack_entry Node_entry;
Pointers & Dynamic Memory
Constructors and Destructors
Indirection.
Speaker: Liu Yu-Jiun Date: 2009/4/15
CS148 Introduction to Programming II
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Instructor: Dr. Michael Geiger Spring 2019 Lecture 23: Exam 2 Preview
Destructors, Copy Constructors & Copy Assignment Operators
Destructors, Copy Constructors & Copy Assignment Operators
C++ support for Object-Oriented Programming
SPL – PS3 C++ Classes.
Lesson 13 Pointers & Dynamic Memory
Presentation transcript:

Class operators

class polynomial class polynomialpolynomial class polynomial { protected: int n; double *a; public: polynomial(){}; …..; // constructor ~polynomial(); // destructor //member functions... double f (const double) const; // polynomial value // friend functions and operators friend polynomial operator+ (const polynomial, const polynomial); friend polynomial operator- (const polynomial, const polynomial); friend polynomial operator* (const double, const polynomial); }; // end of class definition

constructors 1. polynomial::polynomial(){}; // an empty polynomial 2. polynomial::polynomial(const int nn){ n = nn; a = new double[n+1];} // allocate space for coefficients, but don ’ t assign values. 3. polynomial::polynomial(const int nn, const double *c) { int j; n = nn; a = new double [n+1]; for (j=0; j<=n; j++) a[j] = c[j]; } // assign value for each coefficients. 4. polynomial::polynomial(const polynomial & p1) { int j; n = p1.n; a = new double [n+1]; for (j=0; j<=n; j++) a[j] = p1.a[j]; } // copy data from another polynomial

Constructor Allow multiple constructors, same principle as function overload. Allow multiple constructors, same principle as function overload. Function name of constructor: Function name of constructor: polynomial:: polynomial( arguments) polynomial:: polynomial( arguments) { … ; … ; … ; } { … ; … ; … ; } No function type, no return variables. No function type, no return variables.

Dynamic allocation in c++ In C: #include pointer variable = malloc(total_memory_size); or pointer varibale = calloc(dim, size_of_each_varible); 釋放記憶 : void free(void *ptr); In C++: double pointer = new double [dimension] ; 釋放記憶 : delete [] pointer;

Copy constructor polynomial::polynomial(const polynomial & p1) { int j; this->n = p1.n; this->a = new double [n+1]; for (j=0; j a[j] = p1.a[j]; } // copy data from another polynomial

Pass by reference Pass by value: double ssqq(double a) { a = 2.0 * a; return(a*a); } Call: double aa = ssqq(a); // a 不變 Pass by address: double ssqq(double *a) { *a = 2.0 * (*a); return((*a)*(*a)); } Call: double aa = ssqq(&ra); // ra  2(ra) Pass by reference: double ssqq(double &a) { a = 2.0 * a; return(a*a); } Call: double aa = ssqq(a); // a  2a

Deconstructor class polynomial class polynomial { protected: int n; double *a; double *a; public: … ; … ; public: … ; … ; ~polynomial{ delete [] this->a; } ~polynomial{ delete [] this->a; } }; }; polynomial p1(5, acoef); // 宣告 5 階多項式 polynomial p1(5, acoef); // 宣告 5 階多項式 p1.~polynomial(); // 釋放 p1.a[6] 的記憶空間 p1.~polynomial(); // 釋放 p1.a[6] 的記憶空間

Assignment operator= Assignment operator is a member function: p1 = p2; // syntax equivalent to p1.operator=(p2) polynomial& polynomial::operator=(const polynomial & p1) { int i; if (this->n > 0) delete [] this->a; this->n = p1.n; this->a = new double[this->n + 1]; for (i=0; i a[i] = p1.a[i]; return(*this); }

operators polynomial operator* (const double rr, const polynomial p1) { int i; polynomial p2(p1); for (i=0; i<=p1.n; i++) p2.a[i] *= rr; return(p2); } Operators: +, -, *, /, % >, =, >, << [], ++, --, +=, -= *=, %=

operators Operator is a friend function. Operator is a friend function. polynomial p1(5, acoef), p2(4, bceof); polynomial p1(5, acoef), p2(4, bceof); polynomial p3 = p1 + p2; polynomial p3 = p1 + p2; 可以寫成 p3 = operator+ (p1, p2) 可以寫成 p3 = operator+ (p1, p2) Function overlaod 的原則可以運用到 operator 的定 義. Function overlaod 的原則可以運用到 operator 的定 義. example double * polynomial; example double * polynomial; polynomial * double; polynomial * double; polynomial * polynomial; polynomial * polynomial;

Polynomial operator + polynomial operator+(polynomial p1, polynomial p2) { int i; if (p1.n > p2.n) { polynomial pp(p1); for (i=0; i<=p2.n; i++) pp.a[i] += p2.a[i]; return(pp); } else { polynomial pp(p2); for (i=0; i<=p1.n; i++) pp.a[i] += p1.a[i]; return(pp); }

polynomial operator - polynomial operator - if (p1.n > p2.n) { polynomial pp(p1); for (i=0; i<=p2.n; i++) pp.a[i] -= p2.a[i]; return(pp); } else { polynomial pp(p2); pp = (-1.0) * pp; for (i=0; i<=p1.n; i++) pp.a[i] += p1.a[i]; return(pp); }

Polynomial operator* polynomial operator* (const polynomial p1, const polynomial p2) { int i, j, n3=p1.n+p2.n; double sum; polynomial p3(n3); for (i=0; i<=n3; i++) { sum = 0.0; for (j=0; j<=i; j++) if ((j<=p1.n) && (i-j)<= p2.n) sum += (p1.a[j]*p2.a[i-j]); p3.a[i] = sum; } return(p3); }

Polynomial operator/ n3 = p1.n – p2.n; if (n3 < 0) { n3 = 0; polynomial p3(n3); p3.a[0] = 0.0; return(p3); } else { polynomial p3(n3); polynomial pp(p1); for (i=n3; i>=0; i--) { ncur = pp.n - n3 + i; att = pp.a[ncur] / p2.a[p2.n]; p3.a[i] = att; for (j=0; j<=p2.n; j++) pp.a[pp.n-n3+i-p2.n+j] -= att*p2.a[j]; } return(p3); }

Polynomial operator % poynomial p1 % polynomial p2 留下餘數 p3, 階數小於 p2. 程式與上頁相同, 但回傳是留下來的餘 多項式. n3 = p2.n – 1; 但要留意 p3.a 的高次項係數為零時, 要將階數下降 n3 = p2.n – 1; while ((fabs(pp.a[n3]) 0) ) { n3 --; }

practice Write a polynomial class with constructors, member functions and operators for the c++ programs. The class require the following minimal features: programs. 1. Copy constructor. 2. Operators: +, -, *, /, %.

Class Matrix class Matrix { protected: int nrow, ncol; double *xpt; public: constructors; member_functions; friend_operators; }; nrow: number of rows ncol: number of columns xpt : pointer for memory reallocation for matrix elements Example

Plan for construction of Matrix class General matirx (n x m) Template with double and complex Square matrixColumn vector Row vector Base class Derived class

注意要點 1. 每一個 class 編輯器都會自動設兩個 member functions: 一個 desrtuctor, ~class(). 另一個是 class::operator=(class). Destructor 釋放 member 中的 array. 而等號運算是 copy 另一個 class 再 把 members 的值轉過去. 2. 但是目前 Matrix 的結構中, xpt[n*m] 的 array 並非是 class member. 因此編輯器預設的 destrcutor 不會消滅 array xpt[]. 而等號算符會 將兩個 Matrix 的 xpt 指標, 指向同一記憶位址, 產生錯誤運算. 3. 因此, 等號算符和 destructor 都必須再重新 overload.

Destructor ~Matrix() { delete [] this->xpt; this->ncol = this->nrow = 0; } Matrix & Matrix::operator=(Matrix b) { if (this->dim() > 0) delete [] this->xpt; // 去除舊陣列 this->ncol = b.ncol; this->nrow = b.nrow; if (this->dim() > 0 ) { this->xpt = new double [this->dim()]; // 建新陣列 if (this->xpt != NULL) for (i=0; i dim(); i++) this->xpt[i] = b.xpt[i]; //copy b 矩陣值 else this->ncol = this->nrow = 0; } return *this; }