考完了c++,上传一点笔记,以便寒假的时候,继续复习! 1. #include<iostream> using namespace std; //静态成员的初始化,虽然有可能成为全局变量,但因为是static,所以仍是安全的 //int x=100; //class Ws //{ // static int x; // static int y; //public : // void print () const // { // cout <<"ws::x "<<x<<endl; // cout<<"ws::y "<<y<<endl; // } //}; //int Ws::x =1; //int Ws::y =x+1; //int x=100; //int y=x+1; //int main() //{ // Ws s; // s.print (); // return 0; //} 2. //模板定义,其中用到了运算符重载 template <class T> class Array { enum{size =100}; T A [size]; public : T& operator [](int index); }; template<class T> T& Array<T>::operator [](int index) { if(index>=0&&index<size) return A[index]; } int main() { Array<float> fa; fa[0]=1.11111; cout<<fa[0]<<endl; } 3. #include<iostream> using namespace std; //实现向上转化,不需加virtual,加virtual,实现晚捆绑作用=动态捆绑(根据对象类型);与3统一 enum note{middlec,csharp,cflat}; class instrument { public : virtual void play(note) const { cout <<"instrument::play"<<endl; } }; class wind :public instrument { public : void play(note )const { cout <<"wind : paly"<<endl; } }; void tune(instrument & i)//先调用派生类 { i.play (middlec); } int main() { wind flute; tune (flute); } 4. //虚拟函数解决基类、派生类同名函数的调用<-windows编程 #include<iostream.h> class Base { public: virtual void virtualf(void) { cout<<"here is Base/n"; } }; class derived:public Base { public : void virtualf(void) { cout<<"here is Derived/n"; } }; void main() { Base * ptr,Base; derived derivedobject; ptr=&Base; ptr->virtualf (); ptr=&derivedobject; ptr->virtualf() ; }