ndk-c++

1.输出

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

2.函数符号兼容

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

3.引用

引用是c++ 定义的一种新的数据类型
// 声明形参为引用
void change(int &i){
	i = 10;
}
int i = 1;
change(i);
printf("%d\n",i); // i 等于 10

/*
	引用和指针是两个东西
	引用: 变量名是附加在内存位置中的一个标签,可以设置,可以设置第二个标签
*/

4.命名空间

namespace 相当于 package

namespace A{
	void a(){}
}
A::a()

// 嵌套 定义
namespace  A{
	namespace B{
		void a() {};
	}
}

5.字符串

c字符串
// c 的字符串 为字符数组
char str1[] = {'h','e','l','l','o','\n'};
char str2[] = "Hello";
函数概述
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 类
#include<string>
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()

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.可以被该类中的函数、子类的函数、友元函数访问,也可以被该类的对象访问
*/

7.常量函数

函数后写上 const  表示不会 也不允许修改类中的成员
class Student{
	int i;
public:
	Student(){}
	~Sudent(){}
	// 常量函数
	void setName(char *  _name) const{
		name = _name; // 不能修改 name  去掉 const 之后可以
	}
private:
	int j;
	char *name;
protected:
	int k;
};

8.友元

1.类的友元函数是定义在类外部,但是有权访问 所有私有(private)成员 和保护(protected) 成员
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){
		}
};

9.静态成员

1. 可以用static 来声明类成员为静态
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;
}

10.函数重载

void print(int i){
	cout << "int:" << i << endl;
}
void print(double f){
	cout << "double:" << f << endl;
}

11.操作符重载

1.c++ 允许重定义大部分的内置运算符
2.函数名是由关键字 operator 和 其后要重载的运算符符号构成的
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;

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;
		}
}
多继承:
	class<派生类名>:<继承方式1><>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值