类的组合

来源:互联网 发布:一个字很火的网络歌曲 编辑:程序博客网 时间:2024/06/06 05:46

类的组合

要领:
1.类的组合描述的就是一个类内嵌其它类的对象作为成员的情况,它们之间的关系是包含与被包含的关系
2.当创建类的对象时,如果这个类具有内嵌对象成员,那么各个内嵌对象将首先被创建
3.在创建对象时既要对本类的基本类型数据成员进行初始化,又要对内嵌对象成员进行初始化
4.一般定义形式:

类名::类名(形参表):内嵌对象1(形参表),内嵌对象2(形参表)...{类的初始化}

内嵌对象1(形参表),内嵌对象2(形参表)表示的是初始化列表,作用是对内嵌对象成员进行初始化
5.构造组合类调用顺序:调用内嵌对象构造函数→执行本类构造函数的函数体
6.析构函数调用顺序:与构造函数相反
例子:

#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, 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)          //3和4{    cout << "Calling constructor of Line" << endl;    double x = static_cast<double> (p1.getX() - p2.getX());    double y = static_cast<double> (p1.getY() - p2.getY());    len = sqrt(x * x + y * y);}Line::Line(Line &l) : p1(l.p1), p2(l.p2){    cout << "Calling the copy constructor of Line" << endl;    len = l.len;}int main(){    Point myp1(1, 1), myp2(4, 5);//生成两个Point类的对象       Line line(myp1, myp2);//构造Line类的对象line              1和2    Line line2(line);//复制构造函数建立Line类的第二个对象line2   5和6     cout << "The length of the line is: " ;    cout << line.getLen() << endl;    cout << "The length of the line2 is:";    cout << line2.getLen() << endl;    return 0;}

结果:

alling the copy constructor of Point    //1Calling the copy constructor of Point    //2Calling the copy constructor of Point    //3Calling the copy constructor of Point    //4Calling constructor of LineCalling the copy constructor of Point    //5Calling the copy constructor of Point    //6Calling the copy constructor of LineThe length of the line is: 5The length of the line2 is: 5

这个程序在执行时,首先生成两个Point类的对象,然后构造Line类的对象line,接着通过复制构造函数建立Line类的第二个对象line2,最后输出两点的距离。在整个运行过程中,Point类的复制构造函数被调用了6次,分别是两个对象在Line构造函数进行函数参数形实结合时,初始化内嵌对象时,以及复制构造函数line2时被调用。
也就是说,因为
Line line(myp1, myp2);
中对象是函数的参数,所以在函数参数形参结合时需要调用Point类的复制构造函数,即1、2两次,而在Line::Line(Point xp1, Point xp2): p1(xp1), p2(xp2)
中因为要采用类对Line中的内嵌对象进行初始化,即,用xp1对p1进行初始化,用xp2对p2进行初始化。所以在此又调用了两次,即3、4两次。下面的5、6两次自不必多说,就是在构造line2对象时调用的。

前向引用

要领:
1.解决的问题:类A的公有成员函数f的形式参数是类B的对象,同时类B的公有成员函数g也以类A的对象形参,避免引起这样的定义式编译错误
2.定义形式:

Class B;        //前向引用声明class A{        //A的定义pubilc:         //外部接口    void f(B b);//以B类对象b为形参的成员函数};class B{        //B的定义public:         //外部接口    void g(A a);//以A类对象a为形参的成员函数};

3.尽管使用了前向引用的声明,但是在提供一个完整的类定义之前,不能定义该类的对象,也不能在内联成员函数中使用该类的对象
4.使用前向引用声明时,只能使用被声明的符号,不能涉及类的任何细节
5.错误类型:

class fred;class barney{fred x;         //错误:类fred的定义尚不完善};class fred{barney y;};
class fred;class barney{pubilc:    ...    void method(){         x.hanhsu();//错误:fred类对象在定义之前被使用(hanshu()未定义)    }private:fred &x;            //正确:前向引用后,可以声明fred类的对象引用或指针}class fred{pubilc:    ...    void hashu();private:    barney &y;}
原创粉丝点击