{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

c++9 - C C t t t H t H 2 C t X 3 C t t 7 x E 7 x E template...

Info icon This preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon
第第第 第第第 第第第第第第第第 第第第第 C++ 语语语语语语
Image of page 1

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

View Full Document Right Arrow Icon
2 C++ 语语语语语语 语语语语 语语 第第第第第第 第第 第第第 第第第第第第第
Image of page 2
3 C++ 语语语语语语 语语语语 语语 第第第第第第第 第第第第 第第第
Image of page 3

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

View Full Document Right Arrow Icon
4 C++ 语语语语语语 语语语语 语语 第第第第 第第第第第第第第第第第第第第第第第第第第第第第第第第第第第第第第第第第第第第第第第第第第 第第 第第第第第 template <typename 语语语 > 语语语语
Image of page 4
5 C++ 语语语语语语 语语语语 语语 第第第第第第第第第 #include<iostream> using namespace std; template<typename T > T abs( T x) { return x<0?-x:x; } int main() { int n=-5; double d=-5.5; cout<<abs( n )<<endl; cout<<abs( d )<<endl; } 第第第第第 5 5.5
Image of page 5

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

View Full Document Right Arrow Icon
6 C++ 语语语语语语 语语语语 语语 第第第第第第第第第第第 第第第第第第 abs() 第第第第第第第第第第第第第第第第第第第第第第第第第第第第第第 abs(n) 第第第第第 n int 第第第第第第第第第第第第第第 T int 第第第第第第第第第第第第第第第第第第第第第第第第第第第第第第第第 int abs(int x) { return x<0?-x:x; }
Image of page 6
7 C++ 语语语语语语 语语语语 语语 第第第第第第 第第第第第第第第第第第第第第第第第第第第第第第第第第第第第第第第第第第第第第第第第第 第第第第第第第第第第第第第第第第第第第第第第第第第第第第第第第第第第 i i
Image of page 7

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

View Full Document Right Arrow Icon
8 C++ 语语语语语语 语语语语 语语 第第第第第第 第第第第 template < 语语语语语 > class 语语 { 语语语语语 } 第第第第第第第第第第第第第第第第第第第第第第第第第第第第 template < 语语语语语 > 语语语 语语 <T>:: 语语语语语语语语
Image of page 8
9 C++ 语语语语语语 语语语语 语语 9-2 第第第第第第第 #include <iostream> #include <cstdlib> using namespace std; // 第第第 Student struct Student { int id; // 第第 float gpa; // 第第第 }; i i
Image of page 9

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

View Full Document Right Arrow Icon
template <class T> // 第第第第第第第第第第第第第第第第第 class Store { private: T item; // 第第第第第第第第第第第 int haveValue;// 第第第第 item 第第第第第第第第 public: Store(void); // 第第第第第第第第第第第第第第 GetElem(void); // 第第第第第第 void PutElem(T x);// 第第第第第第 }; // 第第第第第第第第第第第 template <class T> Store<T>::Store(void): haveValue(0) {} 10
Image of page 10
template <class T> // 第第第第第第第第第 T Store<T>::GetElem(void) { // 第第第第第第第第第第第第第第第第第第第 if (haveValue == 0) { cout << "No item present!" << endl; exit(1); } return item; // 第第 item 第第第第第第 } template <class T> // 第第第第第第第第第 void Store<T>::PutElem(T x) { // 第 haveValue 第第 TRUE 第第第 item 第第第第第第 haveValue++; item = x; // 第 x 第第第 item } 11
Image of page 11

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

View Full Document Right Arrow Icon
int main() { Student g= {1000, 23}; Store<int> S1, S2; Store<Student> S3; Store<double> D; S1.PutElem(3); S2.PutElem(-7); cout << S1.GetElem() << " " <<S2.GetElem()<<endl; S3.PutElem(g); cout << "The student id is " <<S3.GetElem().id<<endl; cout << "Retrieving object D " ; cout << D.GetElem() << endl; // 第第第第 D 第第第第第 // 第第 D 第第第第第 , 第第第第第 D.GetElement() 第第第 } 12
Image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern