栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【星海出品】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;
}

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

#include 
using namespace std;

void fun(int *pa,int n);

void SunArry(int *pa,int n){
	for(int i=0;i
	int Arry[10]={1,2,3,4,5,6,7,8,9};
	int n=10;
	
	
	SunArry(Arry,n);
	cout << Arry[9] << endl;
}

简单类引用、指针的学习

#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();
}

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

#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();
}

数组含对象,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< 

简单的构造重载应用

#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);
}

类继承的作用域

#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;
}

简单加法类的实现

#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;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1022208.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号