cpp积累

来源:互联网 发布:java猎魔人下载 编辑:程序博客网 时间:2024/06/13 00:35

 @font-face{font-family:"Times New Roman";}@font-face{font-family:"宋体";}@font-face{font-family:"Symbol";}@font-face{font-family:"Arial";}@font-face{font-family:"黑体";}@font-face{font-family:"Courier New";}@font-face{font-family:"Wingdings";}@font-face{font-family:"新宋体";}@font-face{font-family:"Courier New CYR";}p.0{margin:0pt;margin-bottom:0.0001pt;layout-grid-mode:char; text-align:justify;font-size:10.5000pt; font-family:'Times New Roman'; }div.Section0{margin-top:72.0000pt;margin-bottom:72.0000pt;margin-left:90.0000pt;margin-right:90.0000pt;size:612.0000pt 792.0000pt;}namespace sun

{

 template<class T1, class T2>

 struct pair

 {

  // member

  T1 first;

  T2 second;

  pair() : first(T1()), second(T2())  // ???T1,T2后需加(),不然会出错,为什么???

  {   }                      // T1,是类类型,而firsrt 初始化需要一个对象

                                    // T1() 创建了一个无名对象

  pair(const T1& a, const T2& b)

             : first(a) , second(b) 

  {  }

 };

}

//////////////////////////////////////////////////////////////////////////////////////////////////

// kind of

@font-face{font-family:"Times New Roman";}@font-face{font-family:"宋体";}@font-face{font-family:"Symbol";}@font-face{font-family:"Arial";}@font-face{font-family:"黑体";}@font-face{font-family:"Courier New";}@font-face{font-family:"Wingdings";}@font-face{font-family:"新宋体";}@font-face{font-family:"Courier New CYR";}p.0{margin:0pt;margin-bottom:0.0001pt;layout-grid-mode:char; text-align:justify;font-size:10.5000pt; font-family:'Times New Roman'; }div.Section0{margin-top:72.0000pt;margin-bottom:72.0000pt;margin-left:90.0000pt;margin-right:90.0000pt;size:612.0000pt 792.0000pt;}

#include <iostream>

#include <vector>

using namespace std;

class Shape

{

public:

 virtual void print()

 { cout << "Shape" << endl; }

};

class Circle : public Shape

{

public:

 void print()

 { cout << "Circle" << endl; }

};

class Triangle : public Shape

{

public:

 void print()

 { cout << "Triangle" << endl; }

};

void kindOfShape( const vector<class Shape * >  & con);

int main()

{      

 vector<class Shape * >  con;

 con.push_back( new Circle );

 con.push_back( new Triangle );

    kindOfShape( con );

    return 0;

}

void kindOfShape( const vector<class Shape * >  & con)

{

     cout << "0 or 1" << endl;

  int i = 0;

  cin >> i;

  con[i]->print();

}

//////////////

0   --------->  Circle

1   ---------->  Triangle

///////////////////////////////////////////

@font-face{font-family:"Times New Roman";}@font-face{font-family:"宋体";}@font-face{font-family:"Symbol";}@font-face{font-family:"Arial";}@font-face{font-family:"黑体";}@font-face{font-family:"Courier New";}@font-face{font-family:"Wingdings";}@font-face{font-family:"新宋体";}@font-face{font-family:"Courier New CYR";}p.0{margin:0pt;margin-bottom:0.0001pt;layout-grid-mode:char; text-align:justify;font-size:10.5000pt; font-family:'Times New Roman'; }div.Section0{margin-top:72.0000pt;margin-bottom:72.0000pt;margin-left:90.0000pt;margin-right:90.0000pt;size:612.0000pt 792.0000pt;}

在类中定义const常量 

#include <iostream>

#include <vector>

using namespace std;

class Shape

{

 enum{MAX = 20};

public:

 virtual void print()

 { cout << "Shape" << endl; }

private:

 //const int i;

 const static int j;

 int arr[MAX];

};

//const int Shape::i = 9; // “const int Shape::i”: 不允许成员函数重新声明

//int Shape::j = 99; //  重定义;不同的类型修饰符

const int Shape::j = 99;

int main()

{      

    return 0;

}

原创粉丝点击