类的组合
解决复杂问题的有效方法就是将其分解为简单问题的组合
在面向对象程序设计中,可以对复杂对象分解、抽象,把一个复杂对象分解为简单对象的组合,有比较容易理解和实现的部件对象装配而成
对类组合的描述:一个类内嵌其他类的对象作为成员的情况
关系:包含与被包含的关系
例如:线段类
#include<iostream>
#include<cmath>
using namespace std;
class point
{
public:
point(int xx = 0, int yy = 0)
{
x = xx;
y = yy;
}
point(point &p);
int getx()
{
return x;
}
int gety() {
return y;
}
private:
int x;
int y;
};
point::point(point & p)
{
x = p.x;
y = p.y;
cout << "calling the copy constructor of point" << endl;
}
class line
{
public:
line(point xp1, point xp2);
line(line&l);
double getlen() { return len; }
private:
point p1, p2;
double len;
};
line::line(point xp1, point xp2)
:p1(xp1),p2(xp2)
{
cout << "calling constructor of line" << endl;
double x = static_cast<double>(p1.getx() - p2.gety());
doub