任何人都可以帮助功能重载?

时间:2011-05-04 21:56:01

标签: c++ overloading

我被要求做功能重载,我真的很困惑。我需要:

  1. 不接受任何参数并将所有坐标设置为0。

  2. 接受所有坐标作为参数并将数据成员设置为参数。

  3. 接受与参数同时存在的现有对象,并将此对象的数据成员复制到当前对象中。

  4. 接受维度较小的点的对象,并仅复制那些能够在当前对象设置中将未表示的维度表示为0的数据成员。

  5. 这是我的代码到目前为止,我想知道我是否已经完成了上述任何一项,如果没有,可以有人指导我如何做其他人。谢谢

    #include <iostream>
    #include <string>
    #include <math.h>
    
    using namespace std;
    //////////////////////////////
    ////// Class definition //////
    //////////////////////////////
    
    class point1DClass
    {
    private:
        int x;
    
    public:
        point1DClass(); // constructor function
        int getx(); //Accessor function
        void setx(int newx); // Mutator function
        ~point1DClass();    //Destructor function
    };
    /////////////////////////////////////
    //// Member function implementation//
    /////////////////////////////////////
    point1DClass::point1DClass()
    {
        x=0;
    }
    
    void point1DClass::setx(int newx)
    {
        x = newx;
    }
    
    int point1DClass::getx()
    {
        return x;
    }
    
    point1DClass::~point1DClass()
    {
        cout << "Object Going Out of Scope!" << endl;
    }
    
    class point2DClass:public point1DClass
    {
    private:
        int y;
    
    public:
        point2DClass(); // constructor
    
        void sety(int newy); // Mutator function
        int gety(); //Accessor function
    
        ~point2DClass();
    
    //isincident
    //cityblock
    //pythagDistance
    
    };
    /////////////////////////////////////
    //// Member function implementation///
    /////////////////////////////////////
    point2DClass::point2DClass()
    {
        y=0;
    }
    
    void point2DClass::sety(int newy)
    {
        y = newy;
    }
    
    int point2DClass::gety()
    {
        return y;
    }
    
    point2DClass::~point2DClass()
    {
        cout << "Object Going Out of Scope!" << endl;
    }
    
    class point3DClass:public point2DClass
    {
     private:
        int y;
        int z;
    
     public:
        point3DClass();
    
    //    void sety(int newy);
        void setz(int newz); // Mutator function
    //    int gety();
        int getz();
    
        ~point3DClass();
    };
    /////////////////////////////////////
    //// Member function implementation///
    /////////////////////////////////////
    point3DClass::point3DClass()
    {
    //    y=0;
        z=0;
    }
    
    void point3DClass::setz(int newz)
    {
        z=newz;
    }
    
    //void point3DClass::setz(int newy)
    //{
    //    y=newy;
    //}
    
    int point3DClass::getz()
    {
        return z;
    }
    
    //int point3DClass::gety()
    //{
    //    return y;
    //}
    
    point3DClass::~point3DClass()
    {
       cout << " Going Out of Scope!" << endl;
    }
    
    //////////////////////////////////////////////////
    //////Main Function Implementation///////////////
    ///////////////////////////////////////////////////
    
    int main()
    {
        point1DClass x;// create an object
    
        x.setx(3);
        cout << "x co-ordinate: " << x.getx() <<"\n"<<endl;
    
        point2DClass y;
        y.sety(4);
        cout<<"y co-ordinate:" << y.gety() <<"\n"<<endl;
    
        point3DClass z;
        z.setz(8);
        cout <<"z co-ordinate:" << z.getz() <<"\n"<<endl;
    
      system("pause");
        return 0;
    }
    

2 个答案:

答案 0 :(得分:1)

你可能想要重载构造函数。因此,对于point3DClass,您需要在头文件中包含以下内容:

class Point3D {
    public:
        int x, y, j;
        Point3D(); // default
        Point3D(int i, int j, int k); // Make new point with (i,j,k)
        Point3D(Point3D copy);
        Point3D(Point2D copy);
        Point3D(Point1D copy) {
            x = copy.getX();
            y = 0;
            z = 0;
       }
}

如果您重新格式化代码,可能会有人感到更慷慨:P

答案 1 :(得分:0)

听起来应该是,我会尽力提供足够的线索让你得到答案。

<强>继承

当您创建类似class point2DClass:public point1DClass {}的子类时,point1DClass中存在的任何方法都存在于point2DClass中,例如getX()/ setX()可用。

<强>重载

重载允许你做两件事:

  • 替换采用相同参数的父方法
  • 使用带有不同参数的父方法补充父方法

这意味着如果point1D有一个从另一个point1D实例复制值的方法,那么point2D可以从另一个point1D实例复制而不需要任何额外的代码(尽管不会触及Y)。

那么,当我们想将point2D复制到point2D时呢?您可以从2D对象中获取值并存储它们,但这意味着在构建到99D点时会增加复杂性。

解决方法是调用父方法,然后执行任何point2D特定的工作。由于我想为您留下一些工作,我将使用其他一些代码进行演示:

class A {
public:
    void copyFrom(A *a) { }
};

class B : public A {
public:
    void copyFrom(B *b) {
        A::copyFrom(b);
    }
};

int main(int argc, char** argv) {
    B *b = new B();
    B *c = new B();
    b->copyFrom(c);
    return 0;
}

当上面的代码调用b-&gt; copyFrom时,B中的实现调用A中的实现。因为A类不理解B类,所以它将它视为父类A.

希望这很清楚,可以给你一些提示。

注意 我知道我忽略了虚拟,它们不在此范围内,我不想写一本C ++书