The 2nd Chapter C + + template technology

Source: Internet
Author: User

/*The 2nd Chapter C + + Template Technology 2.1 function template 2.2 class template 2.3 Template fully special 2.4 function template overloading 2.5 class template Inheritance 2.6 Chapter Summary*///The 2nd Chapter C + + template technology//2.1 function Template--------------------------------------------------------------------------------------------------- -//P#include <stdio.h>Template<classT>T Max (t A, T b) {returnA> b?a:b;}intMainvoid) {printf ("%d\n", Max <int> (3,Ten)); printf ("%f\n", Max <Double> (16.9,2.8)); return 0;}// -#include <stdio.h>Template<classT1,classT2>T1 Max (T1 t1, T2 T2) {returnStatic_cast<t1> (t1> T2?t1:t2);}intMainvoid) {printf ("%f\n", Max (10.9,3)); return 0;}//Class 2.2 Template---------------------------------------------------------------------------------------------------- //p, my test#include <cstdio>#include<typeinfo>intMainvoid){  intI3); printf ("%s\n", typeID (i). Name ()); return 0;}// -#include <stdio.h>#include<typeinfo.h>Template<classT1,classT2>classa{T1 i;  T2 J;  Public: A (T1 t1, T2 T2) {i=T1; J=T2; }    BOOLComp () {returni >J; }    voidPrint_type ();}; Template<classT1,classT2>voidA&LT;T1, t2>::p rint_type () {printf ("the type of I is%s\n", typeID (i). Name ()); printf ("the type of J is%s\n", typeID (j). Name ());}intMainvoid) {A<int,Double> A (3,67.8); if(A.comp ()) printf ("i>j \ n"); Elseprintf ("i<=j \ n");  A.print_type (); return 0;}//2.3 Template Fully Special------------------------------------------------------------------------------------------------- ---// -#include <stdio.h>Template<classT>voidfunc (T a) {printf ("hello\n");}//function templates are completely specialTemplate <>voidfunc<int> (inta) {printf ("Hello there\n");}intMainvoid) {func (2);//Print Hello thereFunc'y');//Print Hello  return 0;}// in#include <stdio.h>//class template ATemplate <classT>classa{T i;  Public: A (T t) {i=T; } T Compute () {returnIi; }};//full specificity of class template aTemplate <>classa<int>{    inti; intK//Add new data member   Public: A (intt) {i=T; printf ("hello\n"); }  //Add Print    intCompute () {returnI *i *i; } //change to cubic calculation    voidf () {}//To add a new member function};intMainvoid) {A<Double> Dobj (2.5); A<int> iObj (5);//Print Helloprintf"%f\n", Dobj.compute ());//Square calculation 6.25printf"%d\n", Iobj.compute ());//Cubic Compute  return 0;}//2.4 function Template overloading------------------------------------------------------------------------------------------------ ----// -#include <stdio.h>Template<classT>voidfunc (T a) {printf ("Use the func (T a) template \ n");}//Overloading of function templatesTemplate <classT1,classT2>intfunc (T1 t1, T2 T2) {printf ("Use the func (T1 T1, T2 t2) template \ n"); return 1;}intMainvoid) {func ( -, -);//using overloaded function TemplatesFunc +); return 0;}//2.5 class template Inheritance-------------------------------------------------------------------------------------------------- --//30-31#include <stdio.h>Template<classT>classa{ Public:    voidfunc (T a) {printf ("Use the func (T a) member function template \ n"); }};template<classT1,classT2>classB: PublicA<t1>{   Public:    voidfunc (T1 t1, T2 T2) {printf ("Use the func (T1 T1, T2 T2) member function template \ n"); }};intMainvoid) {B<int,Double>b; B.func ( -, -); A<int> A = static_cast < a <int> >(b); A.func (Ten); return 0;}//2.6 Summary of this chapter--------------------------------------------------------------------------------------------------- -

TOP

The 2nd Chapter C + + template technology

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.