C++复习.docx
《C++复习.docx》由会员分享,可在线阅读,更多相关《C++复习.docx(28页珍藏版)》请在冰豆网上搜索。
![C++复习.docx](https://file1.bdocx.com/fileroot1/2023-2/3/70aeaab8-fcba-4935-8dc9-7ba2fbe6fd2d/70aeaab8-fcba-4935-8dc9-7ba2fbe6fd2d1.gif)
C++复习
第一章绪论
面向对象的概念:
类,对象,封装,继承,多态
第二章C++语法
1.常量和变量。
2.标识符
3.算术运算、关系运算、逻辑运算、赋值运算、逗号运算、条件运算。
4.运算符的优先级、左结合和右结合规则。
5.控制结构:
循环,选择
第三章函数
1.C++中函数的定义、调用以及主调函数和被调函数之间的数据传递规则。
2. 函数的返回值和类型。
3. 内联函数的概念和应用。
4. 函数重载的概念和使用场合
第四章类与对象
1.类与对象的概念和定义方法,类成员的访问属性。
2.构造函数、定义方法和使用方法。
3.析构函数的概念、定义方法和使用方法。
4.拷贝构造函数的概念、定义方法和使用方法。
5.包含对象成员的类构造函数的定义方法。
6.静态成员的概念、定义方法、作用和特点。
7 .友元的概念、定义方法、作用和特点。
第六章指针,数组
1.指针、地址、指针类型、空指针(NULL)等概念;
2.掌握指针变量的定义和初始化、指针的间接访问、指针的加减运算、指针变量比较运算和指针表达式;
3.动态空间操作(new,delete);
4.掌握引用参数的使用
第七章继承
1.继承和派生的定义,派生类的定义方法。
(1)继承的两种类型:
单继承和多继承。
(2)private,public,protected三种继承方式的特点。
(3)派生类中的构造函数和析构函数的使用。
(4)派生类的同名覆盖规则。
(5)赋值兼容规则。
基类对象可以使用公有派生类对象来代替,包括:
派生类对象可以赋值给基类对象;派生类对象可以初始化基类对象的引用;基类类型指针可以指向派生类对象。
2. 多重继承、定义方法、多重继承派生类构造函数的执行顺序。
3. 掌握虚基类的概念和定义方法。
第八章多态
1.多态性
2.多态从实现的角度来讲可以划分为两类:
编译时的多态和运行时的多态。
3.运算符重载
4.虚函数
5.纯虚函数
6.抽象类
复习题:
11、如果s是int型变量,且s=8,则下面s%2+(s+1)%2表达式的值为。
12、x为int类型,指出下列表达式值的类型,x<100||x>200:
。
13、执行以下程序段后,输出结果值为。
intmax(inta,intb=10)
{returna>b?
a:
b;}
intmax(inta,intb,intc)
{intt;t=max(a,b);returnmax(t,c);}
voidmain()
{cout<14、利用“对象名.成员变量”形式访问的对象成员仅限于被声明为public的成员;若要访问其他成员变量,需要通过。
19、设有基类定义:
classCbase
{private:
inta;protected:
intb;public:
intc;};
Cbase的派生类采用何种继承方式可以使成员变量c成为自己的私有成员。
11、设所有变量均为整型,则表达式(e=20,f=50,e++,f++,e+f)的值为____。
16、内联函数在定义或说明时前面要加上关键字。
21、运行结果:
21、#include
intadd(intx,inty=10);
voidmain()
{ inta=15;
cout<cout<}
intadd(intx,inty)
{returnx+y;}
22、
22、运行结果:
#include”iostream.h”
f(inta)
{intb=10;
staticintc=6;
b++;c++;
return(a+b+c);
}
voidmain()
{inta=8,i;
for(i=0;i<3;i++)
cout<}
23、运行结果为:
23、#include
#include
intcount=0;
classPoint
{intx,y;
public:
Point()
{x=1;y=1;
count++;
}
~Point(){count--;}
friendvoiddisplay();
};
voiddisplay(){cout<<"Thereare"<voidmain()
{Pointa;
display();
{Pointb[10];
display();
}
display();
}
24、#include
24、运行结果:
classpoint
{intx,y;
public:
point(inta,intb){x=a;y=b;cout<<"构造."<point(point&p);
friendpointmove(pointq);
~point(){cout<<"析构\n";}
intgetx(){returnx;}
intgety(){returny;}
};
point:
:
point(point&p)
{x=p.x;y=p.y;cout<<"拷贝构造\n";}
pointmove(pointq)
{cout<<"OK\n";
inti,j;
i=q.x+10;
j=q.y+20;
pointr(i,j);
returnr;
}
voidmain()
{pointm(15,40),p(0,0);
pointn(m);
p=move(n);
cout<<"p="<
}
21、运行结果:
21、#include
voidfun();
intn=15;
voidmain()
{intn=50;
cout<fun();
}
voidfun()
{cout<22、#include
22、运行结果:
voidfun();
voidmain()
{inti;
for(i=0;i<2;i++)
fun();
}
voidfun()
{staticintm=0;
cout<}
23、运行结果:
23、#include
#include
classPoint
{public:
Point(inta=0,intb=0){x=a;y=b;}
intxcord(){returnx;}
intycord(){returny;}
private:
intx,y;
frienddoubleDistance(Pointp1,Pointp2);
};
doubleDistance(Pointp1,Pointp2)
{doubledx=double(p1.x-p2.x);
doubledy=double(p1.y-p2.y);
returnsqrt(dx*dx+dy*dy);
}
voidmain()
{Pointq1(0,0),q2(30,40);
cout<<”Thedistanceis:
”<25、#include
classA{
25、运行结果:
public:
A(){cout<<"A构造."<virtual~A(){cout<<"A析构"<virtualvoidf(){cout<<"A'sf()."<voidg(){f();}
};
classB:
publicA{
public:
B(){f();cout<<"B构造"<~B(){cout<<"B析构"<};
classC:
publicB{
public:
C(){cout<<"C构造"<~C(){cout<<"C析构"<voidf(){cout<<"C'sf()."<};
voidmain()
{A*a=newC;
a->g();
deletea;
}
改错误。
#include
usingnamespacestd;
classtest
{
private:
intx,y=20;
public:
test(inti=0,intj){x=i,y=j;}
intgetx(){returnx;}
intgety(){returny;}
}
classtestderived:
publictest
{
public:
testderived(inti,intj){x=i;y=j;}
};
voidmain()
{testderivedmt(10,20);
cout<cout<return0;
}
27、#include
classtest
{
public:
voidtest(){cout<<"test构造函数\n";fun();}
virtualvoidfun()=0
{cout<<"test:
:
fun()函数\n";}
virtual~test(inti){cout<<"test析构函数\n"}
};
classtestderived:
test
{public:
testderived()
{cout<<"testderived构造函数\n";fun();}
voidfun(){cout<<"testderived:
:
fun()函数\n";}
}
voidmain()
{test*p=newtestderived(10);}
编程题:
1、
(1)定义一个类A,有保护成员:
x;公有成员:
print();
(2)定义一个派生类B,是A类的公有派生类。
B类本身的私有属性有:
y。
B的公有函数有:
init(),print();
(3)画出整体UML类图。
2、定义一个抽象类A,其中有2个纯虚成员函数,a1(),a2()。
在此基础上派生出类B和类C,实现虚函数a1()和a2()。
给出三个类的定义。
3、重载运算符++,--
附加题:
#include
classLocation
{public:
Location(intxx=0,intyy=0)
{X=xx;Y=yy;cout<<"ConstructorObject.\n";}
Location(constLocation&p)//复制构造函数
{X=p.X;Y=p.Y;cout<<"Copy_constructorcalled."<~Location(){cout<intGetX(){returnX;}intGetY(){returnY;}
private:
intX,Y;
};
voidf(Locationp){cout<<"Funtion:
"<
voidmain()
{LocationA(1,2);
f(A);
}
结果:
constructorobject.
Copy_constructorcalled.
Funtion:
1,2
1,2Objectdestroyed.
1,2Objectdestroyed.
#include
classLocation
{public:
Location(intxx=0,intyy=0)
{X=xx;Y=yy;
cout<<"Objectconstructed."<}
Location(constLocation&p)
{X=p.X;Y=p.Y;
cout<<"Copy_constructorcalled."<}
~Location()
{cout<intGetX(){returnX;}
intGetY(){returnY;}
private:
intX,Y;
};
Voidmain()
{Locationa;Locationb(a);Locationc;}
结果:
Objectconstructed.
Copy_constructorcalled.
Objectconstructed.
Objectdestroyed.
Objectdestroyed.
Objectdestroyed.
#include
classcounter
{staticintnum;
public:
voidsetnum(inti){num=i;}
voidshownum(){cout<};
intcounter:
:
num=0;
voidmain()
{countera,b;
a.shownum();
b.shownum();
a.setnum(10);
a.shownum();
b.shownum();
}
结果:
001010
#include
classA
{friendclassB;
public:
voidDisplay(){cout<private:
intx;
};
classB
{public:
voidSet(inti){Aobject.x=i;}
voidDisplay(){Aobject.Display();}
private:
AAobject;
};
voidmain()
{BBobject;
Bobject.Set(100);
Bobject.Display();
}
结果:
100
classx{
public:
voidf1();
voidf2();
voidprint()
{cout<<"m="<private:
intm;
};
voidx:
:
f1(){
m=5;
}
voidx:
:
f2(){
m=2;
}
voidmain(){
xobj;
obj.f1();
obj.print();
obj.f2();
obj.print();
}
结果:
m=5
m=2
inti=10;
voidmain()
{
inti=5;
if(i<10){
inti;
i=7;
cout<<"i="<
}
cout<<"i="<
cout<<":
:
i="<<:
:
i<}
结果:
i=7
i=5
:
:
i=10
#include
usingnamespacestd;
voidfun()
{
staticinta=1;
inti=5;
a++;
i++;
cout<<"i="<
}
intmain()
{
fun();
fun();
}
结果:
i=6,a=2
i=6,a=3
对象数组:
classPoint
{public:
Point();
Point(intxx,intyy);
~Point();
voidMove(intx,inty);
intGetX(){returnX;}
intGetY(){returnY;}
private:
intX,Y;
};
Point:
:
Point()
{X=Y=0;
cout<<"DefaultConstructorcalled."<}
Point:
:
Point(intxx,intyy)
{
X=xx;
Y=yy;
cout<<"Constructorcalled."<}
Point:
:
~Point(){
cout<<"Destructorcalled."<}
voidPoint:
:
Move(intx,inty){
X=x;
Y=y;
}
intmain(){
Point*Ptr=newPoint[2];//创建对象数组
Ptr[0].Move(5,10);//通过指针访问数组元素的成员
(Ptr+1)->Move(15,20);//通过指针访问数组元素的成员
cout<<"Deleting..."<delete[]Ptr;//删除整个对象数组
return0;
}
结果:
DefaultConstructorcalled.
DefaultConstructorcalled.
Deleting...
Destructorcalled.
Destructorcalled.
classPoint
{public:
Point();
~Point();
private:
intX,Y;
};
Point:
:
Point()
{X=Y=0;cout<<"DefaultConstructorcalled."<Point:
:
~Point()
{cout<<"Destructorcalled."<intmain(){
Point*Ptr=newPoint[2];
cout<<"Deleting..."<delete[]Ptr;
}
结果:
DefaultConstructorcalled.
DefaultConstructorcalled.
Deleting...
Destructorcalled.
Destructorcalled.
#include
classA
{public:
inta1,a2;
A(inti1=0,inti2=0){a1=i1;a2=i2;}
voidprint()
{cout<<"a1="<};
classB:
publicA
{public:
intb1,b2;
B(intj1=1,intj2=1){b1=j1;b2=j2;}
voidprint()//定义同名函数
{cout<<"b1="<voidprintAB()
{A:
:
print();//派生类对象调用基类版本同名成员函数
print();//派生类对象调用自身的成员函数
}
};
voidmain()
{Bb;b.A:
:
print();b.printAB();}
结果:
a1=0a2=0
a1=0a2=0
a1=1a2=2
#include
classB
{public:
staticvoidAdd(){i++;}
staticinti;
voidout(){cout<<"statici="<
};
intB:
:
i=0;
classD:
privateB
{public:
voidf();
{i=5;
Add();
B:
:
i++;
B:
:
Add();
}
};
voidmain()
{Bx;Dy;
x.Add();
x.out();
y.f();
cout<<"statici="<:
i<cout<<"statici="<//cout<<"statici="<}
结果:
statici=1
statici=1
statici=8
#include
classBase
{public:
Base(){cout<<"Basecreated.\n";}
}