<introduction to programming with c++>
1.数组声明:
double myList[2];
myList[0]=1.0;
myList[1]=2.0;
或
double myList[2]={1.0,2.0};
char city[]="Dallas";//可以把每一个字符变成city[i]
或int myList[rowsize][columnsize]
int array[4][3]={{1,2,3},
{4,5,6},
{7,8,9},
{10,11,12}};
2.main方法外的其他方法必须要在程序前加上function prototype:
void m1();
int m2();
......
3.可以为方法设置default argument:
void t1(int x, int y=0, int z=0);
void t2(int x=0, int y=0, int z=0);
PS.the parameters with default values must be defined last.
t1(1, ,20);//illegal
t2(, ,20);//illegal
t1(1);//parameters y and z are assigned a default value
4.
t2(1,2);//parameter z is asigned a default value
5.global variable和static variable
global variable可以在整个程序中使用
6.常量表示
如:const TOTAL_NUMBERS; 不能被修改。在方法中也可以定义如:void m(const int list[])
7.头文件(header files)
**.h是可以调用的function库,若要调用某个头文件的function,在程序开始前先要声明#include "**.h"
8.传递引用
void printArray(int list[])//Java 中是 void printArray(int[] list)
9.指针
#include<iostream>
using namespace std;
int main()
{
int count=5;
int *pCount=&count;
cout<<"the address of count is"<<&count<<endl;
cout<<"the address of count is"<<pCount<<endl;
cout<<"the value of count is"<<count<<endl;
cout<<"the value of count is"<<*pCount<<endl;
system("PAUSE");
return 0;
}
the address of count is 0012ff60
the address of count is 0012ff60
the value of count is 5
the value of count is 5
按任意键继续
px是内存地址,*px是指这个引用的值.
//double *pArea;//wrong
10.using const with pointers
double *const pValue=&radius;//指针指定的地址不能变
const double *const pValue=&radius;//指针指定的地址和这个值不变
const double *pValue=&radius;//只是值不变
11.new 操作符
int *pValue = new int;
或int *list = new int[10];//数组
//在方法中定义,用new的话可以一直在内存中储存
//要destroy的话用delete。 如delete pValue, delete [] list;
12.char city[7]="Dallas";//cannot be reassigned
char *pCity = "Dallas";//can be reassigned
char *const pCity = "Dallas";//cannot be reassigned
可以用以下方法display all characters
cout<<city;或
cout<<pCity;
但是
cout<< *pCity;//只打印出D
13.reading string
cin.getline(char array[],int size,char delimitChar)
读size个字符,或读到delimitChar时停止,delimitChar默认为'/n'。
例如:char city[30];
cout<<"enter a city:";
cin.getline(city,30,'/n');
cout<<"you entered"<<city <<endl;
14.处理字符和字符串的一些函数
15.Circle circle1;//无参构造
Circle circle2(5.0);//有参构造
用circle2=circle1;//可以将circle1的data field复制给circle2,但两者还是不同的对象
16.类的构造方法
无参有名 Circle circle1;
无参无名 Circle();
有参有名 Circle circle(x);
有参无名 Circle(x);
17.类的声明放在header filer里,记得最后面加";".
类的执行(实现implementation)则放在.cpp文件中
类的声明只要列出一些合约(contract)(列出prototype即可)
具体程序中派生出对象则在另外的.cpp文件中。
18.用指针来access object members
Circle circle;
Circle *pCircle = &circle1;
然后可以:
(*pCircle).radius
(*pCircle).getArea
pCircle->radius
pCircle->getArea()
也可以
Circle *pCircle1=new Circle();
Circle *pCircle2=new Circle(5.0);
此时要destroy就调用delete
19.处理字符串有一个string类
20.在类方法中,若一个方法变量与data field重名时,方法变量优先,data field暂时隐去
21.1st passObjectbyValue
m(Circle c){}//access: c.getArea();
2nd passObjectbyReference
m(Circle &c){}//access: c.getArea();
3rd passObjectbyPointer
m(Circle *c){}//access: c->getArea();
22.Array of Object
Circle circleArray[10];//创建了10个无参的对象
Circle circleArray[3]={Circle(3),Circle(4),Circle(5)};
23.this 是一个pointer 用this->a=a;
24.C++中类的数据域不能赋予初值。
25.若A类的数据域中要用到B b;而B类中没有无参构造方法,那么编译会出错,这种情况下则必须在A类的构造方法里加上constructor initializer
A(parameterslist)
:b(**,**,**,...)
{}
26. ~Classname(){
do something
}
这是在调用delete时喜用自动的destructor方法
(以下来自孙鑫)
27.class A{}; 注意加上此分号
28.~Fun()一个类有构造函数,也有析构函数,用来回收内存。不允许有返回值,也不允许带参数,一个类中只能有一个析构函数。
记得这种表示:
class Animal{
public:
Animal(int height,int weight){
cout<<"animal construct"<<endl;
}
~Animal(){
//optional to do something
}
};
class fish: public Animal{
public:
fish():Animal(3,4),a(1) //这种表示方法是指先将animal类用参数3和4构造,然后再构造fish,然后将1这个值赋给常量a.
{
//fish construct...
}
~fish()
{
//optional to do something
}
const int a; //常量必须初始化
};
29.public:
protected:在子类中可以访问,在外部不能访问
private:子类中也不能访问
30.基类的访问特性 类的继承特性 子类的访问特性
Public Public Public
Protected Protected
Private No access
Public Protecetd Protected
Ptotected Protected
Private No access
Public Private Private
Protected Private
Private No access
31.int a=6;
int &b=a; 引用:只有在定义的时候才表示引用,表示b是a的别名;b不需要存储空间,这就是和指针的区别
32.以下两种交换a和b的值的方法:
int a=0;
int b=1;
int change(int *pA,int *pB)
{
int temp=*pB;
*pB=*pA;
*pA=temp;
return 1;
} 传递时用change(&a, &b)。
int change(int &a,int &b)
{
int temp=b;
b=a;
a=temp;
return 1;
} 为了表述清楚,区别两者,第二个表示的是引用,直接代表交换两者的值。在传递的时候直接change(a,b)即可。
当然还有
int change(int a, int b)
{
int temp=b;
b=a;
a=temp;
return 1;
}
33.实例化的子类在内存中的存储形式
---------------------------------------- <---实例化出来的子类的首地址
父类(Father) virtual bool X(){}
bool Y(){}
-- -- -- -- -- -- -- -- -- -- -- -- -- --
子类的特性
(Child)
bool X(){//......}
bool Y(){//.....}
----------------------------------------
父类中如果有virtual 函数,而子类将其重写了,利用子类调用到的该函数则是子类中的函数;如果子类中重写的函数在父类中不是virtual,那么通过这个子类调用到的该函数则是父类中的函数