C分析和总结分析和总结_第1页
C分析和总结分析和总结_第2页
C分析和总结分析和总结_第3页
C分析和总结分析和总结_第4页
C分析和总结分析和总结_第5页
已阅读5页,还剩22页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

HebeibadinqChina

1const与volatile的用法

1const

#include<conio.h>

#include<iostream.h>

〃行参数指向const类型变量的指针

voiddisplay_c(consint*pi)

{

cout<<"display_c:"<<*pi<<endl;

)

〃行参为普通类型变量的指针

voiddisplay(int*pi)

{

cout<<"display_c:"<<*pi<<endl;

)

//const类型变量

constintil=l;

//erroril=3;

//const类型变量的指针

inti2=2;

constint木pi2=&i2;

//error*pi2=3

//const指针

int*constpi3=&i3;

*pi3=5;

//error*pi3=&i4可以赋予不同的值,但是不可一指向不同的变量

〃指向const内容的const指针

constint*cosntpi5=&i5;

2sizeof返回某个便赖宁嘎或数据类型的长度

Loveforever

HebeibadinqChina

3引用

1引用变量

inti=l;

int&r_i=i;

5名空间

1namespace

namespacecar

{

intmodel;

intlength;

intwidth;

)

namespaceplane

{

intmodel;

namespacesize

{

intlenth;

intwidth;

)

)

namespacecar〃添加名空间的成员

{

char*name;

}

namespqcec=car;〃定义另!J名

intTime〃外不变量属于全局名空间

Loveforever

HebeibadinqChina

car::length=3;

plane::size::length=70;

intTime=1996;

::Time=1997;

2using

voidmain()

{

usingnamespacecar;

length;

usingnamespacephane;

model;

)

6new与delete运算符

double*pd;//definepointervariable

pd=newdouble;//allocatememory

if(pd!=null)

{

•••

deletepd;//freememory

)

doublel=null

*pds=newdouble[100];

if(pds)

{

••••

delete[]pds;

Loveforever

HebeibadinqChina

)

如果是使用new进行内存空间分配没有成功,则返回空指针null

释放一个数组分配的内存是,常常采用带口的形式

7void指针它指向一个地址值,但是不说名数据类型,可以使用void指针创建一个通

用的函数,在使用

的时候将指针类型转化成相应的具体类型。

voidShowHex(void*pv,intsiae)

{

••••

((unsignedchar*)pv)[i]

)

voidmain()

{

void*pv=null;

unsignedcharcl=0xl2;

pv=&cl;

ShowHex(pv,sizeof(cl));

)

9typeid运算符用来求得变量或队形爰女嘎的数据类型,返回一个type_inf。类型的

对象,通过该对象

可以获取数据类型的名称

include<typeinfo.h>

inti;

consttypejnfo&tO=typeid(i);

tO.name();

10函数

Loveforever

HebeibadinqChina

1模板函数

template<classT>Tmin(R&x,Tv&y)

{

returnx<y?x:y;

)

2指向函数的指针

intmax(intxzinty)

{

)

intmin(intx,inty)

int(*m_pfunction)(int,int);

voidmain()

{

m_pfunction=max;

(*m_pfunction)(2,3);

m_pfunction=min;

(*m_pfunction)(2,3);

)

11类与对象

1构在函数和析构函数

#include<iostream.h>

#include<string.h>

#include<conio.h>

classBook

Loveforever

HebeibadinqChina

private:

char*pBookName;

public:

intPageNum;

public:

Book(char*pBN=NULL);

~Book(void);

voidSetBookName(char*pBN);

intGetBookName(char*pBNzunsignedintMaxSize);

);

Book:Book(char*PBN)

{

coutvv”构造函数”<vendl;

pBookName=null;

if(oBN!=null)

{

pBookName=newchar[strlen(pBN)+l];

if(pBookName!=null)

strcyp(pBookNamezpBN);

)

)

Book::~Book()

{

delete[]pBookName;

)

voidBook::SetBookName(char*pBN)

Loveforever

HebeibadinqChina

if(pBookName!=null)

delete[]pBookName;

pBookName=newchar[strlen(pBN)+l];

if(pBookName!=null)

strcyp(pBookName,pBN);

)

intBook::GetBookName(char*pBN,unsignedintmaxSize)

{

if((pBookName!=null))&&(MaxSize>strlen(pBookName))

{

strcpy(pBN,pBookName);

retrunstrlen(strlen(pBookName));

)

)

〃使用

Bookbl;

bl.SetBookName("test");

Bookb2(testl);

2对象的引用参数传递

voidAdd(Bookb)

voidAddRef(Book&b);

3静态成员变量是一个公共变量

在初始化的时候利用作用运算符::对私有类型的静态成员变量可以向公有类型的静态

成变量一样赋值

但不能直接引用

3const类型成员函数与mutable

Loveforever

HebeibadinqChina

classCDate

{

public:

intyear;

mutableintmonth;

CDate(inty=2000,intm=l)

{

year=y;

month=m;

);

intBetMonth()const;//readonly

voidSetMonth(intm);//writeonly

)

voidCDate::SetMonth(intm)

{

month=m;

)

voidmain()

{

CDatedl;

)

在const类型的成员函数定义中,不可一直接或简介的修改普通类型的成员变量

如果象修改const类型对象的成员函数,可以使用关键字mutable对该成员变量进行

修改

5对象的初始化与初始化行

将参数类表中的数值赋予成员变量时,不仅可以在构造函数的函数体中进行,以阿宽

衣在初始化行中进行

Loveforever

HebeibadinqChina

在初始化处惊醒初始化的情况有:

1分层类的构在函数可以调用它的件可一个子对象的构造函数

2对常量const类型的成员变量的初始化必须在初始化行上

3对于引用类型的成员变量的初始化必须在初始化行上

classCPoint

{

public:

intx,y;

CPoint(inLax=0,intay=0)

{

x=ax;

y=ay;

)

);

classCRect

{

private:

CPointlow_right;

CPointupjeft;

public:

int&CenterX;

constintCenterY;

CRect(intxl,intylzintx2,inty2zint&x3,inty3)

:up_left(xl,yl)zlow_right(x2zy2),CenterX(x3)/CenterY(y3)

{

)

Loveforever

HebeibadinqChina

);

voidmain()

{

intcx=5;

intcy=6;

CRectrl(l,2,3,4,cx,cy);

)

6拷贝构造函数

拷贝构造函数与普通构造函数的差别在与棋参数类表,参数列表中有一个对象,该对

象的数据类型是

本类的一个引用,而且一般情况下,该对象还应该是一个const类型的对象。

如果在类的定义是不是显示的定义一个拷贝函数,则编译器会自动的定义一个拷贝构

造函数

classCPoint

{

public:

intx,y;

CPoint(intm_x=0,ubtm_y=0);//defaultconstructor

cPoint(constCPoint&c);//copyconsrucotr

};

CPoint::CPoint(intm_xzintm_y)

{

)

CPoint::CPoint(constCPoint&c)

{

x=c.y;

Loveforever

HebeibadinqChina

y=c.x;

)

voidmain()

{

CPointcl(lz2);//invokedefaultconstructor

CPointc2-cl;//invokecopyconstructor

)

7templateclass

template<classtzintSize>classArray//templateclass

{

private:

Tarr[Size];

intCurSize;

public:

Array(T*datejntn)

{

CurSize=n<Size?n;Size;

for(inti=O;i<CurSize;i++)

{

Arr[i]=data[i];

)

)

)

voidmain()

{

intil[10]={l/2,3z4,5,6z7,8z9};

Array<intz6>array_il(ilzi0);

)

Loveforever

HebeibadinqChina

1友员类和友员函数

#include<iostream.h>

#include<string.h>

#include<conio.h>

classSoftWareEngineer;//先又寸SoftwareEngineer类进彳亍显示说明一下

classComputer//定义Computer类

{

private:

intPrice;

public:

Computer(intp){Price=p};

friendclassSoftwareEngineer;〃将友员类载这里声明

frinedvoidJudge(Computer&c,SoftwareEngineer&s)/友员函数

);

classSoftwareEngineer

{

intSalary;

charName[20];

public:

SoftwareEngineer(intszchar*n/勾造函数)

{

Salary=s;

strcpy(Namezn);

Loveforever

HebeibadinqChina

)

intGetComputerPrice(Computer&c){returnc.Price}/访问Computer的思友变量

friendvoidJudge(Computer&czSoftwareEngineer&s)/友员函数

);

〃判断一个软件工程师是否可以用一个月的薪水买的器一台电脑

voidJudge(Computer&c,SoftwareEngineer&s)/桥友员函数

{

if(c.Price>s.Salary)

coutv<”软件工程师”<vs.Ndmev<"的一个月薪水买不起一台电脑”<<endl;

else

coutvv嗽件工程师“vvs.Namevv”的一月薪水买的起一台电脑”<<endl;

)

voidmain()

{

Computercl(100);

SoftwareEngineersl(120,"userl");

Judge(cl,sl);

SiftwareEngineers2(80z"user2")

Judge(cl,s2);

getch();

)

2运算符重载

#include<iostream.h>

#include<conio.h>

#include<iomanip.h>

classTValue{

Loveforever

HebeibadinqChina

private:

intvalue;

public:

TValue(inti){value=i}

〃成员函数重载运算符

TValueoperator!();

TValueoperator+(TValue&rv);

//友员函数重载运算符

friendostream&operator<<{ostream&oszTValue&rv);

)

TValueTvalue::operator!()

{

returnTValue(!value);

)

TValueTValue:operator+(TValue&rv)

{

returnTValuefvalue+rv.value);

)

ostream&operator<<(ostream&os,TValuerv)

{

os<<setw(5)<<rv.value;

returnos;

)

voidmain()

Loveforever

HebeibadinqChina

TValuevl(3),v2(5);

cout<<

)

3类的派生和继承

lclass

Box{publi

c:

intwidth,height;

voidSetWidth(intw){width=w};

voidSetWidth(inth){height=h;};

);

classTColorBox::public

Box{public:

intcolor;

voidSetColor(intc){color=c;};

);

void

main(){TColor

Boxcb;

cb.SetColo「(255);〃声明非继承类的对象

cb.SetWidth(100);〃声明继承类的对象

cb.SetHeight(lOO);/庐明继承类的对象

)

2不能被继承的成员

构造函数,析构函数,用户定义的新操作符,用户定义的赋值操作,友员关系

3构造函数,析构函数的调用顺序

class

A{int

Loveforever

HebeibadinqChina

a,b,c;

Loveforever

HebeibadinqChina

public:

A(intx,inty,intz)

{

a=x;

b=y;

c=z;

)

);

class

B{intd;

public:

B(intxx):A(xx,xx+l,xx+2)(d=xx);〃内联构造函数

B(intx,inty,intz,intxx);〃非内联构造函数

B:B(intx,inty,intz,intxx):A(x,y,z)

{

d=xx;

)

)

实例

classCurrency

{

poublic:

doublepar_value;

Currency(){per_value=O.O;}

Currency(doubled){per_value=d;}

Currency(Currency&rc){par_value=rc.par_value;}

Loveforever

HebeibadinqChina

Currency&operator={Currency&c}

{

par_valuc=c.par_value;

return*this;

)

Currency&operator+(Currency&c)

{

par_value+=c.par_value;

return*this;

)

)

〃人民币

classRMB:publicCurrency

{

public:

RMB():Currency(){};〃调用派生类的构造函数前,需要调用器基类的工造函数

RMB(doubled):Currency(d){};

RMB(Currency&c):Currency(c){};

RMB(RMB&rmb):Currency(rnb){};

friendostream&operator<<{ostream&oszRMB&rnb);/派生类的附加功能

);

ostream&operator<<{ostream&os,RMB&rmb)〃output运算符不能是一个类的成

员函数

{

os<<"¥"<<setiosflags(ios::shwopoint|ios:fixed)<<setprecision(2)rmb.par_value;

returnos;

Loveforever

HebeibadinqChina

voidmain()

{

RMBr_income(5000);

RMBr_balance;

r_balance=rjncome=r_expense;

cout<<"income"<<rincome<<endl;

)

4将iostream运算符重载

l)ostream&operator<<(ostream&os,constTriangular&ths)

{

n

os<<("<<rhs.beg_pos()<<"z"<<rhs.length()<<")";

rhs.display(rhs.length(),rhs.beg_pos()zos);

)

ouutput运算符不能够设计成memberfunction

2)istream&operator>>(istream&is,Triangular&rhs)

{

charchlzch2;

intbpjen;

〃输入chl=,(*,bp=3,ch3=7len=6

is>>chl>>bp>>ch2>>len;

rhs.beg_pos(bp);

rhs.length(len);

rhs.next_reset();

returnis;

)

Triangulartris;

cin>>tri2

4虚基类

Loveforever

HebeibadinqChina

载继承关系中,同一基类被继承多次,不仅会引器歧异,而起可能浪费空间

classA

{

public:

intvalue;

);

classB:publicvirtualA();//虚基类编译器只产生一个基类版本。如果不定义为virtual

编译器不知到调用那个value了,当然

classC:publicvirtualA();//也可以returnB::value;

classD:publicb.publicc

{

public

intGetValue(){returnvalue;}

);

voidmain()

{

Ddd;

dd.GetValue();

)

5多态形,和虚函数

classTFirst

{

publicvirtualvoidDisplay();

);

voidTFirst::Display()

Loveforever

HebeibadinqChina

cout<<"first"

)

classTSecond:publicTFirst

{

public:

virtualvoidDisplay();

);

voidTSecond::Display()

{

cout<<"second"

)

voidDisplay(TRist*pFirst)

{

pFisrt=>Display();

)

voidmain()

{

TFirst*pFirst=newTFirst;

TSecond*pSecond=newTSecond;

pFirst->Display();

pSecond->Display();

Display(pFirst);

Display(pSecond);

deletpfirst;

deletpSecond;

Loveforever

HebeibadinqChina

getch();

)

C++builder中的集合的

1集合的概念基本

Set<type/minval,maxval>

Set<char/A'/C'>si

tydefefSet(cha「,1,255)UPPERCASet;

UPPERCASESetsl;

sl<<,A'<<,B'<<'C;

sysset.h直接包含载vcl.h中

2集合的操作

#include<iostream>

#include<system.hpp>

#include<conio.h>

usingnamespacestd;

typedefSet<char/B7Z'>UpperSet;

typedefSet<char/a7z'>LoverSet;

typederSet<char/a'/j'>HalfLowerSet;

voidset_example()

{

LowerSetae,ae2,aczde;

UpperSetAE,AE2,AC,DE;

HalfLowerSetaj;

)

Loveforever

HebeibadinqChina

异常处理

1C++的异常处理

#include<iostream.h>

#include<conio.h>

classCrange

{

public:

intindex;

CRange(inti){index=i;}

)

classCString

{

chara[100];

intlen;

public:

CString(char*s)

{

Len=strlen(s);

strcpy(a,s);

)

char&operator[](inti)

{

if(i>0&&i<len)returna[i];

elsethrowCRange(i);

)

voidDisplayNoCatch(CString&s)

Loveforever

HebeibadinqChina

intj;

forQ=0lj<20;j++)

cout<<s[j]<<""endl;;

)

intDisplayHasCatch(CString&s)

{

try{

DisplayNoCatch(s);

}catch(CRanger)

{

cerr<<r,index<<endl;

)

return99;

)

)

2多路捕捉

#include<iostre

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论