• ndk-c++


    1.输出

    c 使用printf 向终端输出信息
    c++ 提供标准输出流
    
    • 1
    • 2
    #include
    using namespace  std;
    int main(){
    	cout << "hello c++" << endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2.函数符号兼容

    如果需要在c++中调用实现的库中的方法
    extern  "C" // 指示编译器这部分代码使用C的方式编译而不是C++
    例如: void func(int x,int y);
    	c 编译器将函数名编译为 func ,则c++ 编译器 编译成funcii 之类的名称 原因在于c 没有 函数重载,而c++有
    
    • 1
    • 2
    • 3
    • 4
    // gcc 编译
    gcc main.c -o mainc.o
    gcc main.cpp  -o maincpp.o
    // 查看编译结果
    nm -A mainc.o
    nm -A maincpp.o
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    因此 对于C 库函数可以
    
    • 1
    #ifndef __cplusplus
    extern "C"{
    #endif
    void func(int  x,int y)
    #ifdef __cplusplus
    }
    #endif
    // __cplusplus 是由c++编译器定义的宏,用于表示当前处于c++ 环境
    // extern 关键字 可用于变量或者函数之前,表示真实定义在其他文件,编译器遇到关键字就会去其他模块查找
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    3.引用

    引用是c++ 定义的一种新的数据类型
    
    • 1
    // 声明形参为引用
    void change(int &i){
    	i = 10;
    }
    int i = 1;
    change(i);
    printf("%d\n",i); // i 等于 10
    
    /*
    	引用和指针是两个东西
    	引用: 变量名是附加在内存位置中的一个标签,可以设置,可以设置第二个标签
    */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    4.命名空间

    namespace 相当于 package

    namespace A{
    	void a(){}
    }
    A::a()
    
    // 嵌套 定义
    namespace  A{
    	namespace B{
    		void a() {};
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    5.字符串

    c字符串
    
    • 1
    // c 的字符串 为字符数组
    char str1[] = {'h','e','l','l','o','\n'};
    char str2[] = "Hello";
    
    • 1
    • 2
    • 3
    函数概述
    strcpy(s1,s2)f复制字符串s2到字符串s1
    strcat(s1,s2)连接字符串s2到字符串s1的末尾
    strlen(s1)返回字符串s1的长度
    strcmp(s1,s2)如果s1和s2相同,返回0; s1 < s2 返回小于0,s1 >s2 返回大于0
    strchr(s1,ch)返回指向s1 中ch第一次出现的位置指针
    strstr(s1,s2)返回指向字符串s1中字符串s2的第一次出现的指针位置
    c++string 类
    
    • 1
    #include
    using namespace std;
    string str1 = "hello";
    string str2 = "world";
    string str3("hello");
    string str4(str3);
    
    //string 拼接
    string  str5 = str1 + str2;
    // 追加
    str1.append(str2);
    // 获取c 风格的字符串
    const char *s1 =  str1.c_str();
    //字符串长度
    str1.size();
    // 长度是否为0
    str1.empty()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    6.类

    class Student{
    	int i; // 默认 private
    public:
    	Student(int  i,int j,int k):i(i),j(j),k(k){}; // 构造方法
    	~Student(){}; // 析构函数
    private:
    	int j;
    protected:
    	int k;
    };
    /*
    	1.private: 可以被该类中的函数、友元函数访问.
    	2.protected:可以被该类中的函数、子类函数、友元函数访问.
    	3.可以被该类中的函数、子类的函数、友元函数访问,也可以被该类的对象访问
    */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    7.常量函数

    函数后写上 const  表示不会 也不允许修改类中的成员
    
    • 1
    class Student{
    	int i;
    public:
    	Student(){}
    	~Sudent(){}
    	// 常量函数
    	void setName(char *  _name) const{
    		name = _name; // 不能修改 name  去掉 const 之后可以
    	}
    private:
    	int j;
    	char *name;
    protected:
    	int k;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    8.友元

    1.类的友元函数是定义在类外部,但是有权访问 所有私有(private)成员 和保护(protected) 成员
    2.友元可以是一个函数,该函数被称为友元函数;友元也可以是一个类,该类被称为友元类,这种情况下整个类及其所有成员都是友元.
    
    • 1
    • 2
    // 友元函数
    class Student{
    	int i;
    public:
    	Student(){}
    	~Student(){}
    	void  setName(char * _name){
    		name = _name;
    	}
    	// 友元函数
    	friend  void printName(Student *student);
    private:
    	int j;
    	char *name;
    protected:
    	int k;
    };
    // 定义友元函数
    void printName(Student *student){
    	cout << student ->name  << endl;
    }
    Student *student = new Student;
    student ->setName("Tom");
    printName(student);
    
    // 友元类
    class Student{
    	public:
    		Studen(){}
    		~Student(){}
    		// 友元类
    		friend class Teacher;
    	private:
    		int  j;
    		char *name;
    };
    // 友元类定义
    class Teacher{
    	public:
    		void call(Student *student){
    		}
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    9.静态成员

    1. 可以用static 来声明类成员为静态
    2. 使用静态成员属性或者函数 需要使用 域运算符::
    
    • 1
    • 2
    // Instance.h
    #ifdef INSTANCE_H
    #define INSTANCE_H
    class Instance{
    	public:
    		static Instance *getInstance();
    	private:
    		static Instance *instance;
    };
    #endif
    
    // Instance.cpp
    #include "Instance.h"
    Instance *Instance::instance = 0;
    Instance* Instance::getInstance(){
    	if(!instance){
    		instance = new Instance;
    	}
    	return instance;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    10.函数重载

    void print(int i){
    	cout << "int:" << i << endl;
    }
    void print(double f){
    	cout << "double:" << f << endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    11.操作符重载

    1.c++ 允许重定义大部分的内置运算符
    2.函数名是由关键字 operator 和 其后要重载的运算符符号构成的
    3.重载运算可被定义为普通的非成员函数或者被定义为类成员函数
    
    • 1
    • 2
    • 3
    // 成员函数
    class Test1{
    	public:
    		Test1(){}
    		// 重载+
    		Test1 operator+(const Test& t1){
    			Test1 t;
    			t.i = this ->i + t1.i;
    			return  t;
    		}
    		// 拷贝函数
    		Test1(const Test1& t){
    			this ->i = t.i;
    			cout << "copy" << endl;
    		}
    		int i;		
    };
    Test1 t1;
    Test1 t2;
    t1.i = 100;
    t2.i  = 200;
    Test1 t3 = t1 + t2;
    cout << t3.i << endl;
    
    // 非成员函数 重载
    class Test2{
    	public:
    		int i;
    };
    Test2 operator+(const Test2& t21,const Test2& t22){
    	Test2 t;
    	t.i = t21.i + t22.i;
    	return t;
    }
    Test2 t21;
    Test2 t22;
    t21.i = 100;
    t22.i = 200;
    Test2 t23 = t21 + t22;
    cout << t23.i << endl;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    12.继承

    方式说明
    public基类的public、protected成员也是派生类相应的成员,基类的private成员不能直接被派生类访问,但是可以通过调用基类的公有和保护成员来访问
    protected基类的公有和保护成员将成为派生类的保护成员
    private基类的公有和保护成员将成为派生类的私有成员
    class Parent{
    	public:
    		void test(){
    			cout << "parent" << endl;
    		}
    };
    class Child: Parnt{
    	public:
    		void test(){
    			Parent::test();
    			cout  << "child" << endl;
    		}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    多继承:
    	class<派生类名>:<继承方式1><>
    
    • 1
    • 2
  • 相关阅读:
    MES系统中的派工单及其关键应用
    TikTok shop美国小店适合哪些人做?附常见运营问题解答
    【GEE】​3、 栅格遥感影像波段特征及渲染可视化
    webrtc nack
    免费享受企业级安全:雷池社区版WAF,高效专业的Web安全的方案
    Windows证书过期【解决方法】
    ubuntu系统下mysql重置密码和修改密码操作
    3.1.2 内存池的实现与场景分析
    Python 操作MySql数据库(封装、优雅)
    IPWorks WebSockets Delphi版
  • 原文地址:https://blog.csdn.net/weixin_45054115/article/details/120098897