派生类
#include <iostream>
using namespace std;
class Box{
   private://类私有,只有成员可以调用 也就是说你不可以通过box1.a来调用 ,这些变量其实你默认不用写private 这个变量,只要放在最上面他默认就是 私有
      int a=1;
   protected://protected(受保护)成员
      int b=2;
   public:
      int e=3;
};
class Neet:Box{//派生类
   public:
      void c(){
         // cout<< a;//private 在派生类里是      不可以引用的
         // cout << b;//protected 在派生类里是   可以调用的
         cout <<e;   //public 在派生类里也是     可以访问的
      };
};
int main(){
   Neet Neet1;
   Neet1.c();
} 
public 继承
#include <iostream>
using namespace std;
class A{
   private:
      int a=1;
   public:
      int b=2;
   protected:
      int c=3;
};
class B:public A{
   // int d=c; // 在main里cout << b.d;//会报错
   int d=b;    // 3
   // int d=a;    // 直接报错,A里面private 不能集成和派生
   public:
      void g(){
         // cout << a;//报错
         // cout << b;//2
         cout << c;//3
      };
};
int main(){
   B b;
   b.g();
   // cout << b.d;//报错
} 

private 继承
#include <iostream>
using namespace std;
class A{
   private:
      int a=1;
   public:
      int b=2;
   protected:
      int c=3;
};
class B:private A{
   // int d=c; // 在main里cout << b.d;//会报错
   int d=b;    // 3
   // int d=a;    // 直接报错,A里面private 不能集成和派生
   public:
      B(int x){
         cout << x<<" "<<d;
      };
};
int main(){
   B b(10);//会执行 B里面的b的大写方法也就是B()
   // b.g();
   // cout << b.d;//报错
} 
![]()
protected 继承
#include <iostream>
using namespace std;
class A{
   private:
      int a=1;
   public:
      int b=2;
   protected:
      int c=3;
};
class B:protected A{
   int d=c; // 在main里cout << b.d;//会报错
   // int d=b;    // 3
   // int d=a;    // 直接报错,A里面private 不能集成和派生
   public:
      B(int x){
         cout << x<<" "<<d;
      };
};
int main(){
   B b(10);//会执行 B里面的b的大写方法也就是B()
   // b.g();
   // cout << b.d;//报错
} 
![]()
构造函数
简单的说 A里面的A(),就是constructor,A和A()的命名要一样。他会在 A a;的时候被执行。
void A::set(int x){ 则可以对A里面的方法进行编写。
#include <iostream>
#include <windows.h>
using namespace std;
class A{
   int c;
   public:
      A();
      void set(int);
      int get(void);
};
A::A(){
   cout<<"你好"<<endl;
};
void A::set(int x){
   c=x;
}
int A::get(void){
   return c;
}
int main(){
   SetConsoleOutputCP(65001);
   A a;
   a.set(3);
   cout << "c="<<a.get();
} 

带参数的构造函数
#include <iostream>
#include <windows.h>
using namespace std;
class A{
   int c;
   public:
      A(int);
};
A::A(int x){
   c=x;
   cout<<c<<endl;
};
int main(){
   SetConsoleOutputCP(65001);
   A a(20);
} 

使用初始化列表来初始化字段
A::A(int x):c(x),d(x+1){//等价于 c=x; d=x+1其实就是缩写
其实就是把上个案例写在A::A(int x){的 c=x提到上面来而已,没啥特别的,倒是方便 一目了然哪些是classA 里面的。
#include <iostream>
#include <windows.h>
using namespace std;
class A{
   int c,d;
   public:
      A(int);
};
A::A(int x):c(x),d(x+1){//等价于  c=x; d=x+1其实就是缩写
   cout<<"c="<<c<<"\nd="<<d<<endl;
};
int main(){
   SetConsoleOutputCP(65001);
   A a(20);
} 

其实构造函数可以在A class的时候就写了,效果一样,如下
#include <iostream>
#include <windows.h>
using namespace std;
class A{
   int c,d;
   public:
      A(int x):c(x),d(x+1){//等价于  c=x; d=x+1其实就是缩写
         cout<<"c="<<c<<"\nd="<<d<<endl;
      }
};
int main(){
   SetConsoleOutputCP(65001);
   A a(20);
} 
类的解析函数
#include <iostream>
#include <windows.h>
using namespace std;
class A{
   public:
      A(){//构造函数
         cout<<"-----函数开始-----"<<endl;
      };
      ~A(){//解析函数
         cout<<"-----函数结束-----"<<endl;
      };
      void console(){
         cout<<"csole被执行了"<<'\n';
      }
};
int main(){
   SetConsoleOutputCP(65001);
   A a;
   a.console();
} 

类的多继承
记住 A跟B的如果都定义了 int a;那你在c里调用 a会提示定义模糊
#include <iostream>
#include <windows.h>
using namespace std;
class A{//被集成的类叫做基类
   public:
      int a=1;
};
class B{
   public:
      int b=2;
};
class C:A,B{
   public:
      C(){
         cout<<"a="<<a<<endl;
         cout<<"b="<<b;
      }
};
int main(){
   C c;
} 

多继承 定义模糊解决方案
两个基类都定义了a=1,c类继承调用的时候,可以写成 A::a,或者B::a
#include <iostream>
#include <windows.h>
using namespace std;
class A{
   public:
      int a=1;
};
class B{
   public:
      int a=1;
};
class C:A,B{
   public:
      C(){
         cout<<"a="<<B::a<<endl;
      }
};
int main(){
   C c;
} 

auto 存储类
我就记住一个,自动判断变量类型
#include <iostream>
#include <typeinfo>
#include <windows.h>
using namespace std;
int main(){
   auto a=10.52;
   cout<<typeid(a).name();
} 
![]()
typeid是判断变量类型的函数
register 存储类
寄存器只用于需要快速访问的变量,会提变量的访问效率
(在 /std:c++17 模式及更高版本中可用) 博主用的c++17
#include <iostream>
#include <typeinfo>
#include <windows.h>
using namespace std;
int main(){
   //寄存器只用于需要快速访问的变量,比如计数器。还应注意的是,定义 'register' 并不意味着变量将被存储在寄存器中,它意味着变量可能存储在寄存器中,这取决于硬件和实现的限制。
   for(register int i=0;i<100;i++){
      cout << i<<'\n';
   }
} 

static 存储类
static count 10 我只能说,我不理解他的“其他文件”指的啥 因为我#include "b.cpp" 之后 不用extern也可以调用,后面如果我知道了在回来补充这篇博文吧
这篇其实还要扩展一个阅读===》.cpp调用 .cpp和h的demo_雪狼之夜的博客-CSDN博客
#include <iostream>
#include <typeinfo>
#include <windows.h>
using namespace std;
static int count =10;//别的文件不能引用 count,static 限制变脸count 在本文件内
int main(){
   extern void console();
   console();
}
void console(){
   static int x=0;
   if(x<count){
      x++;
      count--;
      cout <<"x="<<x<<",count="<<count<< endl;
      console();
   }
} 

如果函数内的 x不加static 就会变成

extern 存储类
看博主这篇===》c++的extern用法理解_雪狼之夜的博客-CSDN博客



















