有人可以帮助函数重载吗?
Can anyone help with function overloading?
我被要求做函数重载,我真的很困惑。我需要:
-
不接受任何参数并将所有坐标设置为0
-
接受所有坐标作为参数并将数据成员设置为参数
-
接受与此同时存在的对象作为参数,并将该对象的数据成员复制到当前对象中。
-
接受一个较小维度点的对象,并且只复制那些能够在当前对象中表示的数据成员,将未表示的维度设置为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++书
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 可以打印矢量和矢量中的矢量的非重载C++函数
- 错误 没有与参数列表匹配的重载函数"getline"实例
- 使用模板重载函数
- C++线程中,没有重载函数接受 X 参数
- std::vector 没有重载函数的实例与参数列表匹配
- C++重载函数,一个采用基类的参数,另一个采用派生类的参数
- 错误:无法解析对重载函数的引用;你的意思是调用它吗?
- 对重载函数find_first_not_of的不明确调用
- 如何从重载解析中删除重载函数?
- CUDA:重载函数"isnan"的多个实例
- C++派生类重载函数(带有 std::function 参数)不可见
- 避免在人为的重载函数调用中拼写出类型
- C++:如何为多个重载函数保留通用代码路径?
- 什么时候可以使用常量装饰调用我的重载函数?
- 尝试使用谓词函数会导致错误:"std::sort"未找到匹配的重载函数
- std::调用,未找到匹配的重载函数
- 为什么在传递长整型时调用具有两个双精度类型的参数的重载函数?
- 为什么使用不匹配的参数调用重载函数仍然有效
- 如何通过签名作为模板参数来解决重载函数?