C++.docx

上传人:b****5 文档编号:6686954 上传时间:2023-01-09 格式:DOCX 页数:23 大小:20.53KB
下载 相关 举报
C++.docx_第1页
第1页 / 共23页
C++.docx_第2页
第2页 / 共23页
C++.docx_第3页
第3页 / 共23页
C++.docx_第4页
第4页 / 共23页
C++.docx_第5页
第5页 / 共23页
点击查看更多>>
下载资源
资源描述

C++.docx

《C++.docx》由会员分享,可在线阅读,更多相关《C++.docx(23页珍藏版)》请在冰豆网上搜索。

C++.docx

C++

1const与volatile的用法

1const

#include

#include

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

voiddisplay_c(consint*pi)

{

cout<<"display_c:

"<<*pi<

}

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

voiddisplay(int*pi)

{

cout<<"display_c:

"<<*pi<

}

//const类型变量

constinti1=1;

//errori1=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返回某个便赖宁嘎或数据类型的长度

3引用

1引用变量

inti=1;

int&r_i=i;

5名空间

1namespace

namespacecar

{

intmodel;

intlength;

intwidth;

}

namespaceplane

{

intmodel;

namespacesize

{

intlenth;

intwidth;

}

}

namespacecar//添加名空间的成员

{

char*name;

}

namespqcec=car;//定义别名

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

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

}

double1=null

*pds=newdouble[100];

if(pds)

{

....

delete[]pds;

}

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

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

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

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

voidShowHex(void*pv,intsiae)

{

....

((unsignedchar*)pv)[i]

}

voidmain()

{

void*pv=null;

unsignedcharc1=0x12;

pv=&c1;

ShowHex(pv,sizeof(c1));

}

9typeid运算符用来求得变量或队形爱女嘎的数据类型,返回一个type_info类型的对象,通过该对象

可以获取数据类型的名称

include

inti;

consttype_info&t0=typeid(i);

t0.name();

10函数

1模板函数

templateTmin(R&x,Tv&y)

{

returnx

x:

y;

}

2指向函数的指针

intmax(intx,inty)

{

...

}

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

#include

#include

classBook

{

private:

char*pBookName;

public:

intPageNum;

public:

Book(char*pBN=NULL);

~Book(void);

voidSetBookName(char*pBN);

intGetBookName(char*pBN,unsignedintMaxSize);

};

Book:

Book(char*PBN)

{

cout<<"构造函数"<

pBookName=null;

if(oBN!

=null)

{

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

if(pBookName!

=null)

strcyp(pBookName,pBN);

}

}

Book:

:

~Book()

{

delete[]pBookName;

}

voidBook:

:

SetBookName(char*pBN)

{

if(pBookName!

=null)

delete[]pBookName;

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

if(pBookName!

=null)

strcyp(pBookName,pBN);

}

intBook:

:

GetBookName(char*pBN,unsignedintmaxSize)

{

if((pBookName!

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

{

strcpy(pBN,pBookName);

retrunstrlen(strlen(pBookName));

}

}

//使用

Bookb1;

b1.SetBookName("test");

Bookb2(test1);

2对象的引用参数传递

voidAdd(Bookb)

voidAddRef(Book&b);

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

在初始化的时候利用作用运算符:

:

对私有类型的静态成员变量可以向公有类型的静态成变量一样赋值

但不能直接引用

3const类型成员函数与mutable

classCDate

{

public:

intyear;

mutableintmonth;

CDate(inty=2000,intm=1)

{

year=y;

month=m;

};

intBetMonth()const;//readonly

voidSetMonth(intm);//writeonly

}

voidCDate:

:

SetMonth(intm)

{

month=m;

}

voidmain()

{

CDated1;

}

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

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

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

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

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

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

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

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

classCPoint

{

public:

intx,y;

CPoint(intax=0,intay=0)

{

x=ax;

y=ay;

}

};

classCRect

{

private:

CPointlow_right;

CPointup_left;

public:

int&CenterX;

constintCenterY;

CRect(intx1,inty1,intx2,inty2,int&x3,inty3)

:

up_left(x1,y1),low_right(x2,y2),CenterX(x3),CenterY(y3)

{

}

};

voidmain()

{

intcx=5;

intcy=6;

CRectr1(1,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_x,intm_y)

{

}

CPoint:

:

CPoint(constCPoint&c)

{

x=c.y;

y=c.x;

}

voidmain()

{

CPointc1(1,2);//invokedefaultconstructor

CPointc2-c1;//invokecopyconstructor

}

7templateclass

templateclassArray//templateclass

{

private:

Tarr[Size];

intCurSize;

public:

Array(T*date,intn)

{

CurSize=n

n;Size;

for(inti=0;i

{

Arr[i]=data[i];

}

}

}

voidmain()

{

inti1[10]={1,2,3,4,5,6,7,8,9};

Arrayarray_i1(i1,i0);

}

1友员类和友员函数

#include

#include

#include

classSoftWareEngineer;//先对SoftwareEngineer类进行显示说明一下

classComputer//定义Computer类

{

private:

intPrice;

public:

Computer(intp){Price=p};

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

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

};

classSoftwareEngineer

{

intSalary;

charName[20];

public:

SoftwareEngineer(ints,char*n//构造函数)

{

Salary=s;

strcpy(Name,n);

}

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

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

};

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

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

{

if(c.Price>s.Salary)

cout<<"软件工程师"<

else

cout<<"软件工程师"<

}

voidmain()

{

Computerc1(100);

SoftwareEngineers1(120,"user1");

Judge(c1,s1);

SiftwareEngineers2(80,"user2")

Judge(c1,s2);

getch();

}

2运算符重载

#include

#include

#include

classTValue{

private:

intvalue;

public:

TValue(inti){value=i}

//成员函数重载运算符

TValueoperator!

();

TValueoperator+(TValue&rv);

//友员函数重载运算符

friendostream&operator<<{ostream&os,TValue&rv};

}

TValueTvalue:

:

operator!

()

{

returnTValue(!

value);

}

TValueTValue:

operator+(TValue&rv)

{

returnTValue(value+rv.value);

}

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

{

os<

returnos;

}

voidmain()

{

TValuev1(3),v2(5);

cout<<

}

3类的派生和继承

1classBox{

public:

intwidth,height;

voidSetWidth(intw){width=w};

voidSetWidth(inth){height=h;};

};

classTColorBox:

:

publicBox{

public:

intcolor;

voidSetColor(intc){color=c;};

};

voidmain(){

TColorBoxcb;

cb.SetColor(255);//声明非继承类的对象

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

cb.SetHeight(100);//声明继承类的对象

}

2不能被继承的成员

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

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

classA{

inta,b,c;

public:

A(intx,inty,intz)

{

a=x;

b=y;

c=z;

}

};

classB{

intd;

public:

B(intxx):

A(xx,xx+1,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=0.0;}

Currency(doubled){per_value=d;}

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

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&os,RMB&rnb};//派生类的附加功能

};

ostream&operator<<{ostream&os,RMB&rmb}//output运算符不能是一个类的成员函数

{

os<<"¥"<

:

shwopoint|ios:

fixed)<

(2)rmb.par_value;

returnos;

}

voidmain()

{

RMBr_income(5000);

RMBr_balance;

r_balance=r_income=r_expense;

cout<<"income"<

}

4将iostream运算符重载

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

{

os<<"("<

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

}

ouutput运算符不能够设计成memberfunction

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

{

charch1,ch2;

intbp,len;

//输入ch1='(',bp=3,ch3=','len=6

is>>ch1>>bp>>ch2>>len;

rhs.beg_pos(bp);

rhs.length(len);

rhs.next_reset();

returnis;

}

Triangulartris;

cin>>tri2

4虚基类

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

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()

{

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;

getch();

}

c++builder中的集合的

1集合的概念基本

Set

Sets1

tydefefSet(char,1,255)UPPERCASet;

UPPERCASESets1;

s1<<'A'<<'B'<<'C';

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

2集合的操作

#include

#include

#include

usingnamespacestd;

typedefSetUpperSet;

typedefSetLoverSet;

typederSetHalfLowerSet;

voidset_example()

{

LowerSetae,ae2,ac,de;

UpperSetAE,AE2,AC,DE;

HalfLowerSetaj;

}

异常处理

1c++的异常处理

#include

#include

classCrange

{

public:

intindex;

CRang

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 医药卫生 > 基础医学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1