- 博客(18)
- 收藏
- 关注
c++ 类的虚拟继承
有一个基类:class Animal {public:Animal();void setWeight(float weight);float getWeight();private:float weight;}有两个Animal类的派生类:class ma:public Animal{public:ma();void run();...
2010-11-10 21:15:16
113
c++ 虚拟new操作符(创建抽象基类对象的拷贝)
基类定义如下:class Query{public:virtual Query *clone() = 0;};派生类定义如下:class NameQuery : public Query{public:virtual Query *clone(){return new NameQuery(*this);}};Query *pq = new...
2010-11-10 15:14:42
367
c++ 类的虚拟函数和缺省实参
#include <iostream>class Base{public:virtual int foo( int ival = 1024 ){cout<<"Base::foo()----- ival: "<<ival<<endl;return ival;}};class Derived : public ...
2010-11-10 14:58:32
109
c++ 类的虚拟机制
以下类的定义只是为了说明虚拟机制而设计:class Animal{public:int getNum();}class Bird : public Animal{public:int getNum();}Animal *pa = new Bird();1、pa->getNum();此时调用的是Animal类的getNum()修改...
2010-11-10 11:43:00
294
c++ 基类和派生类函数构成重载
class Diff{public :void numble( int doftness );}class Shy : public Diff{public:void numble( string str);using Diff::numble;}要想使基类和派生类中的这两个函数形成重载:需要在派生类中加一句话using Diff::numble; 这样...
2010-11-10 11:21:28
442
c++ 多态性
c++支持多态性的几种方式:1、通过一个隐式转化,从“派生类指针或引用”转化到“其公有基类类型的指针或引用”Query *pquery = new NameQuery( "Glass" );2、通过虚拟函数机制3、通过dynamic_cast 和 typeid 操作符(RTTI)1)首先这两个操作符是在“运行时刻类型识别”!2)当一个类库是由第三方库提供商提供时,此时我们不...
2010-11-10 11:01:49
82
c++ 指向类成员的指针
定义类如下:class Foo{public:static int getNum();private:static int num;}1、定义一个指针指向 类的数据成员 numint Foo::*p = &Foo::num2、定义一个指针指向 类的成员函数 getNum()int (Foo::*pf)() = &Foo::getNum;...
2010-11-10 09:42:43
115
原创 c++ 类的静态成员
1、静态数据成员的类型可以是其所属类,而非静态数据成员只能被声明为该类的对象的指针。例如:class Bar{private:static Bar mem;//正确Bar mem1;//错误Bar *mem2;//正确}2、静态数据成员可以被作为类成员函数的缺省实参,而非静态成员不能。例如:class Foo{private:static int s...
2010-11-10 09:16:28
66
类总结 集锦
class Screen{public:void home(){_cursor = 0;}private:int _cursor;}虽然每个类对象都有自己的类数据成员拷贝。但是每个类成员函数的拷贝“只有一份”!例如:Screen myScreen, groupScreen;myScreen.home();groupScreen.home();其中my...
2010-11-09 21:54:33
97
原创 类的 访问控制(private,public,protected)
Animal为基类,Bird为派生类Bird继承Animal当继承方式为public时父类的public成员在子类也是public父类的protected成员在子类也是protected父类的private成员只属于父类当继承方式为protected时父类的public成员在子类是protected成员父类的protected成员在子类也是protected...
2010-11-09 21:41:43
220
原创 c++ 类的数据成员初始化
class First{public: int num = 0;//错误 const int num1 = 0;//错误 static int num2 = 0;//错误 static const int num3 = 0;//正确,只有静态常量整型数据成员才可以在类中初始化 string str = "0";//错误 const string str1 = "0";/...
2010-11-09 17:26:56
107
c++ 异常处理
#include "stdafx.h"#include <iostream>using namespace std;enum EHstate{ noErr, zeroOP,nega, severeError};enum EHstate state = noErr;int mathFunc(int i){ if(i == 0) { throw s...
2010-10-23 10:09:44
93
c++ 强制类型转化
dynamic_cast:通常在父类与子类之间转化时使用,转化的类型和对象都是类对象,不能应用于c++内置类型static_cast:一般的类型转换,但不能将一个const对象转型为non-const对象(只有const_cast能做到)。const_cast:将一个const对象转型为non-const对象;reinterpret_cast:通常对于操作数的位模式执行一个比较低层次的重新解释。...
2010-10-16 10:19:12
81
基础总结 锦集
char cal1[] = {'C','+','+'};//维数是3 char cal2[] = "c++";//维数是4 //char cal3[] = cal1;//不能用一个数组初始化另一个数组 //char &cal4[] = {'C','+','+'};//c++不允许数组引用 string *ps = new string("Hello World!...
2010-10-16 09:46:02
68
原创 bitset 操作
#include "stdafx.h"#include <iostream>#include <bitset>using namespace std;int _tmain(int argc, _TCHAR* argv[]){ bitset<32> bitvec(8); bool flag = bitvec.any();//判...
2010-10-16 09:26:48
106
vector 使用方法
#include "stdafx.h"#include <iostream>#include <vector>using namespace std;int _tmain(int argc, _TCHAR* argv[]){ /****数组用法****/ int iar[6] = {-12,-2,0,8,5,4}; vector&l...
2010-10-16 09:21:40
98
c++ const 指针与引用
#include "stdafx.h"#include <iostream>using namespace std;int _tmain(int argc, _TCHAR* argv[]){ /****const 指针****/ const double pi = 3.14; //double *p = &pi;//错误,const对...
2010-10-15 20:58:33
67
C++ 全局变量与局部变量
/****Lession1.h****/extern bool a;extern int b;extern float c;extern double d;extern unsigned int e;extern long f;extern unsigned long g;extern char h;extern long double i; /**...
2010-10-15 20:44:33
150
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人