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