锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
#include <iostream>
using namespace std;
class A
{
public:
virtual void fnA()
{cout<<"A."<<endl;}
};

class B
{
public:
virtual void fnB()
{cout<<"B."<<endl;}
};

class C:public A,public B
{
public:
void fnA()
{cout<<"fnA->C."<<endl;A::fnA();}
void fnB()
{cout<<"fnB->C."<<endl;B::fnB();}
};

int main()
{
C *pC=new C();
cout<<pC<<endl;
pC->fnA();
pC->fnB();
B *pB=pC;
cout<<pB<<endl;
pB->fnB();
A *pA=pC;
pA->fnA();
cout<<pA<<endl;
delete pC;
return 0;
}
2.鍩虹被鐨勬瀽鏋勫嚱鏁?br>
#include <iostream>
using namespace std;

class A
{
public:
A()
{cout<<"A()."<<endl;}
~A()
{cout<<"~A()."<<endl;}
};

class B:public A
{
public:
B()
{cout<<"B()."<<endl;}
~B()
{cout<<"~B()."<<endl;}
};

int main()
{
A *p=new B(); //鍩虹被娌℃湁灝嗘瀽鏋勫嚱鏁板0鏄庝負铏氭嫙,鍒欐渶鍚庡茍涓嶄細璋冪敤瀛愮被鐨勬瀽鏋勫嚱鏁?/span>
delete p;
return 0;
}
鏆傛椂鍙疄鐜頒簡榪欎釜綆鍗曠殑Demo,絳夋嬁鍒般婃繁搴︽帰绱++瀵硅薄妯″瀷銆嬪啀浠旂粏寮勪笅 ..~
涓涓緢綆鍗曠殑鐮?..~ 榪欎釜鐮佹槸鍦╒C6.0涓繍琛岀殑 鎵浠ヤ笉絎﹀悎鐜板湪鐨勬爣鍑?br>
#include <iostream.h>
class Point

{
public:
virtual void output()
{
cout<<"璋冪敤浜嗗熀綾葷殑output()鍑芥暟.";
}
void cjp()
{
this->output();
}
};
class test:public Point

{
public:
void output ()
{
cout<<"璋冪敤浜嗘淳鐢熺被鐨刼utput鍑芥暟銆?/span>";
}
};
int main()

{
test tt;
tt.cjp();
return 0;
}//endof main()
濡傛灉榪欓噷鍩虹被鐨刼utput涓嶆槸virtual function 榪愯鏃惰繖涓猼his 瀵硅薄灝辨槸鍩虹被鐨勬寚閽?br>... 鍔犱簡灝辨槸榪愯媧劇敓綾葷殑output
]]>
1. ZeroOnePack
棰樼洰
鏈?/span>N浠剁墿鍝佸拰涓涓閲忎負V鐨勮儗鍖呫傜i浠剁墿鍝佺殑璐圭敤鏄?/span>c[i]錛屼環鍊兼槸w[i]銆傛眰瑙e皢鍝簺鐗╁搧瑁呭叆鑳屽寘鍙嬌浠峰兼誨拰鏈澶с?/span>
鍏抽敭鐮侊細
for(i=0;i<nPack;++i)
for(j=MaxVolume;j>=v[i];--j)
Dp[j]>?=Dp[j-v[i]]+w[i];
cout<<Dp[MaxVolume]<<endl;
// 鏈鍚嶥p[MaxVolume]涓烘渶澶т環鍊?#8230;
2. CompletePack
棰樼洰
鏈?/span>N縐嶇墿鍝佸拰涓涓閲忎負V鐨勮儗鍖咃紝姣忕鐗╁搧閮芥湁鏃犻檺浠跺彲鐢ㄣ傜i縐嶇墿鍝佺殑璐圭敤鏄?/span>c[i]錛屼環鍊兼槸w[i]銆傛眰瑙e皢鍝簺鐗╁搧瑁呭叆鑳屽寘鍙嬌榪欎簺鐗╁搧鐨勮垂鐢ㄦ誨拰涓嶈秴榪囪儗鍖呭閲忥紝涓斾環鍊兼誨拰鏈澶с?/span>
鍏抽敭鐮侊細
for(i=0;i<nPack;++i)
for(j=v[i];j<=MaxVolume;++j)
Dp[j]>?=Dp[j-v[i]]+w[i];
cout<<Dp[MaxVolume]<<endl;
//鏈鍚嶥p[MaxVolume]涓烘渶澶т環鍊?#8230;

void fnHanoi(int n, char a, char b, char c)
{
if(n==0) return;
fnHanoi(n-1,a,c,b);
std::cout<<"絎?/span>"<<n<<"涓洏瀛愪粠"<<a<<" 縐誨姩 "<<c<<std::endl;
fnHanoi(n-1,b,a,c);
}
#include <iostream>
using namespace std;

void fnHanoi(int m,int n)
{
int c,t,y;
for(t=n,y=t&1,c=m;y-1;t=t>>1,y=t&1,--c);//c琛ㄧず灞傚彿
switch((c&1)*3+(t>>1)%3)//c&1濂囧伓鍒ゆ柇,t>1涓哄眰涓簭鍙?/span>
{
case 0:cout<<n<<"*:A - B"<<endl;break;
case 1:cout<<n<<"*:B - C"<<endl;break;
case 2:cout<<n<<"*:C - A"<<endl;break;
case 3:cout<<n<<"*:A - C"<<endl;break;
case 4:cout<<n<<"*:C - B"<<endl;break;
case 5:cout<<n<<"*:B - A"<<endl;break;
}
}

int main()
{
int nMax=1,nDish,nMove=1;
cin>>nDish;
for(nMax<<=nDish;nMove<nMax;++nMove)
fnHanoi(nDish,nMove);
return 0;
}
#include <iostream>
#include <utility>
#include <queue>
#include <stack>
using namespace std;
const int SPACE=0;
const int WALL=1;
bool bFinish;
int Path[4][2]=
{
{-1,0},
{0,-1},
{0,1},
{1,0}};
int M[5][5]=
{
{0,1,1,1,0},
{0,0,0,0,0},
{0,1,0,0,1},
{0,1,0,0,0},
{0,1,1,1,0}};

bool fnIsInSide(int a,int b)
{
return (a>=0&&a<=4&&b>=0&&b<=4);
}

int fnBfs()
{
queue<pair<int,int> >MyQ;
int nX,nY,nCnt=1;
pair<int,int>MyP_A,MyP_B;
MyP_A.first=0;
MyP_A.second=0;
MyQ.push(MyP_A);
while(!MyQ.empty())
{
MyP_A=MyQ.front();
++nCnt;
if(MyP_A.first==4&&MyP_A.second==4)
return ++bFinish;
MyQ.pop();
M[MyP_A.first][MyP_A.second]=WALL;
for(int i=0;i<4;++i)
{
nX=MyP_A.first+Path[i][0];
nY=MyP_A.second+Path[i][1];
if(fnIsInSide(nX,nY)&&M[nX][nY]!=WALL)
{
MyP_B.first=nX;
MyP_B.second=nY;
MyQ.push(MyP_B);
}
}
}
return bFinish;
}

void fnDfs(int x,int y)
{
int nX,nY;
if(x==4&&y==4) ++bFinish;
if(bFinish) return;
for(int i=0;i<4;++i)
{
nX=x+Path[i][0];
nY=y+Path[i][1];
if(fnIsInSide(nX,nY)&&M[nX][nY]!=WALL)
{
M[nX][nY]=WALL;
fnDfs(nX,nY);
M[nX][nY]=SPACE;
}
}
}

int main()
{
int hWay;
cout<<"If you want Breadth First Search ,input(0),else input(1) -- Depth First Search."<<endl;
cin>>hWay;
hWay?fnDfs(0,0):(void)fnBfs();
bFinish?cout<<"The path is found."<<endl:cout<<"The path isn't found"<<endl;
return 0;
}

/**//*
cin>>hWay;
hWay?fnDfs(0,0):fnBfs();
2涓嚱鏁?br>
void fnDfs(0,0);
int fnBfs();
涓嶆噦涓轟粈涔圙++ 緙栬瘧鍣ㄦ彁紺?br>
error: `fnDfs(0, 0)' has type `void' and is not a throw-expression
鍘熸潵榪欓噷蹇呴』
A?B:C
鏉′歡鎿嶄綔絎鍜孋瑕佹湁鐩稿悓鐨勭被鍨嬫垨鑰呭彲浠ョ浉浜掕漿鍖?br>
浣犵殑涓涓獀oid錛屼竴涓猧nt鑲畾涓嶈
The rule is: if one the operand is of type void, then the other
| > operand must be of type void or a throw expression -- but both cannot
| > be a throw-expression.
*/
#include <iostream>
using namespace std;
const int MAXN=10000;
const int M=9001;
int Hash[MAXN];

class AddressList
{
public:
AddressList()
{ memset(Hash,-1,sizeof(Hash));}
~AddressList()
{}
unsigned ELFHash(const char *str)
{
unsigned Key=0,x=0;
while(*str)
{
Key=(Key<<4 )+(*str++); //Key鍊煎乏縐?浣嶅姞涓婁竴涓瓧絎?/span>
if((x=Key&0xF0000000L)!=0)//鍒ゆ柇Key鍊肩殑楂?浣嶆槸鍚︿笉涓?錛屽洜涓轟笉涓?鏃墮渶瑕佷笅闈㈢壒孌婂鐞嗭紝鍚﹀垯涓婇潰涓姝ョ殑宸︾Щ4浣嶄細鎶婅繖楂樺洓浣嶇粰縐昏蛋錛岄犳垚淇℃伅涓㈠け
{
Key^=(x>>24); //鎶婂垰鎵嶇殑楂?浣嶈窡Key鐨勪綆5-8浣嶅紓鎴?/span>
Key&=~x; //鎶婇珮4浣嶆竻0
}
}
return (Key & 0x7FFFFFFF); //鍙ey鍊兼槸涓涓潪璐熸暟
}
void Search(const char *str)
{
int Key=ELFHash(str);
int t=Key%M;
while(Hash[t]!=Key&&Hash[t]!=-1)
t=(t+5)%M;
if(Hash[t]==-1)
Hash[t]=Key;
else
cout<<str<<"'s AddressList is recorded."<<endl;
}
};

int main()
{
AddressList AL;
char str[MAXN];
int nCase;
cout<<"You want input (n?) AddressList."<<endl;
freopen("in.cpp","r",stdin);
while(scanf("%d",&nCase)!=EOF)
for(gets(str);nCase>0;nCase--)
{
gets(str);
AL.Search(str);
}
return 0;
}