复习笔记

本文档包含C++复习笔记,涉及静态成员初始化、模板定义及运算符重载、向上转型及虚拟函数的应用等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

考完了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() ; }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值