C++程序设计资料Word文档格式.docx
- 文档编号:17721001
- 上传时间:2022-12-08
- 格式:DOCX
- 页数:24
- 大小:23.68KB
C++程序设计资料Word文档格式.docx
《C++程序设计资料Word文档格式.docx》由会员分享,可在线阅读,更多相关《C++程序设计资料Word文档格式.docx(24页珍藏版)》请在冰豆网上搜索。
14)每一个文件都有两个指针,一个是读指针,说明输入操作在文件中当前位置;
另一个是写指针,说明写操作在文件中的当前位置。
每次执行输入或输出操作时,相应的读写指针将自动后移。
15)一个类有多个直接基类的继承关系称为多继承
16)多个基类的派生类构造函数用初始化列表调用基类构造函数,执行顺序与单继承构造函数情况类似。
17)多个直接基类构造函数执行顺序取决于定义派生类时指定的各个继承基类的顺序。
按基类在被继承时所声明的次序从左到右依次调用。
18)当派生类从多个基类派生,而这些基类又从同一个基类派生,则在访问此共同基类中的成员时,将产生二义性——采用虚基类来解决。
19)虚基类声明:
以virtual修饰说明基类
例:
classB1:
virtualpublicB
20)虚基类对象是由最直接派生类的构造函数通过调用虚基类的构造函数进行初始化的。
21)异常处理可以增加系统的鲁棒性
22)关键字try:
出错时产生异常的代码放在try块中
23)关键字throw:
throw语句可以抛出任意类型的异常,包括自定义类型
24)关键字catch:
catch块(异常处理器)捕捉和处理异常
25)catch(…)能匹配成功所有的数据类型的异常对象,包括C++语言提供所有的原生数据类型的异常对象
26)可以在函数的声明中列出这个函数可能抛掷的所有异常类型。
例如:
voidfun()throw(A,B,C,D);
27)堆对象:
通过new运算符动态产生的对象
28)所谓模板是一种使用无类型参数来产生一系列函数或类的机制。
29)模板通过参数实例化可以构建具体的函数或类,称为模板函数和模板类。
30)如同函数模板一样,类模板是参数化的类,即用于实现数据类型参数化的类。
31)容器:
可容纳各种数据类型的数据结构。
32)迭代器:
可依次存取容器中元素的东西
33)算法:
用来操作容器中的元素的函数模板。
例如,STL用sort()来对一个vector中的数据进行排序,用find()来搜索一个list中的对象。
34)容器分为三大类:
1)顺序容器
vector:
后部插入/删除,直接访问
deque:
前/后部插入/删除,直接访问
list:
双向链表,任意位置插入/删除
2)关联容器
set:
快速查找,无重复元素
multiset:
快速查找,可有重复元素
map:
一对一映射,无重复元素,基于关键字查找
multimap:
一对一映射,可有重复元素,基于关键字查找
前2者合称为第一类容器
3)容器适配器
stack:
LIFO
queue:
FIFO
priority_queue:
优先级高的元素先出
2.程序阅读题
1)#include<
iostream>
usingnamespacestd;
classClxTest{
public:
ClxTest();
~ClxTest();
voidOutput()const;
intGetOutputTimes()const;
private:
mutableintm_iTimes;
};
ClxTest:
:
ClxTest()
{m_iTimes=0;
}
~ClxTest()
{}
voidClxTest:
Output()const
{cout<
<
"
Outputfortest!
"
<
endl;
m_iTimes++;
intClxTest:
GetOutputTimes()const
{returnm_iTimes;
voidOutputTest(constClxTest&
lx)
{
cout<
lx.GetOutputTimes()<
lx.Output();
}
voidmain(){
ClxTestts1;
OutputTest(ts1);
2)#include<
string>
#include<
intmain(){
stringstrinfo="
//*---HelloWord!
......------"
;
stringstrset="
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
intfirst=strinfo.find_first_of(strset);
if(first==string:
npos){
cout<
notfindanycharacters"
endl;
return-1;
}
intlast=strinfo.find_last_of(strset);
if(last==string:
strinfo.substr(first,last-first+1)<
return0;
3)#include<
voidstring_replace(string&
strBig,conststring&
strsrc,conststring&
strdst){
string:
size_typepos=0;
size_typesrclen=strsrc.size();
size_typedstlen=strdst.size();
while((pos=strBig.find(strsrc,pos))!
=string:
npos){
strBig.replace(pos,srclen,strdst);
pos+=dstlen;
intmain(){
ThisisWinter,Winterisaprogrammer.DoyouknowWinter?
cout<
Orignstringis:
endl<
strinfo<
string_replace(strinfo,"
Winter"
"
wende"
);
AfterreplaceWinterwithwende,thestringis:
4)#include<
iostream.h>
voidfn(inta,intb)
{if(b==0)
cerr<
Zeroencountered.Themessageconnotberedirected.\n"
;
else
a/b<
endl;
voidmain()
{
fn(20,2);
fn(20,0);
5)#include<
iomanip>
usingnamespacestd;
voidmain()
{constintk=618;
setw(10)<
setfill('
#'
)<
setiosflags(ios:
right)<
k<
endl;
setbase(8)<
*'
)
resetiosflags(ios:
left)<
6)#include<
{doublex=22.0/7;
inti;
outputinfixed:
\n"
cout.setf(ios:
fixed|ios:
showpos);
//定点输出,显示+
for(i=1;
i<
=5;
i++)
{cout.precision(i);
x<
outputinscientific:
//清除原有设置,科学示数法输出
cout.setf(ios:
scientific,ios:
fixed|ios:
{cout.precision(i);
x*1e5<
7)#include<
#include<
sstream>
{stringtestStr("
Inputtest256*0.5"
);
strings1,s2,s3;
doublex,y;
istringstreaminput(testStr);
input>
s1>
s2>
x>
s3>
y;
s1<
ends<
s2<
s3<
y<
="
x*y<
8)#include<
fstream>
{ofstreamost;
ost.open("
c:
\\\\my1.dat"
);
ost<
20<
30.5<
ost.close();
ifstreamist("
\\\\my1.dat"
intn;
doubled;
ist>
n>
d;
n<
endl<
d<
9)#include<
voidmain()
{fstreamf("
d:
\\DATA.dat"
ios:
in|ios:
out|ios:
binary);
for(i=0;
i<
20;
i++)f.write((char*)&
i,sizeof(int));
longpos=f.tellp();
for(i=20;
40;
i++)f.write((char*)&
f.seekg(pos);
f.read((char*)&
Thedatastoredis"
f.seekp(0,ios:
beg);
i++)
{f.read((char*)&
i<
ends;
10)#include<
classBase1
{public:
Base1(intx){value=x;
intgetData()const{returnvalue;
protected:
intvalue;
classBase2
Base2(charc){letter=c;
chargetData()const{returnletter;
charletter;
classDerived:
publicBase1,publicBase2
public:
Derived(intval,charlet,doublere):
Base1(val),Base2(let){
real=re;
value<
"
letter<
real<
doublegetReal()const
returnreal;
private:
doublereal;
Base1b1(10);
Base2b2('
k'
Derivedd(5,'
A'
2.5);
11)#include<
classB1//声明基类B1
public:
//外部接口
intnV;
voidfun(){cout<
MemberofB1"
classB2//声明基类B2
MemberofB2"
classD1:
publicB1,publicB2
//同名数据成员
MemberofD1"
}//同名函数成员
voidmain()
{D1d1;
d1.nV=1;
//对象名.成员名标识,访问D1类成员
d1.fun();
d1.B1:
nV=2;
//作用域分辨符标识,访问基类B1成员
fun();
d1.B2:
nV=3;
//作用域分辨符标识,访问基类B2成员
12)#include<
try
在tryblock中,准备抛出一个异常."
//这里抛出一个异常(其中异常对象的数据类型是char*)
char*p=0;
throwp;
//catch(char*value)
//注意这里catch语句
catch(...)
在catch(…)block中,char*类型的异常对象也被处理"
<
13)#include<
classCA
CA(intx,inty)
{a=x;
b=y;
intGetSum()
{returna+b;
private:
inta,b;
CA*pa;
pa=newCA(3,5);
//newCA();
sum="
pa->
GetSum()
deletepa;
14)#include<
vector>
intmain()
std:
vector<
int>
v1;
v2;
v1.push_back(5);
v1.push_back
(1);
v2.push_back
(1);
v2.push_back
(2);
v2.push_back(3);
cout<
(v1<
v2);
15)#include<
vector<
v;
//一个存放int元素的向量,一开始里面没有元素
v.push_back
(1);
v.push_back
(2);
v.push_back(3);
v.push_back(4);
const_iteratori;
//常量迭代器
for(i=v.begin();
i!
=v.end();
i++)
cout<
*i<
reverse_iteratorr;
//反向迭代器
for(r=v.rbegin();
r!
=v.rend();
r++)
*r<
iteratorj;
//非常量迭代器
for(j=v.begin();
j!
j++)
*j=100;
i++)
3.程序填空题
1、用函数模板实现3个数值中按最小值到最大值排序的程序。
iostream>
template<
classT>
voidsort(Ta,Tb,Tc)
Tarray[3],temp;
inti,j;
array[0]=a;
array[1]=b;
array[2]=c;
for(i=0;
3;
i++)
for(j=0;
j<
2;
j++)
if(array[j]>
array[j+1])
{temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
array[0]<
array[1]<
array[2]<
{sort(5,1,9);
2、用get函数从键盘输入字符
{charc;
EnterfirstsentencefollowedbyEnter\n"
while((c=cin.get())!
='
\n'
)cout.put(c);
EntersecondsentencefollowedbyEnter\n"
while(cin.get(c))
{if(c=='
)break;
cout.put(c);
EnterthirdsentencefollowedbyEnter\n"
chars[80];
cin.get(s,10);
s<
3、用输出串流对象将要输出的string对象插入
{ostringstreamOutput;
Inputx:
cin>
x;
Inputy:
Output<
*"
="
cou
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ 程序设计 资料
![提示](https://static.bdocx.com/images/bang_tan.gif)