Chapter10 - Programming with Classes Engineering Problem...

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

View Full Document Right Arrow Icon
Engineering Problem Solving with C++, Etter/Ingber Chapter 10 Additional Topics in Programming with Classes Engineering Problem Solving with C++, Second Edition. J. Ingber 1 Programming with Classes ± Introduction to Generic Programming ± Recursion ± Class Templates ± Inheritance ± virtual Methods Engineering Problem Solving with C++, Second Edition. J. Ingber 2 INTRODUCTION TO GENERIC PROGRAMMING function templates overloading operators friend functions Engineering Problem Solving with C++, Second Edition. J. Ingber 3 Generic Programming ± Generic programming supports the implementation of a type independent algorithm. ± Type independent algorithms can be defined in C++ using the keyword template, and at least one parameter. ± The compiler generates a unique instance of the template for each specified type. Engineering Problem Solving with C++, Second Edition. J. Ingber 4
Background image of page 1

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

View Full DocumentRight Arrow Icon
Function Templates ± A function template is a parameterized function definition. Syntax : template<typename identifier [, typename identifier,…] > return-type function name(parameter list) { …} Example : template<typename Dtype> { Dtype temp = a; a = b; b = temp; } Engineering Problem Solving with C++, Second Edition. J. Ingber 5 Instantiating Templates Example : void swapTwo(Dtype& a, Dtype& b); //prototype int main() { double x(1.0), y(5.7); char ch1('n'), ch2('o'); swapTwo(x,y); swapTwo(ch1, ch2); cout << x << ',' << y << endl << ch1 << ch2 << endl; ... Engineering Problem Solving with C++, Second Edition. J. Ingber 6 Output: 5.7, 1.0 on Engineering Problem Solving with C++, Second Edition. J. Ingber 7 Overloading Operators ± Allows a programmer defined data type to be used in the same way that a predefined data type is used. ± Definitions for overloaded operators are included in the class definition. keyword operator is used followed by the operator. the operator must be one of the predefined C++ operators. Only predefined operators may be overloaded. ± All predefined operators except ( . :: .* ?: sizeof ) may be overloaded. Engineering Problem Solving with C++, Second Edition. J. Ingber 8 Complex Number Class ± A complex number is a number that has two components; the real component and the imaginary component. a + bi ± Arithmetic is defined as follows: (a + bi) + (c + di) = (a + c) + (b + d)i (a + bi) - (c + di) = (a - c) + (b - d)i (a + bi) * (c + di) = (ac - bd) + (ad + bc)i (a + bi) / (c + di) = (ac + bd) / (c**2+d**2) + [ (bc -ad) /(c**2+d**2)]i
Background image of page 2
Engineering Problem Solving with C++, Second Edition. J. Ingber 9 Class Declaration class complex { public: complex(); complex(double,double); void print(ostream&); void input(istream&); complex operator+(complex) const; complex operator-(complex) const; complex operator*(complex) const; complex operator/(complex) const; private: double real, imag; }; Engineering Problem Solving with C++, Second Edition. J. Ingber 10 Implementation - constructors
Background image of page 3

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

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

This note was uploaded on 10/23/2011 for the course ECE 224 taught by Professor Wangxin during the Spring '09 term at SUNY Stony Brook.

Page1 / 18

Chapter10 - Programming with Classes Engineering Problem...

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

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