一、C++概述
(1)C++编程头脑
面向对象:通过分析出解决问题所必要的步调,然后用函数把这些步调一步一步实现。
面向对象:算法与数据看成一个整体。步伐=对象+对象+对象。
(2)C++面向对象的三大特征
封装:将相同属性的数据和方法封装在一起,加权限区分,用户只能借助公共方法操作私有数据。
继承:体现在类与类之间的关系,如果A类继承于B类,那么A类直接拥有B的数据与方法。
多态:一个接口(函数),拥有多种功能。
(3)作用域::
::解决归属问题
- int a = 30;//全局变量
- void test()
- {
- int a =20;//局部变量
- cout<<"局部变量a="<<a<<endl;
- cout<<"全局变量a="<<::a<<endl;//作用域
- }
复制代码 (4)输入输出
- #include <iostream>//标准输入输出流
- //使用std命名的空间
- using namespace std;
- //不需要对数据进行类型说明
- //输入
- cin>>a>>endl;
- //输出
- cout<<a<<endl;
复制代码 二、命名空间
(1)关键字
利用关键字namespace,控制名称的作用范围。
命名空间的本质:对符号常量、变量、函数、布局、枚举、类、和对象等举行封装。
注:命名空间只能定义在全局
(2)命名空间的创建
- namespace 空间名称
- {
- //定义变量、函数、类型
- }
复制代码 例:
- namespace A //命名空间A
- {
- int num = 10;
- char str = 'd';
- }
- namespace B //命名空间B
- {
- int num = 20;
- char str = 'w';
- }
- void test2()
- {
- //需加作用域指明数据,否则产生歧义
- cout<<A::data<<endl;
- cout<<B::str<<endl;
- }
复制代码 (3)命名空间嵌套
- namespace A //命名空间A
- {
- int data = 10;
- char str = 'd';
- namespace C //命名空间C
- {
- int data = 30;
- char str = 'r';
- }
- }
复制代码 命名空间A 嵌套 命名空间C
- //需加作用域
- //打印A的内容
- cout<<A::str<<endl;
- //打印C的内容
- cout<<A::C::data<<endl;
复制代码 (4)命名空间可以随时参加新成员
- namespace name1
- {
- int data = 60;
- }
- namespace name1//加入新成员
- {
- char str = 'q';
- void fun(void)
- {
- cout<<"name1中的fun函数"<<endl;
- }
- }
复制代码 (5)命名空间声明与实现分离
- namespace name
- {
- //函数在命名空间内布声明
- void fun1();
- void fun2();
- }
- //函数在命名空间外实现
- void name::fun1()
- {
- cout<<"name中的fun1"<<endl;
- }
- void name::fun2()
- {
- cout<<"name中的fun2"<<endl;
- }
- void test()
- {
- name::fun1();
- name::fun2();
- }
复制代码 (6)无名命名空间
无名命名空间内容只能在本文件内访问
- namespace
- {
- int num = 100;
- }
- void test()
- {
- //使用方式
- cout<<"num= "<<::num<<endl;
- cout<<"num= "<<num<<endl;
- }
复制代码 (7)命名空间取别名
利用方法:namespace 别名=原名
- namespace AName
- {
- int a= 100;
- int b= 300;
- }
- namespace BName=AName;//取别名
- void test()
- {
- cout<<"a="<<BName::a<<endl;
- cout<<"a="<<AName::a<<endl;
- }
复制代码 (8)利用using说明命名空间中的某几个成员可用
- namespace name1
- {
- int a = 10;
- int b = 20;
- int c = 30;
- }
- namespace name2
- {
- int a = 10;
- int b = 20;
- int c = 30;
- }
- void test()
- {
- using name2::a;//之后的a均指命名空间name2中的a
- cout<<"a="<<a<<endl;//直接使用变量名就可以,不需加作用域,只在当前{}有效
- cout<<"b="<<name2::b<<endl;
- cout<<"c="<<name2::c<<endl;
- }
复制代码 如果命名空间包含一组用相同名字的重载的函数,using声明就声明白这个重载函数的所有聚集
- namespace name4
- {
- void fun1(void)
- {
- cout<<"fun1"<<endl;
- }
- void fun1(int x)
- {
- cout<<"fun2"<<endl;
- }
- void fun1(int x,int y)
- {
- cout<<"fun3"<<endl;
- }
- }
- void test8()//using使用
- {
- using name4::fun1;
- fun1();
- fun1(1);
- fun1(1,2);
- }
复制代码 (9)利用using说明整个命名空间成员可用 可直接通过命名成员利用
- namespace name5
- {
- int d = 10;
- int e = 20;
- int f = 30;
- }
- void test9()
- {
- using namespace name5;
- cout<<"d="<<d<<endl;
- cout<<"e="<<e<<endl;
- cout<<"f="<<f<<endl;
- }
复制代码 三、C++增强
(1)范例增强
1-全局变量检测增强
2-c++的函数形参都必须有范例
3-c++的函数没有形参发起写void
4-c++中枚举不答应赋其他int值
(2)布局体增强
1-c++可以不写关键字struct(c不答应)
- struct stu
- {
- int num;
- char name[32];
- };
- void test10()
- {
- stu lucy = {100,"lala"};//可不写关键字
- cout<<"num="<<lucy.num<<" name="<<lucy.name<<endl;
- }
复制代码 2-答应函数作为布局体成员
(3)const增强
1-修饰变量为只读
- const int a=10;
- a = 100;//错误
复制代码 2-c++的const对变量优化
(1 如果以 常量 初始化const修饰的变量 编译器会将变量的值 放入符号常量表中,不会立即给变量开辟空间
只有当对a取地址时 编译器才会给a开辟空间(只读变量)
- void test13()
- {
- const int a = 10;
- int *p = (int *)&a;
- *p = 100;
- printf("*p=%d\n",*p);
- printf("a=%d\n",a);
- }
复制代码 *p=100;
a=10;
通过指针变量p访问的内容*p去的是空间的值
通过变量a访问的是符号常量表中的值
(2 如果以 变量 初始化const修饰的只读变量,没有符号常量表,立即开辟空间
- void test14()//const初始化变量
- {
- int b = 10;
- const int a = b;
- int *p = (int *)&a;
- *p = 100;
- printf("*p=%d\n",*p);
- printf("a=%d\n",a);
- }
复制代码 (3 如果以 自定义范例 初始化const修饰的只读变量,没有符号常量表,立即开辟空间
- //const初始化自定义类型变量
- struct STU
- {
- int num;
- char name[32];
- };
- #include<string.h>
- void test()
- {
- const STU lucy = {100,"lala"};
- STU *p = (STU *)&lucy;
- p->num = 200;
- strcpy(p->name,"koko");
- cout<<p->num<<" "<<p->name<<endl;
- cout<<lucy.num<<" "<<lucy.name<<endl;
- }
复制代码 (4 只管利用const取代define
const有范例,可举行编译器范例安全检查。#define无范例,不可举行范例检查
const有作用域,而#define不器重作用域,宏不能作为命名空间、布局体、类的成员,而const可以
(4)c++新增bool范例
bool范例拥有两个值 true false
- void test()
- {
- bool bl;
- bl = true;
- if(bl)
- {
- cout<<"真"<<endl;
- }
- else
- {
- cout<<"假"<<endl;
- }
- }
复制代码 (5)c++三目运算符增强
- void test12()
- {
- int a=10;
- int b=20;
- int c;
- c= a>b?a:b=100;//C++可对b赋值
- cout<<c<<endl;
- }
复制代码 a>b?a:b=100;可以对b赋值(c不可以)
(6)左值右值(c++、c共有)
左值:能放在=左边,(能被赋值的值)
右值:只能放在=右边,(不能被赋值的值)
五、总结
如有错误或好的发起接待留言评论
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |