• 【星海出品】C++类的学习(通过代码)


    static类函数的简单应用

    #include 
    using namespace std;
    
    class Count{
    	static int count;
    	public:
    		Count(){
    		    cout << count++;
    		}
    		
    		static int Getc(){
    			return count;
    		}
    		
    		~Count(){
    			count--;
    		};
    };
    
    int Count::count = 10,obj;
    
    int main(){
    	cout << obj << endl;
    	cout << Count::Getc() << endl;
    	return 0;
    }
    
    • 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

    C++指针为形参,数组为实参的简单应用

    #include 
    using namespace std;
    
    void fun(int *pa,int n);
    
    void SunArry(int *pa,int n){
    	for(int i=0;i<n-1;i++)
    		*(pa+9)+=*(pa+i);
    }
    
    int main(){
    	int Arry[10]={1,2,3,4,5,6,7,8,9};
    	int n=10;
    	
    	
    	SunArry(Arry,n);
    	cout << Arry[9] << endl;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    简单类引用、指针的学习

    #include 
    using namespace std;
    
    class Person{
    	public:
    		virtual void disp(){
    			cout << "Person";
    		}
    };
    
    class Address:public Person{
    	public:
    		virtual void disp(){
    			cout << "Address";
    		};
    };
    
    class PhoneNumber:public Person{
    	public:
    		void disp(){
    			cout << "PhoneNumber" << endl;
    		}
    };
    
    int main(void){
    	Person *p;
    	Address a;
    	PhoneNumber pn;
    	p = &a;
    	p->Person::disp();
    	p->disp();
    	
    	p=&pn;	
    	cout<< endl ; p->Person::disp();
    	p->disp();
    }
    
    
    • 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

    运用构造输出开始语、运用析构输出结束语

    #include 
    using namespace std;
    
    static int time=0,end=0;
    
    class Test{
    	public:
    		Test(){
    			if(time==0){
    				cout << "欢迎使用测试小程序" << endl;
    			}
    			time=time+1;	
    		}
    		
    		~Test(){
    			end=end+1;
    			if(end==time)cout << "next say'y " << endl;
    		}
    		
    		void print(){
    			cout << "生成" << time << "个对象" << endl; 
    		}
    };
    
    int main(){
    	Test obj1,obj2,obj3;
    	obj1.print();
    }
    
    
    • 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

    数组含对象,for循环提取数据

    #include 
    using namespace std;
    
    static int time=0,end=0;
    
    class toy{
    	private:
    		int num,price;
    	public:
    		toy(int q,int p){
    			num = q;
    			price = p;
    		}
    		int get_num(){
    			return num;
    		}
    		int get_price(){
    			return price; 
    		} 
    };
    
    int main(){
    	toy op[2][2] = {toy(10,20),toy(30,48),toy(50,68),toy(70,80)};
    	
    	int i;
    	for(i=0;i<2;i++){
    		for(int j=0;j<2;j++){
    			cout<<op[i][j].get_num() << ",";
    			cout<<op[i][j].get_price() << '\n';  
     		}
    	}
    }
    
    
    • 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

    简单的构造重载应用

    #include 
    using namespace std;
    
    class A{
    	int a,b;
    	public:
    		A(){
    			a=b=0;
    		}
    		A(int aa,int bb):a(aa),b(bb){
    			cout << "a=" << a << "in" << "b=" << b << endl; 
    		}
    		~A(){
    			cout << "Destructor!" << endl; 
    		}
    };
    
    int main(){
    	A x,y(2,3);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    类继承的作用域

    #include 
    using namespace std;
    
    class A{
    	public:
    		A(int i){
    			x=i;
    		}
    		
    		void dispa(){
    			cout << "A:" << x << endl;	
    		} 
    	private:
    		int x;
    	};
    
    class B:public A{
    	public:
    		B(int i):A(i+10){x=i;};
    		
    		void dispb(){
    			dispa();
    			cout<<"B:" << x << endl;
    		};
    	private:
    		int x; 
    }; 
    
    int main(){
    	B b(10);
    	b.dispb();
    	return 0;
    }
    
    
    • 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

    简单加法类的实现

    #include 
    using namespace std;
    
    
    class Test{
    	private:
    		int x,y;
    		//int z;
    	public:
    		init(int i,int j){
    			x=i;
    			y=j;
    		};
    
    		print(){
    			cout << x << " - " << y << " = " << x-y+30;
    		};
    };
    
    
    int main(){
    	Test a;
    	a.init(2019,100);
    	a.print();
    	return 0;
    }
    
    • 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
  • 相关阅读:
    计算机毕业设计Java旅游官网(源码+mysql数据库+系统+lw文档)
    第一次工业革命
    Oracle数据库的权限管理(二)
    Linux系统下压缩与解压命令
    基于Java的宠物领养管理系统设计与实现(源码+lw+部署文档+讲解等)
    探讨 volatile 关键字
    2022最新版-李宏毅机器学习深度学习课程-P51 BERT的各种变体
    线性代数:向量、张量、矩阵和标量
    安卓讲课笔记5.11 菜单
    React.js 3D开发快速入门
  • 原文地址:https://blog.csdn.net/weixin_41997073/article/details/125893061