C++笔记 --入门

系统环境: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 ViewController

namespace 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值