C++笔记 --入门
来源:互联网 发布:容祖儿 借过知乎 编辑:程序博客网 时间:2024/06/03 06:45
系统环境:Mac OS
IDE: XCode
概念:命名空间
关键字:namespace
没有用关键字时遇到的情况:
/* 程序员小德 定义了一个变量a 类型为int ,程序员小林 也定义了一个变量a 类型为BOOL, 因此当程序员小德和程序员小林的代码结合到一起的时候,就会造成一定的混乱 */ int a = 1; // ... other code BOOL a = true;
使用关键字后:
namespace De{ //程序小德的变量声明 int flag = 1;}namespace Lin{ //程序小林的变量声明 bool flag = true;}- (void)viewDidLoad { [super viewDidLoad]; De::flag = 0; Lin::flag = YES;}
使用using声明
// ::操作符 采用using声明 using De::flag; flag = 0; Lin::flag = YES;
使用using声明整个命名空间
namespace De{ //程序小德的变量声明 int flag = 1;}namespace Lin{ //程序小林的变量声明 bool flag = true;}using namespace Lin; // 可以用于声明整个命名空间- (void)viewDidLoad { [super viewDidLoad]; flag = 0; De::flag = YES; NSLog(@"%d",flag);}
hello world
#include<iostream> // 标准的C++头文件using namespace std; // C++标准程序库中的所有标识符都被定义于一个名为std的namespace中。@interface ViewController ()@end@implementation ViewControllernamespace De{ //程序小德的变量声明 int flag = 1;}namespace Lin{ //程序小林的变量声明 bool flag = true;}using namespace Lin; // 可以用于声明整个命名空间- (void)viewDidLoad { [super viewDidLoad]; // ::操作符 采用using声明 flag = 0; De::flag = YES; cout << "hello world!" << endl; /* hello world! cout = c + out << C++标准库的Stream类重载了<<运算符,使之成为流的输出符号 endl 作用是换行 */}
概念:输入输出
常见的有 cin 、cout、cerr
/*需要特别强调的是cin、cout、cerr不是C++中的关键字,其本质是函数调用, 实现采用的是C++的运算符重载。 cout和cerr的输出目的地都是显示器,但不同的是cout是带有缓冲的,而cerr则不带缓冲。 在我们使用cout进行输出时需要紧接着使用“<<”操作符, 使用cin进行输入时需要紧接着使用“>>”操作符, 这两个操作符可以自行分析所处理的数据类型,无需我们像用scanf和printf那样设置输入输出格式化语句。*/ string str ; cout << "hhg input some " << endl; cin >> str; cout << "result is=" << str << endl;
概念:引用(reference)
/* 引用是C++语言相对于C语言的又一个扩充,类似于指针,在声明的时候用&取代了*。 引用可以看做是被引用对象的一个别名 在声明引用时,必须同时对其进行初始化。 引用的声明方法如下: 类型标识符 &引用名 = 被引用对象 */ int a = 1; int &b = a; cout << a << " = " << b << endl; // 打印值 1 = 1 cout << &a << " = " << &b << endl; // 打印地址 0x7fff50df3b8c = 0x7fff50df3b8c b = 30; cout << a << " = " << b << endl; // 打印值 30 = 30
概念 :函数
无返回值函数
void mySwap(int &a, int &b);- (void)viewDidLoad { [super viewDidLoad]; int numA = 233; int numB = 333; cout << numA << " " << numB << endl; // 233 333 mySwap(numA, numB); // 调用方法 cout << numA << " " << numB << endl; // 333 233}void mySwap(int &a , int &b) { int temp = a; a = b; b = temp;}
普通返回值的函数
int addMethod(int &a);- (void)viewDidLoad { [super viewDidLoad]; int numA = 233; cout << numA << endl; // 233 addMethod(numA); cout << numA << endl; // 238}int addMethod(int &a) { a += 5; return a;}
int & addMethod(int &a); // 多了 &- (void)viewDidLoad { [super viewDidLoad]; int numA = 233; cout << numA << endl; // 233 int numB = addMethod(numA); cout << numB << endl; // 238}int & addMethod(int &a) { a += 5; return a;}
概念:强转
关键字: static_cast
// C语言强转 int a = 33; int b = 454; float c = (float)a /(float)b; NSLog(@"%f",c); // 打印结果 0.072687 // C++ 强转 static_cast 形式: static_cast <类型说明符> (变量或表达式) int d = 43; int f = 45; float g = static_cast<float>(d) / static_cast<float>(f); NSLog(@"%f",g); // 打印结果 0.955556
关键字:const_cast
// C++ 强转 const_cast 形式: /* 在C语言中,const限定符通常被用来限定变量,用于表示该变量的值不能被修改。 const_cast则是用于强制去掉这种不能被修改的常数特性, 需注意的是 const_cast 不是用于去除变量的常量性, 是去除指向常数对象的指针或引用的常量性 其去除常量性的对象必须为指针或引用。 */ int a = 10; const int *p = &a; cout << " " << *p << endl; // *p = 10 int *q ; q = const_cast<int*>(p); *q = 20; cout << *q << " " << *p << endl; // *q = 20 *p = 20
概念:内联
关键字:inline
// 通常在程序设计过程中,我们会将一些频繁被调用的短小函数声明为内联函数。void swap(int &a ,int &b); // inline 关键字放函数处不齐作用,应放在函数体一起inline void swap(int &a, int &b) { int temp = a; a = b; b = temp;}
概念:分配、释放内存
关键字:new、new[]、delete、delete[]
/* C 中 动态分配释放内存的函数是malloc calloc和free C++中 动态分配释放内存的是new、new[]、delete、delete[] */ int *p = new int; int *A = new int[10]; delete p; delete []A;
概念:类
关键字:class
class student { // 类的声明 学生类 public: long studentID ; // 学生变量};- (void)viewDidLoad { [super viewDidLoad]; student myLi; // 实例化 myLi.studentID = 234234; cout << myLi.studentID <<endl;}
概念:成员变量,成员函数
class student { // 成员变量 char name[15]; // 姓名 int studentID ; // 学号 int age; // 年龄 char sex; // 性别 // 成员函数 public: void set_age(int a){age = a;}; // 类内部定义函数 int get_age(); // 外部定义函数, 也可以直接在该函数前加inline ,使之成为内联函数,也和类内部定义函数一样。};// 类外部定义函数int student::get_age() { return age;}- (void)viewDidLoad { [super viewDidLoad]; student myLi ; myLi.set_age(33); cout << myLi.get_age() << endl;}
0 0
- Object-c入门笔记
- Objective-C 入门笔记
- C语言入门笔记
- [C++]入门笔记1:WinMain
- [C++]入门笔记2.Objcet
- [C++]入门笔记4.绘图
- [C++]入门笔记5.文本
- [C++]入门笔记6.菜单
- [C++]入门笔记7.对话框
- C: 入门学习笔记(1)
- C: 入门学习笔记(2)
- C基础入门笔记1
- C语言入门笔记 20150522
- C语言入门笔记20150605
- C语言入门笔记20150601
- 《C语言入门》笔记---慕课网
- C++Primer学习笔记一:快速入门
- 1.没有营养的C入门笔记
- 68.二叉树的后序遍历
- 数据结构Java实现04----循环链表、仿真链表
- Linux入门(4)——Ubuntu16.04安装MATLAB2016b
- Java异常的深入研究与分析
- javascript中的json对象应用
- C++笔记 --入门
- linux查看服务器设备信息命令dmidecode
- 批处理 打开指定的文件夹
- iOS
- UINavigation修改
- birthday
- 先验概率、似然函数、后验概率、贝叶斯公式
- linux wc命令学习
- 链表的有序集合