有人可以帮助函数重载吗?

Can anyone help with function overloading?

本文关键字:重载 函数 帮助      更新时间:2023-10-16

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

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

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

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

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

这是我的代码到目前为止,我想知道我是否做了上述任何一个,如果不是可以有人指导我如何做其他人。谢谢你

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

您可能想要重载构造函数。因此,对于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

因为这听起来像是家庭作业,所以我会尽量为您提供足够的线索来找到答案。

当你创建像class point2DClass:public point1DClass {}这样的子类时,任何存在于point1DClass中的方法都存在于pointt2dclass中,例如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->copyFrom时,b中的实现调用A中的实现。因为类A不理解类b,它将其视为父类A。

希望这足够清楚,给你一些提示。

注意我知道我已经忽略了虚拟,它们不在这里的范围内,我不想写一本c++书