C++ 中的函数重载
class PrintData
{
public:
void print(int v)
{
cout << "cout v=" << v << endl;
}
void print(double b)
{
cout << "cout b=" << b << endl;
}
void print(char c[])
{
cout << "cout c" << c << endl;
}
private:
};
int main(void)
{
PrintData pd;
pd.print(8);
pd.print(88.888);
char c[] = "打印字符串";
pd.print(c);
return 0;
}
cout v=8
cout b=88.888
cout c打印字符串
class Box
{
public:
double getVolume(void)
{
return length * breadth * height;
}
void setLength(double l)
{
length = l;
}
void setBreadth(double b)
{
breadth = b;
}
void setHeight(double h)
{
height = h;
}
Box operator+(const Box& b )
{
Box box;
box.length = this->length + b.length;
box.breadth = this->breadth + b.breadth;
box.height = this->height + b.height;
return box;
}
private:
int length;
int breadth;
int height;
};
int main(void)
{
Box Box1;
Box Box2;
Box Box3;
double volume = 0.0;
Box1.setLength(1.0);
Box1.setBreadth(1.0);
Box1.setHeight(1.0);
Box2.setLength(2.0);
Box2.setBreadth(2.0);
Box2.setHeight(2.0);
volume = Box1.getVolume();
cout << "box1体积=" << volume << endl;
volume = Box2.getVolume();
cout << "box2体积=" << volume << endl;
Box3 = Box1 + Box2;
volume = Box3.getVolume();
cout << "box3体积=" << volume << endl;
return 0;
}
box1体积=1
box2体积=8
box3体积=27