using namespace std; //标准名字空间
C++的类型转换 static_cast<int>(5.4)等于 C语言中的(int)5.4
#include<ctime>//FOR time function
#include<cstdlib>//FOR rand and srand function
srand(time(0))//func1tion change the value of seed
int num=rand()//get random number
switch 语句的直通现象 当某个switch进行了匹配 会到第一个break 开始结束
#include<iomanip>对输出格式进行操纵
setw()制定打印的宽度 cout<<setw()<<"C++"
rignt 右对齐 left 左对齐
setw()制定打印的宽度 cout<<setw()<<"C++"
rignt 右对齐 left 左对齐
cout<<right
cout<<setw(8)<<"C++";
cout<<setw(8)<<"C++";
cout<<setw(8)<<"C++";
cout<<setw(8)<<"C++";
枚举类型 enum day{one,two,three};//枚举类型实际上是以整数存储的 次序与整数0、1、2、3
对应
对应
void function can use "RETURN;" to finish
C++ give a special variable called "reference variable"
形参将成为实参的一个引用 任何作用到引用变量的改变都会作用到
原变量上!
for example: count=1;
int &recount=count;
recount++;
cout<<count ===== cout<<recount
C++的关键函数重载!!
C++中函数的名字相同 但是他的参数列表不相同
但是会存在 二义调用的问题
void max(int x,int y) void max(double x,double y)
{ {}
}
C++中函数的名字相同 但是他的参数列表不相同
但是会存在 二义调用的问题
void max(int x,int y) void max(double x,double y)
{ {}
}
函数原型 函数的提前声明 实际上是忽略了参数的名字的
int max(int ,int)
默认参数
全局变量的默认值为0 局部变量没有默认值
static 静态变量
数组中第一个的元素的地址是1000 则指针加1的地址为1000+sizeof(int)
一个数组 实际上是一个常量指针
const常量关键词 const double* onstpvalue=&radius 指针是常量 数据也是常量
conse=t double* pvalue-&radius 指针不是常量 指针指向的数据是常量
const关键词在声明函数参数是特别有用
const关键词在声明函数参数是特别有用
c++支持动态内存分配 使用new
int *pValue=new int ;
int *list=new int[10]
局部变量在栈中分配空间,而new操作符分配的内存空间出自堆的分配, 分配的内存空间一直都是可用的,除非你显示的释放它
释放NEW操作符分配的内存空间
delete pvlaue;
delete pvlaue;
C++提供的若干字符检测函数 头文件<cctype>
for example : isdigit(c)是数字返回真
tolower(c)若c为大写字母,返回对应的小写字母
toupper(c)若c为小写字母,则返回为大写字母。
对于数组而言,一旦进行了声明,地址就确定了
但对于指针方式,是允许被赋予一个新的地址的
for example : isdigit(c)是数字返回真
tolower(c)若c为大写字母,返回对应的小写字母
toupper(c)若c为小写字母,则返回为大写字母。
对于数组而言,一旦进行了声明,地址就确定了
但对于指针方式,是允许被赋予一个新的地址的
C字符串函数 <cstring>
一个类还提供一些特殊的函数,这些函数称为构造函数用来执行初始化对象的数据域,
构造函数可以重载,重载的函数签名不同,可以用来初始化不同的数据类型
注意,构造函数的名字必须与类名相同,且没有返回类型
例子: Circle circel2(5.5)------>调用带实参的构造函数,指定半径为5.5
使用运算符“.”对象成员访问运算符,访问对象的数据
class object1---------->类名
{
public:------->the symbol of data domain
double radius;------------------->数据域--->notic! the data domain can not operate initialization
double radius;------------------->数据域--->notic! the data domain can not operate initialization
object1()----------------->无参构造函数
{
radius=1;
{
radius=1;
}
object1(double newRadius)----------->有参构造函数
{
radius=newRadius;
}
double getarea()------------------>成员函数
{
return radius*3.14*2;
}
{
return radius*3.14*2;
}
};---------------->pay attention to the ';'
类的声明与实现分离
类声明简单列出了数据域、构造函数原型、成员函数原型、类实现给出了构造函数和成员函数的实现,两者置于两个分离的文件里面,两个文件
应当使用相同的名字,但是扩展名字不相同,类声明是.h 类实现文件名为.cpp
例子: class Circle------------------->类声明Circle.h文件
{
public:
double radius;
Circle();
Circle(double);
double getarea();
};
#include"Circle.h"------------------>类实现 Circle.cpp
Circle::Circle() -------->notic!every construct must add the symbol of ";;"to tell the compiler " these functions were defined in the Cirle class "
{
Circle::Circle() -------->notic!every construct must add the symbol of ";;"to tell the compiler " these functions were defined in the Cirle class "
{
radius=1;
}
Circle::Circle(double newRadius)
{
radius=newRadius;
}
double Circle::getarea()----------->notic!every functions must add the symbol of ";;"to tell the compiler " these functions were defined in the Cirle class "
{
return radius * radius *3.1415
}
对象名一旦声明就无法修改,可以创建对象的指针,只要需要,任何时候都可以将对象地址赋指针;
Circle circle;
Circle *p=&circle;
Circle *p=&circle;
Notice!需要注意的是在一个函数中声明创建的Circle类对象,创建在栈中,当函数返回时,对象就会被销毁
Notice!为保留对象 我们可以在堆中创建对象,使用NEW操作符
Classname *object= new Classname();---------->使用无参构造函数创建一个对象,并将对象地址赋予指针;
Classname *object2=new Classname(arguement);
显示的销毁动态对象 delete object1/object2;
Notice!为保留对象 我们可以在堆中创建对象,使用NEW操作符
Classname *object= new Classname();---------->使用无参构造函数创建一个对象,并将对象地址赋予指针;
Classname *object2=new Classname(arguement);
显示的销毁动态对象 delete object1/object2;
构建一个字符串
构架字符串文字常量----------> string message("hello");
char charArray=['G']
string message(charArray);
string data;
data.data();当前字符串以一个数组返回;
data.length();返回当前字符串的长度;与data.size();相同
构架字符串文字常量----------> string message("hello");
char charArray=['G']
string message(charArray);
string data;
data.data();当前字符串以一个数组返回;
data.length();返回当前字符串的长度;与data.size();相同
追加字符串,向字符串添加新的内容
s1.append(" C++")
s1.append(" C++")
assign用于字符串的赋新值
string s1("hello")
s1.assign("hi");--->the "hello" change into "hi";
string s1("hello")
s1.assign("hi");--->the "hello" change into "hi";
at提取字符串中指定位置的字符
s1.at(3);
s1.at(3);
s1.clear();用于清空字符串
s1.erase(2,3);///erase擦除 指定位置,擦除后 字符串的容量并不会改变。
s1.length()====s1.size()///字符串的长度;
s1.capacity();///字符串的容量
s1.capacity();///字符串的容量
string s1("hello")
s1.erase(1,2);//erase 1 and 2;
s1.size()=s1.length()=====3表示字符串的长度
s1.capacity();----------->任为5;
s1.erase(1,2);//erase 1 and 2;
s1.size()=s1.length()=====3表示字符串的长度
s1.capacity();----------->任为5;
字符串的比较
s1.compare(s2);
仍然为返回 负数 0 正数
s1.compare(s2);
仍然为返回 负数 0 正数
为了避免bug,和外部对象直接修改数据域,我们可以对数据域进行封装
class Circle
{
public:
Circle();
Circle(double);
double getarea();
private:
double radius;
};
程序之外无法通过直接引用对象访问它
通过get 和 set
class Circle
{
public:
Circle();
Circle(double);
double getAera()
double getRadius();
void setRaduis(double);
private:
double raduis;
程序之外无法通过直接引用对象访问它
通过get 和 set
class Circle
{
public:
Circle();
Circle(double);
double getAera()
double getRadius();
void setRaduis(double);
private:
double raduis;
};
局部变量在函数内声明,只能在函数内使用,如果一个局部变量与数据域具有相同的名字,数据域将会被屏蔽,因为局部变量的优先级更高。
局部变量在函数内声明,只能在函数内使用,如果一个局部变量与数据域具有相同的名字,数据域将会被屏蔽,因为局部变量的优先级更高。
Circle::Circle(double radius)
{X
this->radius=radius;
}
void Circle::setRadius(double radius)
{
this->radius=(radius>=0) ? radius:0;
0 '
0 '
}
避免重复声明
#ifndef用来防止一个头文件加载两次,
#ifndef DATA_H
define DATA_H
'''''
''''
#endif
#ifndef用来防止一个头文件加载两次,
#ifndef DATA_H
define DATA_H
'''''
''''
#endif
实例成员与静态成员
Circle A
Circle b(5) A和b的radius 不共享,存储的内存位置不相同,
如果希望一个类的所有实例共享数据,那么应该使用静态变量,
static int numberOfObjects;
static int getNumberObjects();
Circle b(5) A和b的radius 不共享,存储的内存位置不相同,
如果希望一个类的所有实例共享数据,那么应该使用静态变量,
static int numberOfObjects;
static int getNumberObjects();
int Circle::numberOfObjects=0;数据域进行初始化
析构函数 对象被创建时构造函数被调用,当对象销毁时析构函数被调用
波浪线Circle()析构函数没有返回类型和参数
只有动态创建于堆中的对象 ,才能使用delet操作符显式的销毁
作用于当销毁一个对象时,也应该销毁对象中包含的另一个对象,这样可以避免内存的泄露