cpp积累

  @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;

}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值