计算机软件基础和c++复习.ppt
文本预览下载声明
计算机软件基础与C++复习
1.A better “C”
(1)引用必须初始化。
int count=0;
int refCount=count; initialize
refCount=1;
count + =count;
int* ptrCount=refCount;
;#define PAI 3.1415926 preprocessor;C++允许初始化新分配的对象
float *thingPtr=new float(3.14159);
char* pChar=new char(‘a’);;
; (4)函数原型
C++与 C不同之处是声明使用原型,以保证实参和形参类型一致(编译器检查)
;在第一次出现该函数名时指定
可用于内联函数
默认参数的定义和匹配遵从最右原则; (6)函数重载(Function Overloading)
C++把同一作用域内名字相同,但参数不同的函数称为重载函数。这得益于函数原型不仅给出函数名,而且指出了参数类型。
为了保证编译器根据参数类型识别重载函数,必须保证重载函数的参数不同。;Play the piano
Play basketball
Play with toy
Play the stock market
………; 仅返回值类型不同导致二义性
int print(int);
double print(int);
仅用const或引用使参数类型有所不同
int print (const int );
int print (int );
类型因加long,short,signed,unsigned而不同的参数,若无法根据实参判断相应类型而导致二义性
void print(unsigned int);
void print(int);
print (1l); //出错
print (1u); //正确
缺省参数的使用而导致二义性
void print(int a,int b=1);
void print(int a);
print(11);
;2.ClassObject;Class definition;Access specifiers;private;Initialization Cleanup;The constructor;How a constructor does?; class stack
{
private:
char v[100] ;
char *p ;
public:
stack()
{
p=v;
}
void push(char c)
{…}
char pop()
{…}
};; class stack
显示全部