构造 复制构造 重载构造 析构 静态数据成员

#include<bits/stdc++.h>
using namespace std;
 

class Box
{
public:   
	Box();
	Box(Box &p);
	Box(int length,int width,int height);
	~Box();  
	int volume(); 
	void show();
private:
	int length;
	int width;
	int height;
	static int steps;
};

int Box::steps=0;

Box::Box(Box &p)
{
	length=p.length;
	width =p.width;
	height =p.height;	
	steps++;
	cout << "拷贝构造函数被调用" << endl;
}

Box::Box()
{
	length = 10;
	width =10;
	height =10;
	steps++;
	cout << "无参的构造函数被调用:Box()"<<endl; 
 
}
Box::Box(int length,int width,int height):length(length),width(width),height(height)    
{
steps++; 
cout << "含参的构造函数被调用:Box(int length,int width,int height)"<<endl; 
}
 
Box::~Box() 
{
   cout << "析构函数被调用:~Box()"<<endl; 
}
 
int Box::volume()
{
   return (height*width*length);
}

void Box::show()
{
	cout<<steps<<endl;
}
int main()
{
	Box box1;
	cout <<"The volume of box1 is :" << box1.volume()<<endl;
 	box1.show();
 	
	Box box2(15,12,13);
	Box box3(box2);
	cout <<"The volume of box2 is :" << box3.volume()<<endl;
 	box3.show();
 
	return 0;
}

函数指针

#include<bits/stdc++.h>
using namespace std;
 

class Box
{
public:   
	Box();
	Box(Box &p);
	Box(int length,int width,int height);
	~Box();  
	int volume(); 
	void show();
private:
	int length;
	int width;
	int height;
	static int steps;
};

int Box::steps=0;

Box::Box(Box &p)
{
	length=p.length;
	width =p.width;
	height =p.height;	
	steps++;
	cout << "拷贝构造函数被调用" << endl;
}

Box::Box()
{
	length = 10;
	width =10;
	height =10;
	steps++;
	cout << "无参的构造函数被调用:Box()"<<endl; 
 
}
Box::Box(int length,int width,int height):length(length),width(width),height(height)    
{
steps++; 
cout << "含参的构造函数被调用:Box(int length,int width,int height)"<<endl; 
}
 
Box::~Box() 
{
   cout << "析构函数被调用:~Box()"<<endl; 
}
 
int Box::volume()
{
   return (height*width*length);
}

void Box::show()
{
	cout<<steps<<endl;
}
int main()
{
	Box *obj=new Box;
	int (Box::*fun)()=&Box::volume;
	Box box1;
	Box *p=&box1;
	cout <<"The volume of box1 is :" << (p->*fun)()<<endl;
 	box1.show();
 	
	Box box2(15,12,13);
	Box box3(box2);
	cout <<"The volume of box2 is :" << box3.volume()<<endl;
 	box3.show();
 
	return 0;
}
多继承同名隐藏举例

#include<bits/stdc++.h>
using namespace std;

class Base0{
	public:
		int var0;
		void fun0(){
			cout<<"Member of Base0"<<endl;
		}
};

class Base1:public Base0 {
	public:
		int var1;
};

class Base2:public Base0 {
	public:
		int var2;
};

class Derived :public Base1,public Base2{
	public:
		int var;
		void fun(){
			cout<<"Member of Derived"<<endl;
		}
};

int main()
{
	Derived d;
	d.Base1::var0=2;
	d.Base1::fun0();
	d.Base2::var0=3;
	d.Base2::fun0();
	return 0;
}

运算符的重载

#include<bits/stdc++.h>
using namespace std;

class point{
	public:
		point(double x=0.0,double y=0.0):x(x),y(y){}
		point operator+ (const point &c2) const;
		point operator- (const point &c2) const;
		void display() const;
	private:
		double x;
		double y;
};

point point::operator+ (const point &c2) const{
	return point (x+c2.x,y+c2.y);
}

point point::operator- (const point &c2) const{
	return point (x-c2.x,y-c2.y);
}

void point::display() const{
	cout<<"("<<x<<", "<<y<<")"<<endl;
}

int main()
{
	point c1(5,4),c2(2,10),c3;
	cout<<"c1="; c1.display();
	cout<<"c2="; c2.display();
	c3=c1-c2;
	cout<<"c3="; c3.display();
	c3=c1+c2;
	cout<<"c3="; c3.display();
	return 0;
}

函数模板(交换,前一个参数为值传递)

#include<bits/stdc++.h>
using namespace std;
template<class T> 
void SSwap(T x,T &y)
{
	 T t;
	 t=x;
	 x=y;
	 y=t;
	
	 cout<<x<<" "<<y<<endl;
}
	int	main()
		{
			int a=3,b=4;
			double c=7,d=8;
			char e='h',f='i';
			cout<<"befor exchange(交换前):"<<endl;
			cout<<a<<" "<<b<<endl; 
			cout<<c<<" "<<d<<endl;
			cout<<e<<" "<<f<<endl;
			 cout<<"after exchange(交换后):"<<endl;
			SSwap(a,b); 
			SSwap(c,d); 
			SSwap(e,f); 
			return 0;
		}
			 
			


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值