Lec8 - Lecture 8 Template Classes PIC 10B Todd Wittman Sec...

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

View Full Document Right Arrow Icon
1 Lecture 8: Template Classes PIC 10B Todd Wittman Sec 22.3 : Template Classes b We can use templates to design general-purpose classes. b We've actually already seen this in action with the vector class. vector<double> v(10); v[3]=-42.3; vector<string> s(20); s[3]="Yoda"; b For example, this week you're designing an IntMatrix class to store a matrix of integers. b It would be awesome if we could design just one class Matrix to hold a matrix of integers, doubles, bools, strings, or whatever we want. Matrix<double> D(3,4); D(1,2)=-42.3; Matrix<string> S(3,4); S(1,2)="Yoda"; b By the way, this is next week's HW assignment. ..
Background image of page 1

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

View Full Document Right Arrow Icon
2 Template Classes b List all template types before the class declaration. template <typename T1, typename T2, . ..> class ClassName { public: functions. .. private: data. .. } b When defining a member function, write the template line above each function. (Copy and paste.) b Also, list all types in <> after the class name. template <typename T1, typename T2,. ..> returnType ClassName<T1,T2,. ..>::function_name ( params ) { b When you call the constructor, tell it what types you want. Just like we do with the vector class. ClassName<int,string,.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 6

Lec8 - Lecture 8 Template Classes PIC 10B Todd Wittman Sec...

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

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