#include <jni.h>
#include <string>
#include <iostream>
#include <ctime>
#include <fstream>
#include <android/log.h>
#include <vector>
#include <algorithm>
#define TAG "fct"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__);
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__);
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, TAG, __VA_ARGS__);
typedef int i;
using namespace std;
void 给变量起别名();
void 参数传递();
void swap_1(int a, int b);
void swap_2(int *a, int *b);
void swap_3(int &a, int &b);
int &函数的调用可以作为左值();
int &函数的调用可以作为左值();
void 修饰形参_防止误操作(int &a);
extern "C" JNIEXPORT jstring JNICALL
Java_com_android_jnitest_MainActivity_stringFromJNI(
JNIEnv *env,
jobject ) {
string hello = "Hello from C++";
std::cout << "asdasd" << endl;
return env->NewStringUTF(hello.c_str());
}
extern "C" JNIEXPORT jint JNICALL
Java_com_android_jnitest_MainActivity_switchNum(
JNIEnv *env,
jobject ) {
string hello = "Hello from C++";
cout << "asdasd" << endl;
return 1;
}
void 给变量起别名() {
int a = 10;
int &b = a;
b = 20;
LOGD("a=%d", a)
LOGD("b=%d", b)
}
void 参数传递() {
int a = 10;
int b = 20;
swap_1(a, b);
swap_2(&a, &b);
swap_3(a, b);
}
void swap_1(int a, int b) {
int temp = a;
a = b;
b = temp;
}
void swap_2(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void swap_3(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}
void 引用做函数的返回值() {
int &ref = 函数的调用可以作为左值();
函数的调用可以作为左值() = 100;
LOGD("ref=%d", ref);
}
int &函数的调用可以作为左值() {
static int a = 10;
return a;
}
void 修饰形参_防止误操作(const int &a);
void 常量引用() {
const int &ref = 10;
int a = 100;
const int &b = a;
修饰形参_防止误操作(b);
}
void 修饰形参_防止误操作(const int &a) {
}
int 函数的默认参数(int a, int b = 20, i c = 30) {
return a + b + c;
}
int 如果函数的声明有默认函数_函数实现就不能有默认参数(int a = 10, int b = 20);
int 如果函数的声明有默认函数_函数实现就不能有默认参数(int a, int b) {
return a + b;
}
int 函数占位参数(int a, int) {
return 1;
}
void 函数重载() {}
void 函数重载(int a) {}
void 函数重载(string a) {}
void 函数重载(int a, string b) {}
void 函数重载(string b, int a) {}
class Car {
public:
int price;
string mark;
void print() {
LOGD("价格——品牌:%d%s", price, mark.c_str());
}
};
void useCar() {
Car car;
car.price = 1800000;
car.mark = "大众";
car.print();
}
class A1 {
string name;
public:
string getName() {
return name;
}
void setName(string name) {
this->name = name;
}
};
struct A2 {
int name;
};
void struct和class的区别() {
A1 a1;
a1.setName("fct");
A2 a2;
a2.name = 100;
A2 *a = new A2();
a->name = 100;
}
class Car2 {
public:
Car2() {
price = 1;
name = "pass";
}
~Car2() {
price = 0;
name = "";
}
int price;
string name;
};
void 构造函数和析构函数() {
Car2 car2;
}
class Car3 {
public:
Car3() {
price = 1;
name = "pass";
}
Car3(int p) {
price = p;
name = "pass";
}
Car3(const Car3 &car) {
price = car.price;
}
~Car3() {
price = 0;
name = "";
}
int price;
string name;
};
void 构造和析构函数的分类以及调用() {
Car3 car3(10);
Car3 car33(car3);
Car3 car31 = Car3(10);
Car3 car32 = 10;
}
class Person {
public:
Person() {
LOGD("%s", "构造函数")
}
Person(int age) {
this->age = age;
LOGD("%s", "有参构造函数")
}
Person(const Person &person) {
age = person.age;
LOGD("%s", "拷贝构造函数")
}
~Person() {
LOGD("%s", "析构函数")
}
int age;
};
void 值传递的方式给函数套数传值(Person p);
Person 值传递的方式给函数套数传值();
void 拷贝构造函数调用时机() {
Person p1(10);
Person p2(p1);
值传递的方式给函数套数传值(p2);
Person p3 = 值传递的方式给函数套数传值();
}
void 值传递的方式给函数套数传值(Person p) {
}
Person 值传递的方式给函数套数传值() {
Person p;
return p;
}
class Animal {
public:
Animal() {
}
Animal(int age, int size) {
this->age = age;
this->size = new int(size);
}
Animal(const Animal &animal) {
age = animal.age;
size = new int(*animal.size);
}
~Animal() {
if (size != NULL) {
delete size;
}
}
public:
int age;
int *size;
};
class Food {
public:
Food() : a(10), b(20), c(30) {
}
Food(int _a, int _b, int _c) : a(_a), b(_b), c(_c) {
}
public:
int a;
int b;
int c;
};
class Bed {
public:
Bed(int size) : size(size) {}
public:
int size;
};
class Room {
public:
Room(float area, int size) : area(area), bed(size) {}
public:
float area;
Bed bed;
};
class Fruit {
public:
static int kind;
string name;
static void getFruit() {
kind = 2;
}
};
int Fruit::kind = 0;
void 访问静态成员变量() {
Fruit fruit;
fruit.kind = 1;
Fruit::kind = 2;
};
class Cup {
public:
Cup() {}
Cup(int type) {
this->type = type;
}
Cup factory() {
return *this;
}
public:
int type;
};
class NBA {
public:
NBA() {}
void showNBA() {
LOGD("%s", "我是NBA");
}
void showTeam() {
}
string name;
};
void 空指针访问成员函数() {
NBA *nba = NULL;
nba->showNBA();
nba->showTeam();
}
class Apple {
public:
Apple() {}
public:
int price;
mutable int size;
void setPrice() const {
this->size = 2;
}
void show() {
}
};
void 常对象() {
const Apple apple;
apple.size = 1;
apple.setPrice();
};
class Orange {
friend void 全局函数做友元();
public:
string name;
private:
int price;
};
void 全局函数做友元() {
Orange orange;
orange.name = "橘子";
orange.price = 1;
}
class Employee;
class Boss {
public:
Boss();
void askSalary();
public:
Employee *employee;
};
class Boss2 {
public:
Boss2();
void 成员函数做友元();
public:
Employee *employee;
};
class Employee {
friend class Boss;
friend void Boss2::成员函数做友元();
public:
Employee();
public:
string name;
private:
int salary;
};
Employee::Employee() {
name = "fct";
salary = 1800;
}
Boss::Boss() {
employee = new Employee;
};
void Boss::askSalary() {
employee->name = "asd";
employee->salary = 1900;
}
Boss2::Boss2() {
employee = new Employee;
}
void Boss2::成员函数做友元() {
employee->name = "asd";
employee->salary = 1900;
}
void 类做友元() {
Boss boss;
boss.askSalary();
}
class Rice {
public:
int a;
};
Rice operator+(Rice &rice1, Rice &rice2) {
Rice temp;
temp.a = rice1.a + rice2.a;
return temp;
}
Rice operator+(Rice &rice1, int a) {
Rice temp;
temp.a = rice1.a + a;
return temp;
}
void 运算符重载() {
Rice rice1;
Rice rice2;
Rice rice = rice1 + rice2;
Rice rice_ = rice1 + 10;
}
class AsiaAnima {
public:
string type;
string name;
};
class Cat : public AsiaAnima {
};
void setCatName() {
Cat cat;
cat.name = "aa";
};
class Base {
public:
Base() {
m_A = 100;
}
public:
int m_A;
static string m_B;
public:
void func() {
}
static void func2() {}
};
class Son : public Base {
public:
Son() {
m_A = 100;
}
public:
int m_A;
static string m_B;
public:
void func() {
}
static void func2() {}
};
void 父类和子类出现同名函数和同名属性如何调用() {
Son son;
son.m_A = 1000;
son.Base::m_A = 1000;
son.func();
son.Base::func();
son.m_B = "1";
son.Base::m_B = "1";
Son::m_B = "1";
Base::m_B = "1";
Son::Base::m_B = "1";
son.func2();
son.Base::func2();
Son::func2();
Base::func2();
Son::Base::func2();
}
class Base1 {
};
class Base2 {
};
class SonMul : public Base1, public Base2 {
};
class Animal_ {
public:
int m_Age;
};
class Yang : virtual public Animal {
};
class Tuo : virtual public Animal {
};
class YangTuo : public Yang, public Tuo {
};
void 菱形继承() {
YangTuo yangTuo;
yangTuo.Yang::age = 18;
yangTuo.Tuo::age = 20;
yangTuo.age = 18;
yangTuo.Yang::age = 18;
yangTuo.Tuo::age = 20;
}
class DongWu {
public:
virtual void speak() {
cout << "动物说话" << endl;
}
};
class Mao : public DongWu {
public:
void speak() {
cout << "猫说话" << endl;
}
};
void doSpeak(DongWu *dongWu) {
dongWu->speak();
}
void 静态多态_动态多态() {
Mao mao;
doSpeak(&mao);
}
class Season {
public:
virtual void name() = 0;
};
class Spring : public Season {
public:
void name() {
cout << "春天" << endl;
}
};
auto namea(10);
decltype(10 + 1) be;
void 纯虚函数和抽象类() {
int a = namea + be;
LOGD("%d", sizeof(a));
Spring *spring = new Spring;
spring->name();
}
void dealfile() {
}
template<typename T>
void swapNum(T &t1, T &t2) {
T temp = t1;
t1 = t2;
t2 = temp;
}
void 模板泛型() {
int a = 1;
int b = 2;
swapNum(a, b);
swapNum<int>(a, b);
}
template<class T>
void 模板需要注意第二点() {
cout << "aaaa" << endl;
}
void 要不是自动推断或者要不就是指定好() {
模板需要注意第二点<int>();
}
template<class T>
T add(T a, T b) {
return a + b;
}
int normalAdd(int a, int b) {
return a + b;
}
void 普通函数和函数模板的区别() {
int a = 1;
char b = 'b';
normalAdd(a, b);
add<int>(a, b);
}
template<class NameType, class AgeType>
class Children {
public:
Children(NameType name, AgeType age) {
this->name = name;
this->age = age;
}
NameType name;
AgeType age;
};
void 类模板() {
Children<string, int> children("fct", 1);
}
template<class NameType, class AgeType>
class chongwu {
public:
chongwu(NameType name, AgeType age) {
this->name = name;
this->age = age;
}
NameType name;
AgeType age;
};
template<class NameType, class AgeType=int>
class chongwu2 {
public:
chongwu2(NameType name, AgeType age) {
this->name = name;
this->age = age;
}
NameType name;
AgeType age;
};
void 类模板与函数模板区别() {
chongwu2<string> chongwu2("dog", 2);
}
template<class T>
class Boli {
public:
Boli(T t) {
this->size = t;
}
void showSize() {
cout << size << endl;
}
T size;
};
void 指定传入的类型(Boli<int> &boli) {
boli.showSize();
}
template<class T>
void 参数模板化(Boli<T> &boli) {
cout << typeid(T).name() << endl;
boli.showSize();
}
template<class T>
void 整个类模板化(T &boli) {
cout << typeid(T).name() << endl;
boli.showSize();
}
void 类模板作为对象传入函数的方式() {
Boli<int> boli(2);
指定传入的类型(boli);
参数模板化(boli);
整个类模板化(boli);
}
template<class T>
class Roomhouse {
public:
T name;
};
class BedRoom : public Roomhouse<string> {
};
template<class T1, class T2>
class BathRoom : public Roomhouse<T2> {
public:
T1 size;
};
void 类模板继承() {
BedRoom bedRoom;
BathRoom<int, string> bathRoom;
}
template<class T>
class Cloth {
public:
Cloth(T mark);
void showMark();
T mark;
};
template<class T>
Cloth<T>::Cloth(T mark) {
this->mark = mark;
}
template<class T>
void Cloth<T>::showMark() {
}
void printVector(int val) {
cout << val << endl;
}
void vector容器使用() {
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
vector<int>::iterator itBegin = v.begin();
vector<int>::iterator itEnd = v.end();
while (itBegin != itEnd) {
cout << *itBegin << endl;
itBegin++;
}
for (vector<int>::iterator itBegin = v.begin(); itBegin != v.end(); itBegin++) {
cout << *itBegin << endl;
}
for_each(v.begin(), v.end(), printVector);
}
class Water {
public:
Water(string name, int age) {
this->name = name;
this->age = age;
}
string name;
int age;
};
void 存入自定义类型数据() {
vector <Water> v;
v.push_back(Water("a", 10));
v.push_back(Water("b", 21));
v.push_back(Water("c", 33));
for (vector<Water>::iterator it = v.begin(); it != v.end(); it++) {
cout << (*it).name << endl;
cout << it->name << endl;
}
vector <Water*> vv;
Water w1("a", 10);
Water w2("b", 21);
Water w3("c", 33);
vv.push_back(&w1);
vv.push_back(&w2);
vv.push_back(&w3);
for (vector<Water*>::iterator it = vv.begin(); it != vv.end(); it++) {
cout << (*it)->name << endl;
}
}