Template Implicit function overload. Function overload Function overload double ssqq(double & a, double & b) { return(a*b);} float ssqq(float & a, float.

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick Recursion rA recursive function must have at least two parts l A part that solves a simple case of the.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Operator overloading redefine the operations of operators
For(int i = 1; i
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Vectors, lists and queues
Computer Science 1620 Function Overloading. Review Question: suppose I have the following function: can I call this function with an integer? yes – compiler.
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
CS 112 Introduction to Programming
Functions Prototypes, parameter passing, return values, activation frams.
Class 15 - Overhead 11Object Oriented Programming Using C #define t_circle 1 #define t_rectangle 2 struct circle_shape {short type; double x,y; double.
Class operators. class polynomial class polynomialpolynomial class polynomial { protected: int n; double *a; public: polynomial(){}; …..; // constructor.
1 Data Structures - CSCI 102 CS102 C++ Operator Overloading Prof Tejada.
Review What is a virtual function? What can be achieved with virtual functions? How to define a pure virtual function? What is an abstract class? Can a.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
第三次小考. #include using namespace std; int aaa(int *ib,int a1,int a2) { int u,v; int m=(a1+a2)/2; if(a1==a2)return ib[a1]; u=aaa(ib,a1,m); cout
Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such.
Operator Overloading Fundamentals
C/c++ 4 Yeting Ge.
1 Class Vehicle #include #define N 10../.. 2 Class Vehicle class vehicle { public: float speed; char colour[N+1]; char make[N+1];
Esempio Polimorfismo1 // Definition of abstract base class Shape #ifndef SHAPE_H #define SHAPE_H class Shape { public: virtual double area() const { return.
AU/MITM/1.6 By Mohammed A. Saleh 1. Arguments passed by reference  Until now, in all the functions we have seen, the arguments passed to the functions.
A) 80 b) 53 c) 13 d) x 2 = : 10 = 3, x 3 = 309.
Function Overloading Can enables several function Of same name Of different sets of parameters (at least as far as their types are concerned) Used to create.
Function Overloading Can enables several functions Of same name Of different sets of parameters (at least as far as their types are concerned) Used to.
C++ Typecasting. Math Library Functions.. Operator / Operands A = x + y.
C++ Programming for Graphics Lecture 4 Overloading, Default Values and Inline.
CS 1400 March 21, 2007 Odds and Ends Review. Reading to the end of a file Example test.txt : … Suppose we need.
Functions Pass by Value Pass by Reference IC 210.
Function Overloading Overloading  Using same name for more than one function  Example: ovldmean.cpp.
Templates Overload function: define more than one function With same function name Different parameter type Different type Different number of parameter.
EEL 3801 Part VIII Fundamentals of C and C++ Programming Template Functions and Classes.
 Review structures  Program to demonstrate a structure containing a pointer.
Passing Data - by Reference Syntax && double Pythagorus(double &, double &); Pythagorus(height, base); & & double Pythagorus(double& a, double& b) function.
Functions Pass by reference, or Call by reference Passing addresses Use of & part 3.
Templates Class Templates Used to specify generic class types where class members data types can be specified as parameters, e.g. here is a generic List.
1 CSC241: Object Oriented Programming Lecture No 25.
Lecture 4 Function example. Example1 int max (int a, int b) { int c; if (a > b) c = a; else c = b; return (c); } void main ( ) {int x, y; cin>>x>>y; cout.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
C++ Templates 1. Why Use Templates? C++ requires variables, functions, classes etc with specific data types. However, many algorithms (quicksort for example)
Lecture 17: 4/4/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Introduction to Programming Lesson 3. #include #include main ( ) { cout
` $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
#define #include<iostream> using namespace std; #define GO
Introduction Rules Types Programs
Programming Fundamentals
Zhen Jiang West Chester University
نوع داده هاي انتزاعي Abstract Data Types
دور المجتمع المدني في ترشيد وتحسين كفاءة الطاقة آفاق جديدة ومتجددة...
2. Primary and Subfunctions
Name: Rubaisha Rajpoot
Introduction to Programming
Counting Loops.
Starting Out with C++: From Control Structures through Objects
CMSC 202 Lesson 22 Templates I.
CSC 102 Chabli Boler.
The Run-Time Stack and Reference Parameters
G1. G1 g2 g3 g4 g5 g6 g8 g10 g11 g12 g14.
الوحدة الرابعة البرمجة وصياغة حل المسائل البرمجة وأهميتها أهداف الدرس الأول مفهوم البرمجة. الفرق بين المبرمج ومستخدم البرنامج. الحاجة إلى البرامج.
Object-Oriented Programming (OOP) Lecture No. 37
Default Arguments.
Изразеното в настоящата презентация мнение обвързва единствено автора и не представлява официално становище на Комисията за финансов надзор Данил Джоргов.
Introduction to Programming
Function Overloading.
Templates I CMSC 202.
Type Conversion It is a procedure of converting one data type values into another data type In C programming language we are having two types of type conversion.
TOPIC: FUNCTION OVERLOADING
Unit-1 Introduction to Java
Introduction to Algorithms and Programming COMP151
Presentation transcript:

Template Implicit function overload

Function overload Function overload double ssqq(double & a, double & b) { return(a*b);} float ssqq(float & a, float & b) {return(a*b);} int ssqq(int & a, int & b) {return(a*b);} main() { int na=3, nb=-5; float ra=2.1, rb=-3.4; double da=4.7, db=-5.6; cout << "integer : " << ssqq(na, nb) << "\n"; cout << "float : " << ssqq(ra, rb) << "\n"; cout << "double : " << ssqq(da, db) << "\n"; system("pause"); return(0); }

template template TYPE1 ssqq(TYPE1 & a, TYPE1 & b) { return (TYPE1)(a*b);} main() { int na=3, nb=-5; float ra=2.1, rb=-3.4; double da=4.7, db=-5.6; cout << "integer : " << ssqq(na, nb) << "\n"; cout << "float : " << ssqq(ra, rb) << "\n"; cout << "double : " << ssqq(da, db) << "\n"; system("pause"); return(0); } template.. or template class can be used for both class and variable type, but type name only refers to variable types

Function template with two types Function template with two types template double ssqq(TYPE1 & a, TYPE2 & b) { return (double)(a*b);} main() { int na=3, nb=-5; float ra=2.1, rb=-3.4; double da=4.7, db=-5.6; cout << "2 integer : " << ssqq(na, nb) << "\n"; cout << "float * double : " << ssqq(ra, db) << "\n"; cout << "double * float : " << ssqq(da, rb) << "\n"; system("pause"); return(0); }

Template-function overload Template-function overload template double ssqq(TYPE1 & a, TYPE2 & b) { return((double)(a*b));} template TYPE3 ssqq(TYPE3 & a) { return((TYPE3)(a*a)); } main() { int na=3, nb=-5; float ra=2.1, rb=-3.4; double da=4.7, db=-5.6; cout << "2 integer : " << ssqq(na, nb) << "\n"; cout << "float & integer : " << ssqq(ra, nb) << "\n"; cout << "float & double : " << ssqq(rb, db) << "\n"; cout << "1 integer : " << ssqq(na) << "\n"; cout << "1 float : " << ssqq(ra) << "\n"; cout << "1 double: " << ssqq(da) << "\n";

Template class Template class template class position { XTYPE x, y, z; public: position(){}; position(XTYPE px, XTYPE py, XTYPE pz) { x = px; y=py; z=pz; } position(position & r) {x=r.x; y=r.y; z=r.z;} //member functions XTYPE r() {return (XTYPE)(sqrt((double)(x*x+y*y+z*z)));} void setx(XTYPE rx){ x=rx;} void sety(XTYPE ry){ y=ry;} void setz(XTYPE rz){ z=rz;} XTYPE getx() { return(x);} XTYPE gety() { return(y);} XTYPE getz() { return(z);} XTYPE phi(); XTYPE theta(); void print(); };

Member function of template class template void position ::print() { printf("x = %lf y = %lf z = %lf\n",(double)x,(double)y,(double)z); } template XTYPE position ::theta() { return( (XTYPE) acos(z/this->r())); } template XTYPE position ::phi() { return( (XTYPE) atan2(this->y, this->x) ); }

Using template class position r(3.0F, 4.0F, 5.0F); r.print(); r.setx(3.0); r.print(); printf("r=%f phi=%f theta=%f\n",r.r(), 180.0F*r.phi()/MyPi, 180.0F*r.theta()/MyPi); position ra[5]; // declare a position array for (i=0; i<5; i++) { ra[i].setx( (double)i); ra[i].sety( (double)i); ra[i].setz( (double)i); }

Complex class Complex class #include using namespace std; complex ca, cb(1.2, 6.1); ca = complex (2.4, 5.3); Headfile: Declare:

Complex functions cout << "ca = " << ca << "\n"; cout <<" real = " << ca.real() << "\n"; cout <<" imag = " << ca.imag() << "\n"; cout << "cb = " << cb << "\n"; cout << "ca+cb = " << (ca+cb) << "\n"; cout << "ca*cb = " << ca*cb << "\n"; cout << ca << "conjugate = "<< conj(ca) << "\n"; cout << "exp(ca) = " << exp(ca) << "\n"; cout << "abs(ca) = " << abs(ca) << "\n";

complex array complex cc[4]; cc[0] = ca = complex (0.0, 1.0); for (i=1; i<4; i++) cc[i] = cc[i-1] * ca; for (i=0; i<4; i++) cout << i << " : " << cc[i] << "\n";

Practice Using Newton ’ s method to solve a root for a polynomial. Write the polynomial in a form of template class to allow complex coefficients. dx = f(x) / df(x); x = x – dx; All these variables will be double or complex numbers.

template class polynomial { }; template class polynomial { }; template class polynomial { int n; XTYPE *a; public: polynomial(int); //constructor polynomial(int, XTYPE *); //member functions XTYPE f (const XTYPE) const; XTYPE df(const XTYPE) const; void print() const; void coef(XTYPE *); inline void coef(int n, XTYPE x) {a[n] = x;} }; // end of class definition

constructor template polynomial ::polynomial (int nn, XTYPE *c) { int i; n = nn; a = new XTYPE[n+1]; if (a==NULL) n=0; else { for (i=0; i <= n; i++) a[i] = c[i]; }

I/O format for complex number complex a(1.0, 2.0); cout << a << “ \n ” Output 結果 : (1, 2) complex b; cin >> b; 輸入格式為 : (1.0, 2.0)

練習二. 將 Matrix class 加上 template for double and complex 練習二. 將 Matrix class 加上 template for double and complex typedef complex CMPLX; // short-hand for complex template class Matrix { protected: XT *xpt; int nrow, ncol; public:.... // constructor, member function and frieds. };

Matrix (){;} Matrix (const int, const int); Matrix (const Matrix &); Matrix (int, int,XT*); ~Matrix () { this->nrow = this->ncol = 0; delete [] this->xpt; } Constructors XT is the symbol defined at the declaration of a class object. class Matrix m1(3,4), m2; class matrix > c1, c2;

template Matrix ::Matrix (int n, int m, XT *ap) { int i; if ((n > 0) && (m > 0) ) { this->xpt = new XT [n*m]; if (this->xpt != NULL) { this->nrow = n; this->ncol = m; for (i=0; i xpt[i] = ap[i]; } Example of constructor code built outside the class

inline int row() const { return this->nrow;} inline int col() const { return this->ncol;} inline int dim() const { return (this->nrow * this->ncol) ;} inline int CV(const int i,const int j) const { return(ncol*i + j);} inline XT get(const int i, const int j) const { return this->xpt[CV(i,j)]; } inline void set(const int i, const int j, XT xx) { this->xpt[CV(i,j)] = xx; } inline XT get(const int i) const { return this->xpt[i]; } inline void set(const int i, XT xx) { this->xpt[i] = xx; } Matrix getcol(int) const; // get a column form the matrix Matrix getrow(int) const; // get a row from the matrix Manipulation member functions

template Matrix Matrix ::getcol(int nr) const { int i; Matrix colv(this->nrow, 1); for (i=0; i nrow; i++) colv.xpt[i] = this->get(i, nr); return colv; } Example of Member function built outside the class

double norm() const; // remain double inline double abs() const {return sqrt(this->norm());} Matrix transport() const; Matrix adjoint() const; // for complex -- transport and conjugate Utility member functions adjoint: 共軛轉置矩陣 程式中若須要區分 double 和 complex, 可用 sizeof(XT). 當 sizeof (XT) <= 8 adjoint = transport In code norm: 先把 xx cast 轉成 complex, 取共軛再相乘, 乘完再取 real. template double Matrix ::norm() XT xx; xx = this->xpt[i]; sum = sum + (conj((CMPLX)xx) * xx).real(); 不然就必須把 Matrix 和 Matrix 分開寫兩個程式.

Declaration inside class: Matrix &operator= (const Matrix ); Matrix &operator+=(const Matrix ); Matrix &operator-=(const Matrix ); Unary operator Code outside the class: template Matrix &Matrix ::operator+=(const Matrix m2) { int i; if ((this->ncol==m2.ncol) && (this->nrow==m2.nrow)) for (i=0; i dim(); i++) this->xpt[i] += m2.xpt[i]; else { this->ncol = this->nrow = 0; delete [] this->xpt; } return *this; } // the code is same for both double and complex.

template friend ostream &operator ); template friend Matrix operator+(Matrix, Matrix ); template friend Matrix operator-(Matrix, Matrix ); template friend Matrix operator*(double, Matrix ); template friend Matrix operator*(CMPLX, Matrix ); template friend Matrix operator*(Matrix, double); template friend Matrix operator*(Matrix, CMPLX); template friend Matrix operator*(Matrix, Matrix ); Template defined friend functions 每一個用任何 class 或 變數 取代 TT 所得的函數, 都被當作是 class Matrix 的 friend function.

template Matrix operator+(Matrix m1, Matrix m2) { int i; Matrix m3; if ((m1.nrow == m2.nrow) && (m1.ncol == m2.ncol)) { m3 = m1; for (i=0; i<m3.dim(); i++) m3.xpt[i] += m2.xpt[i]; } else { m3.~Matrix (); } return m3; } Template friend operator built outside the class

#include #include "ytluMatrix2.h “ // CMPLX = complex using namespace std; int main() { int n=3, m=4, i; CMPLX xx[12]; for (i=0; i<12; i++) xx[i] = CMPLX((double)i, (double)i*0.5 ); Matrix a1(n, m, xx), a3, a4; Matrix a2(m, n, xx); cout << " a1 = " << a1 ; cout << " 2nd COlumn of a1 " << a1.getcol(1); cout << " 2nd row of a1 " << a1.getrow(1); cout << " a2 = " << a2; a3 = a4 = a1; cout << "a1 = " << a1 << "a3 = " << a3 << "a4 = " << a4; cout << "a1 + a3 + a4 = " << (a1+a3+a4); cout << "a1 * (1i) " << a1 * CMPLX(0.0, 1.0); cout << "a1 * 2.0 " << a1 * 2.0; cout << "(1i) * a1 *2 " << CMPLX(0.0,1.0)*a1*2.0; cout << "a1 * a2" << a1 * a2; system("pause"); return 0; } Exmaple of program using Matrix class

#include #include "ytluMatrix2.h “ // typedef complex CMPLX using namespace std; int main() { int n=3, m=4; double xx[12] = { 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12.}; Matrix a1(3, 4, xx), a3, a4; Matrix a2(4, 3, xx); cout << " a1 = " << a1 ; cout << " 2nd Column of a1 " << a1.getcol(1); cout << " 2nd row of a1 " << a1.getrow(1); cout << " a2 = " << a2; a3 = a4 = a1; cout << "a1 = " << a1 << "a3 = " << a3 << "a4 = " << a4; cout << "a1 + a3 + a4 = " << (a1+a3+a4); cout << "a3 - a4 " << (a3-a4); cout << "a1 - a3 -a4 " << (a1-a3-a4); cout << "a1 * (1i) " << a1 * CMPLX(0.0, 1.0); cout << "a1 * 2.0 " << a1 * 2.0; cout << "(1i) * a1 *2 " << CMPLX(0.0,1.0)*a1*2.0; cout << "a1 * a2" << a1 * a2; cout << "(1i)*a1 *a2" << CMPLX(0.0, 1.0)*a1*a2; system("pause"); return 0; } Example of using Matrix