C++语法总结(2)(2)
Triangular tris;
cin>>tri2
4 虚基类
载继承关系中,同一基类被继承多次,不仅会引器歧异,而起可能浪费空间
class A
{
public:
int value;
};
class B:public virtual A(); //虚基类 编译器只产生一个基类版本。如果不定义为virtual 编译器不知到调用那个value 了,当然
class C:public virtual A();// 也可以return B::value;
class D:public b.public c
{
public
int GetValue(){return value;}
};
void main()
{
D dd;
dd.GetValue();
}
5 多态形,和虚函数
class TFirst
{
public virtual void Display();
};
void TFirst::Display()
{
cout<<"first "
}
class TSecond:public TFirst
{
public:
virtual void Display();
};
void TSecond::Display()
{
cout<<"second"
}
void Display(TRist * pFirst)
{
pFisrt=>Display();
}
void main()
{
TFirst * pFirst=new TFirst;
TSecond * pSecond=new TSecond;
pFirst->Display();
pSecond->Display();
Display(pFirst);
Display(pSecond);
delet pfirst ;
delet pSecond;
getch();
}
c++ builder 中的集合的
1 集合的概念基本
Set<type,minval,maxval>
Set<char,'A','C'> s1
tydefef Set(char,1,255) UPPERCASet;
UPPERCASESet s1;
s1<<'A'<<'B'<<'C';
sysset.h 直接包含载vcl.h 中
2 集合的操作
#include<iostream>
#include<system.hpp>
#include<conio.h>
using namespace std;
typedef Set<char,'B','Z'> UpperSet;
typedef Set<char,'a','z'> LoverSet;
typeder Set<char,'a','j'> HalfLowerSet;
void set_example()
{
LowerSet ae,ae2,ac,de;
UpperSet AE,AE2,AC,DE;
HalfLowerSet aj;
}
异常处理
1 c++ 的异常处理
#include <iostream.h>
#include <conio.h>
class Crange
{
public:
int index;
CRange(int i){index=i;}
}
class CString
{
char a[100];
int len;
public:
CString(char * s)
{
Len=strlen(s);
strcpy(a,s);
}
char & operator[](int i)
{
if(i>0 && i<len) return a[i];
else throw CRange(i);
}
void DisplayNoCatch(CString & s)
{
int j;
for(j=0lj<20;j++)
cout<<s[j]<<""endl;;
}
int DisplayHasCatch(CString & s)
{
try{
DisplayNoCatch(s);
}catch(CRange r)
{
cerr<<r.index<<endl;
}
return 99;
}
}
2 多路捕捉
#include<iostream.h>
#include<conio.h>
void MultiException()
{
int i;
double d;
int no;
cout<<"(>0 and <5)";
cin>>no;
tyr
{
switch(no)
{
case 1;
throw 123;
break;
case 2:
throw i;
break;
case 3:
throw d;
break;
case 4:
throw "Error";
break;
default:
cout<<"error";
}
}
catch(int ie)
{
cout<<"int"<<endl;
}
catch(double de)
{
cout<<"double"<<endl;
}
catch(char* se)
{
cout<<"char"<<endl;
}
}
3 bcb中的异常类
1)却省vcl 异常处理 无try catch 自动处理
int i=4,j=0;k;
k=i/k;
//下一句永远不会执行
ShowMessage(k);
2) 引发vcl 异常类
try{
}
catch(Exception &e)
{
ShowMessage(e.,Message);
}
不能引发如int double 等简单类型的异常
3) c++ 异常处理
try
{
throw 2222;
}
catch(int ErrorCode)
{
ShowMessage(ErrorCode);
}
}
4 单一捕捉异常
try{
k=10/0;
}catch(EDivByZero & E)
{
MessageDlg()
}
5 捕捉所有异常
try
{
}catch(...) or(EExternalException &E)
- 最新评论
