任何人都可以帮助解决函数重载问题吗?

发布于 2024-11-05 07:00:44 字数 2963 浏览 1 评论 0原文

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

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

I have been asked to do function overloading and i am really confused. I need to :

  1. Accepting no parameters and setting all co-ordinates to 0.

  2. Accepting all co ordinates as arguments and setting data members to arguments.

  3. Accepting an existing object of the same time as an argument, and duplicating the data members of this object into the current object.

  4. Accepting an object of a point of lesser dimensionality, and copying only those data members that are able to be represented in the current object setting unrepresented dimensions to 0.

This is my code so far and i was wondering if i have done any of the above , if not can some one direct me how to do the others. Thank you

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

牵你手 2024-11-12 07:00:44

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

You're probably going to want to overload the constructor. So, for point3DClass, you'll want the following in the header file:

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

If you reformat your code, someone will probably feel more generous :P

不甘平庸 2024-11-12 07:00:44

这听起来应该是,我'我们会尽力提供足够的线索让您找到答案。

继承

当您创建像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->copyFrom 时,B 中的实现调用 A 中的实现。因为 A 类不理解 B 类,它把它当作父类 A。

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

注意我知道我忽略了虚拟,它们不在本文的讨论范围内,而且我不想写一本 C++ 书

As this sounds like it should be , I'll try to provide enough clues for you to get to the answer.

Inheritance

When you create a subclass like class point2DClass:public point1DClass {}, any method that exists in point1DClass exists in point2DClass, e.g. getX()/setX() is available.

Overloading

Overloading allows you to do 2 things:

  • Replace a parent method that takes the same arguments
  • Supplement a parent method with one that takes different arguments

This means that if point1D has a method to copy values from another point1D instance, then point2D can copy from another point1D instance without any extra code (although Y won't be touched).

So, what about when we want to copy point2D into point2D? You could fetch the values from the 2D object, and store them, but this means increasing complexity as we build up to point 99D.

The solution is to call the parent method, then do any point2D specific work. As I want to leave some work for you, I'll demonstrate with some other code:

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

When the above code calls b->copyFrom, the implementation in B calls the implementation in A. Because class A doesn't understand class B, it treats it as the parent class A.

Hopefully this is clear enough to give you some hints.

Note I know I've ignored virtuals, they're not in scope for this, and I don't want to write a C++ book

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文