c++入门学习笔记

预处理-》编译-》链接-》运行
#include 
//内置的头文件
// 预处理  编译 链接  运行
//预处理就是把头文件复制到cpp内 
// pragma once 这个头文件如果被重复包含 只会被复制一次
// define Koohai int  定义一个变量 
// ifdef  如果有定义 
// if  

预处理之后会形成一个 .i的文件   编译后会有个.obj的结尾
头文件的好处是 可以预编译 ,避免重复编译系统类库等。


 汇编语言 .asm结尾   汇编器把asm 转变为obj
 如果想两个cpp互相调用,而不用头文件, 就可以使用声明
  通过头文件声明,只会引用对应的cpp 
 
基本语法
int koo={10}; 
更加严格   写成小数会报错 
debug模式下声明的变量没有赋值  他的地址就就是cccc  烫烫烫
release模式下 会随机给一些数
const 只可读
int* koohai=(int*)&koo;
*koohai=999;
unsigned int 无符号 没有负数  

数据类型
char  1个字节  0-255
short  2个字节 
unisgined  char age;
int  4字节 
longlong 8 byte
float  4byte  float  a{3.44344f} 不加f 是double
doublt aa{3.452542452} 
printf("float:%.10f\n"
bool 

转换
int main() {
    int a{1};
    unsigned int b{2};
    std::cout << (a-b)<(a-b); c++写法
a=(char)(a-b) c写法

字符串和宽字符
    char ca{'a'};
    cout << ca << endl;
    cout << (int)ca << endl;
    cout << (char)97 << endl;
    cout << (char)('a' + 1) << endl;
    cout << (char)('a' + 2) << endl;

    return 0;
万国码  wchar_t wws{L'答'}
字符组成字符串 数组或者指针 
 数组    char wlist[]="hellsdfae";
    std::cout << wlist << std::endl;
    std::cout <
auto 关键词
    auto b{"bb"};
    cout<
string
用string 标准库
char a="koohai";
string aa="koohai1 ";
十六个字节内会直接显示 大于16个是分配指针 
auto str2=aa.c_str(); 转为c风格的字符串

字符串替换
    string aa="koohai1koohai ";
    while (true){
        auto pos=aa.find("k");
        if (pos==string::npos){
            break;
        }
        aa.replace(pos,1,"*");
    }
    
std::cout >>age;

//c++的方法 
    string first_name = "John";
    string sec = "snow";
    string res=first_name+sec;
    cout << res << endl;
// c的方法
    char a[]="hjtll0";
    char b[]="woorld";
    char* c=new char[strlen(a)+strlen(b)];
    strcpy(c,a);
    strcat(c,b);
    cout<
枚举
enum LogLevel{
    INFO,
    DEBUG,
    ERROR
};

using namespace std;

void Log(string str,int level){

    switch(level){
        case INFO:
            cout << "[INFO]: " << str << endl;
            break;
        case DEBUG:
            cout << "[DEBUG]: " << str << endl;
            break;
        case ERROR:
            cout << "[ERROR]: " << str << endl;
            break;
    }
}

int main() {
    Log("Hello World",INFO);
    Log("Hello err",ERROR);
    Log("Hello debug",DEBUG);
}
 
 默认是0开始 
 
定义类的别名
d#include 
#include 
#include 
#define myself unsigned int
typedef char mychar;
using myself3=unsigned int;

int main() {
    myself aa   = 10;
    mychar b = 'a';
    myself3 c = 100;

    std::cout <
命名空间
  
namespace My{
    string name = "My name is My";
}
string name= "My name is Out";
int main() {
    cout << My::name << endl;
    cout << name << endl;
    return 0;  
}



结构体
struct Pos{
    int x=0;
    int y=3;
   string name="33";
};

int main() {
   Pos p=Pos(5,5,"44");
  cout << p.x << endl;
  cout << p.y << endl;
}
内存中是连续的
不够4个字节 就填充cc

array
   string urls[]{"google", "http://www.yahoo.comhoo.com", "http://www.facebook.com"};
   cout << sizeof(string)<< endl;
   cout < urls{"google", "http://www.yahoo.comhoo.com", "http://www.facebook.com"};
    cout <<     urls.size() << endl;
    cout <<     urls.at(1) << endl;
   for(auto tmp:urls) {
      cout << tmp << endl;
   }
   
vcector
动态大小 
   vector v;
   vector v{3,"Hello"};
//   for(int i = 0; i < 10; i++) {
//      v.push_back("Hello");
//   };
v[0]="feafae";
    v.insert(v.begin()+1,  "Hellvvvo");
   for (auto i: v){
       cout << i << endl;
   }
    cout << v.size() << endl;


    只能放单一类型 ,如果想既放str 又放int 用结构体
    

指针
void test(int* a){
    *a=*a+1;
};

int main() {
   int a=1;
   int* ap=&a;
    test(ap);
    cout<
引用
void test(int& a){
    a+=1;
};

int main() {
   int a=1;
   int& ac=a;
   a=3030;
   cout<
智能指针
unique_ptr不能复制 可以指向  堆内存 不用手动删除
shared_ptr 共享 

函数指针

using prt = int(*)(int,int); //类型一样就可以
int Add(int a,int b){
    return a+b;
}

int Sub(int a,int b){
    return a-b;
}
int test(int a,int b,prt p){
    return p(a,b);
}

int main(int argcount,char* args[]) {
    cout<
函数重载
阅读方便  参数不同 返回值不同
int Add(int a,int b){
    return a+b;
}

int Add(int a){
    return a;
}

int main(int argcount,char* args[]) {
    cout<
函数模板
template
T Add(T a,T b){
    return a+b;
}
//template
//T Add(T a,M b){
//    return a+b;
//}
//模板重载  强制类型转化     cout<(1.1,3.2)<(1,3)<(1.1,3.2)<
decltype
//auto 无法推测const 的类型

static

static 修饰的函数 只可以当前文件用  无法被引用文件使用
inline 也可以处理重复定义的问题

和c混合 extern
extern "C" int Add(int a,int b) {
    return a+b;
}
//c++里写一个c函数


clion打包dll库
http://www.chn520.cn/article_detail/1688736838056108
类成员函数
class Course{
public:
    const char* anme;
    int count;
    void repr();
//    void repr(Course& course){
//        cout << course.anme <<"数量"<anme <<"数量"<count<< endl;
}

int main() {
    Course couse;
couse.anme = "语文";
//couse.count = 10;
cout<anme = "数学";
    cour1->count = 20;
    cour1->repr();

   return 0;
}


const
class Course{
public:
    const char* anme;
    mutable int count;
    void repr();
    int get_count() const{
        count=3;
        cout<< "get_count" << count<< endl;
        return count;
    }
//    void repr(Course& course){
//        cout << course.anme <<"数量"<anme <<"数量"<count<< endl;
}

void test(Course* couse){
    cout << couse->anme << endl;
    couse->count = 10;
}
//指针的操作就是点换成箭头
int main() {
    Course couse;
couse.anme = "语文";
couse.count = 10;
couse.get_count();

   return 0;
}


构造函数

class Course{
public:
    char* name;
    int count;
    Course(int a ,char* n){
        this->count = a;
    this->name = n;
    };
    Course(int a):count(a){};  //简洁写法 

    Course()=default;
};
//作用域 另一种写法

//指针的操作就是点换成箭头
int main() {
    Course c(33,"saf");
    cout<< c.count << endl;
    Course c3e(332);
    cout<< c3e.count << endl;
    Course c33();
   return 0;
}


析构函数
  
class Course{
int* arr1;
int* arr2;
Course(){
//    arr1=200;
    arr1= new int(100);
    arr2 = new int[10];

};

~Course(){
    //析构函数 类似python的————del__
    delete arr1;
    delete[] arr2;
};

};
//作用域 另一种写法

静态成员变量
class Course{
public:
    static int name;
    int test;


};

int Course::name = 0;
//static 单独存放
int main() {
    Course c1,c2,c3;
    cout<
静态函数
class Course{
public:
    static int name;
    void test(){
        cout << "test" << endl;
    }
    static void test2(){
            cout << "test2" <

友元

class  Teac{
    string name;
    void getName(){
        cout << "name is " << name << endl;
    }
    //getTeacher
    friend class Course;


};
};
class Course{
private:
    int age=1;
    void getAge(){
        cout << "age is " << age << endl;
    }
    void getTeacher(Teac& t){
        t.name="teacher";
        t.getName();
    }
    friend void test(Course& c);
    friend class Teac;
};

void test(Course& c){
    c.age=333;
    c.getAge();
    Teac t;
    c.getTeacher(t);
}

//友元打破类封装 访问private 不属于类成员函数
int main() {
    Course c1;
    test(c1);
   return 0;
}

//Course& c 传递改变参数要通过地址 ,否则不影响原数据

嵌套类
class Course{
public:
    const static int num=1;
    void getNum(){
        cout << num << endl;
    }
    class Teacher{
    public:
        int age=1;
        void getAge(){
            cout << num << endl;
            cout << age << endl;
        };
    Teacher* getTeacher();
    };

};

Course::Teacher* Course::Teacher::getTeacher (){
    return this;
};
int main() {
    Course::Teacher t1;
    t1.getAge();
   return 0;
}
运算符重载
重写大于 让两个类的属性来比较
类似__gt__ __lt__

class Human{
public:
    int age;

    bool operator>(Human &h){
        return this->age > h.age;
    }


    Human(int age):age(age){}
};
int main() {
    Human t1(33);
    Human t2(20);
    cout << (t1 > t2) << endl;
        cout << (t1.operator>(t2)) << endl;
   return 0;
}

=========
#include 
#include 
#include 
#include 
#include 
#include 
#include "library.h"
//typedef int(*prt)(int,int);
using namespace std;

class Human{
    friend bool operator>(Human& h1,Human& h2);
public:
    int age;

    Human(int age):age(age){}
};

bool operator>(Human& h1,Human& h2){
    return h1.age > h2.age;
}
int main() {
    Human t1(33);
    Human t2(20);
    cout << (t1>t2) << endl;
   return 0;
}


继承

class Human{
    friend bool operator>(Human& h1,Human& h2);

public:
    const char* name="Koohai";
    void getName(){
        cout << name << endl;
    }


};
class H1 : public Human{

};
//private wufa jicheng
//protected 继承后可以访问,但是实例化后不可以访问

int main() {
    H1 t1;
    t1.name="feffe";
    H1 t2;
    t1.getName();
   return 0;
}


匿名
int main() {
    auto func=[]{
        cout<<"Hello World"<

更多内容请移步公众号一起学习,同篇笔记可能会有更新喔

c++入门学习笔记_第1张图片

你可能感兴趣的:(零基础爬虫第一天,c++,学习,笔记)