Can anyone help with function overloading?
I have been asked to do function overloading and i am really confused. I need to :
Accepting no parameters and setting all co-ordinates to 0.
Accepting all co ordinates as arguments and setting data members to arguments.
Accepting an existing object of the same time as an argument, and duplicating the data members of this object into the current object.
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开发者_C百科 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;
}
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
As this sounds like it should be homework, 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
精彩评论