C++中的一些操作笔记
1.C++中建立对象
#include <stdio.h>
//通过class关键字类定义类
class Student{
public:
//类包含的变量
char *name;
int age;
float score;
//类包含的函数
void say(){
printf("%s的年龄是 %d,成绩是 %f\n", name, age, score);
}
};
int main(){
//通过类来定义变量,即创建对象
class Student stu1; //也可以省略关键字class
//为类的成员变量赋值
stu1.name = "小明";
stu1.age = 15;
stu1.score = 92.5f;
//调用类的成员函数
stu1.say();
return 0;
}
1.1C++中的类class
对象object
类的定义比C语言中的结构体struct多出了能定义函数,且能忽略关键字。
两个语言的组织方式区别:
2.C++与C的编译的区别
GCC
中有对应的编译器叫g++
.
3.C++中的命名空间
namespace Li{ //小李的变量定义
FILE* fp = NULL;
}
namespace Han{ //小韩的变量定义
FILE* fp = NULL;
}
使用方式:
Li::fp = fopen("one.txt", "r"); //使用小李定义的变量 fp
Han::fp = fopen("two.txt", "rb+"); //使用小韩定义的变量 fp
3.1其中的::
符号
::
符号是C++中的 域解析操作符
在C++中用来解析要使用的命名空间
3.2using
关键字的使用
using Li::fp;
fp = fopen("one.txt", "r"); //使用小李定义的变量 fp
Han :: fp = fopen("two.txt", "rb+"); //使用小韩定义的变量 fp
using namespace Li;
fp = fopen("one.txt", "r"); //使用小李定义的变量 fp
Han::fp = fopen("two.txt", "rb+"); //使用小韩定义的变量 fp
以上两段代码都分别使用了using
关键字,其主要的作用就是声明后面使用的变量,如第一段出用using
定义了Li
的fp
那么在后续的程序中就可以直接用fp
来代替Li::fp
了
同样的该using
关键字还能针对整个命名空间
3.3在声明命名空间的时候需要注意的点
下面看两段代码:
#include <iostream>
void func(){
//必须重新声明
using namespace std;
cout<<"http://c.biancheng.net"<<endl;
}
int main(){
//声明命名空间std
using namespace std;
cout<<"C语言中文网"<<endl;
func();
return 0;
}
#include <iostream>
//声明命名空间std
using namespace std;
void func(){
cout<<"http://c.biancheng.net"<<endl;
}
int main(){
cout<<"C语言中文网"<<endl;
func();
return 0;
}
其中第一段中的using namespace std
需要注意一下,如果在不同函数中用到该变量空间就需要重新声明,第二段中使用了全局的声明方式,这样所有的函数中就都可以使用命名空间std
了。
但是原则上是第一种更加的规范。
4.C++中的常量
前缀为0x或者0X
表示十六进制
,前缀为0
表示8进制,C++中还会用到一些后缀,其中L
表示长整数
,U
表示无符号整数
,UL
就表示无符号长整数
.
常量是固定值,可以是任何的基本数据类型
,常量的值在被定义后就不能修改了。
85 // 十进制
0213 // 八进制
0x4b // 十六进制
30 // 整数
30u // 无符号整数
30l // 长整数
30ul // 无符号长整数
5.C++中的存储类
C++中的存储类有auto
,register
,static
,exteren
,mutable
,thread_local(仅在C++11以上)
.
- auto存储类
- 声明变量时根据初始化表达式自动推断该变量的类型.
- 声明函数时函数返回值的占位符.
auto f=3.14; //double
auto s("hello"); //const char*
auto z = new auto(9); // int*
-
register存储类
- 该存储类的定义存储在寄存器中而不是存储在RAM中局部变量,其大小取决于寄存器的大小,且不能对其使用
&
符号。
- 该存储类的定义存储在寄存器中而不是存储在RAM中局部变量,其大小取决于寄存器的大小,且不能对其使用
-
mutable存储类
- 只适用于类的对象。
-
thread_local存储类
- 仅用于其创建的线程上的变量的的定义,且在线程销毁时销毁。
6.C++中的头文件库
可以兼容传统C的带.h
的头文件,新标准的C++将原本的库都去了.h
直接命名,除了C++自带的iostream
不用在前面加上c
以外其他的一些C原本自带的库需要添加c
。
例如:
stdio.h
->cstdio
stdlib.h
->cstdlib
需要注意的是:对于不带.h
的库在使用的时候,都应在命名了std
命名空间后再使用其中的符号。
例如:
#include <cstdio>
int main(){
std::printf("http://c.biancheng.net\n");
return 0;
}
7.C++中的cin和cout的使用
看一段代码:
#include "cstdio"
#include "iostream"
int main(int argc,char **argv)
{
using namespace std;
int x = 0;
int y = 0;
cout << "please input num x,y" << endl;
cin >> x >> y;
cout << "you put in the num of x = " << x << ", y = "<< y <<endl;
return 0;
}
其中头文件需要注意此处我已经改回来了,因为没有带.h
文件所以在使用变量的时候需要声明变量空间。
可以看到输出时使用的是<<
如果到了文章的结尾可以用一个endl
,(end of line)
来表示输出的末尾为最后一个字符。输入时使用的是>>
8.C++中的内存分配
在C语言中动态分配内存常常会用到malloc()
函数和free()
函数。而在C++中除了包含以上的内存分配方法还新增加了new
和delete
关键字来分配内存。可以看以下两段代码。
C中实现内存分配
int *p = (int*) malloc(sizeof(int)*10);//分配10个int型的内存
free(p);//释放分配的内存空间
C++中实现内存分配
int *p = new int;//分配1个int类型的空间
delete p;//释放该空间
int *p = new int[10];//分配10个int类型的内存空间
delete[] p;//释放这些空间
需要注意的是C++中在内存分配的时候需要将两个对应起来使用,即new
对应delete
,new[]
对应delete[]
且不要将这些与malloc
以及free
混用。
9.C++中的继承
类的继承有点像是在说明什么是什么的意思,类似的狗是动物
可以用以下的代码来表示
// 基类
class Animal {
// eat() 函数
// sleep() 函数
};
//派生类
class Dog : public Animal {
// bark() 函数
};
由以上的代码可以看出狗是动物类的继承,所以他除了动物的一些基本函数能使用它还能使用bark()
函数。
下面看个例子:
#include "iostream"
#include "cstdio"
using namespace std;
class shape//建立一个类
{
public:
int y;
void youIn()
{
cout << "put in your num" << endl;
cin >> y;
cout << "your num is " << y << endl;
}
void yourName()
{
cout << "my name is Ryan" <<endl;
}
protected:
int num = 1;
int num1 = 2;
};
class otherfun : public shape//继承上面一个类
{
public:
void putOutNum()//输出被保护的值
{
cout << "the num = " << num << " and num1 = "<< num1 << " is be protected " << endl;
}
void changeNum()//继承的类里面改变num和num1的值
{
num = 3;
num1 = 4;
cout << "the num is ch = " << num << " the num1 is ch = " << num1 << endl;
}
int getNum()
{
return num*num1;
}
};
int main(int argc,char** argv)
{
otherfun smalltest;
smalltest.youIn();
smalltest.yourName();
smalltest.putOutNum();
smalltest.changeNum();
cout << "the num multiply is " << smalltest.getNum() << endl;
return 0;
}
该部分的代码主要是做了一个测试程序,其中对于protected
部分做了一个测试。
protected
中的程序变量是可以在继承的类里面进行改变的,但是在main中是不能使用的,所以要在main中用到变量的值的运算,就应该在继承的类里面进行完成。
下面使用的范围可以用下表来体现