Help with function Overloading

I have 3 classes which are point 1DClass, Point 2DClass and Point 3DClass.
Point 1DClass contains data member x
Point 2DClass contains data member y and inherits data member x from point 1DCLass
Point 1DClass contains data member z and inherits the other two data members.

these co-ordinates are to offer methods for accessing, mutating and displaying the values of co-ordinates. then multiple constructors will be provided by function overloading. this is the function overloading i need to do.

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

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

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

D- 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.

if anyone could put me on the right track for this i would really appreciate it. I am so confused.
Thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class 1D
{
 protected:
  double x;

 public:
  1D();
  1D(double x);
  
  double getX();
  void setX(double x);
};


class 2D : public 1D
{
 protected:
  double y;

 public:
  2D();
  2D(double x, double y);

  double getY();
  void setY(double y);
};


class 3D : public 2D
{
 protected:
  double z;

 public:
  3D();
  3D(double x, double y, double z);

  double getZ();
  void setZ(double z);
};


Did you get this far yet?
Last edited on
This is what i have done so far. I'm not sure if it is write though.

class point1DClass
{
private:
int x;

public:
point1DClass(); // constructor function
int getx(); //Accessor function
void setx(int newx); // Mutator function
~point1DClass(); //Destructor function
};

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();


};

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

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


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 seems okay, remember that if 3D extends 2D then it inherits the members y and x from it, although you can only access them though the public mutators methods.

I think you still need to add the second constructors though:
B- Accepting all co ordinates as arguments and setting data members to arguments.


PS: Can you format your code please?
Yeah i still need to add the second part but im unsure how to do that. Also what do you mean by format by code? i'm unsure how to format it
Put your code between code tags. When you're making/editing a post, you should see a button with <> as its icon under "Format:". Just highlight all your code and push that button.
Last edited on
OK thank you very much
I have formatted my code now. Any one have any ideas how to :
B- Accepting all co ordinates as arguments and setting data members to arguments.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <iostream>
#include <string>
#include <math.h>

using namespace std;
//////////////////////////////
////// Class definition //////
//////////////////////////////

class point1DClass
{
private:
    int x;

public:
    point1DClass(); // constructor function
    point1DClass(int x);

    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
    point2DClass(int x,int y);

    void sety(int newy); // Mutator function
    int gety(); //Accessor function

    ~point2DClass();


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

 public:
    point3DClass(); // Constructor function
    point3DClass(int x, int y, int z);

//
    void setz(int newz); // Mutator function
//
    int getz();

    ~point3DClass();
};
/////////////////////////////////////
//// Member function implementation///
/////////////////////////////////////
point3DClass::point3DClass()
{
//
    z = 0;
}

void point3DClass::setz(int newz)
{
    z = newz;
}


int point3DClass::getz()
{
    return z;
}

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




In here i have tried to accept no parameters and set the co-ordinates to 0. Is this right?
Thanks
Last edited on
You have them declared (a function header):
1
2
3
4
5
6
class point3D {
  ...
 public:
  point3D(int x, int y, int z);
  ...
};

But you need to define them (function body):
1
2
3
4
5
point3D::pont3D(int x, int y, int z) {
  setx(x);
  sety(y);
  setz(z);
}

Also, you need something similar for the other two...
Last edited on
I have now changed my code. Is this correct now.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
class point1DClass
{
private:
    int x;

public:
    point1DClass(); // constructor function
    point1DClass(int x);

    int getx(); //Accessor function
    void setx(int newx); // Mutator function
    ~point1DClass();    //Destructor function

};
/////////////////////////////////////
//// Member function implementation//
/////////////////////////////////////
point1DClass::point1DClass(int x)
{
    setx(newx);
    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
    point2DClass(int x,int y);

    void sety(int newy); // Mutator function
    int gety(); //Accessor function

    ~point2DClass();


};
/////////////////////////////////////
//// Member function implementation///
/////////////////////////////////////
point2DClass::point2DClass(int x, int y)
{
    setx(newx);
    sety(newy);

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

 public:
    point3DClass(); // Constructor function
    point3DClass(int x, int y, int z);

//
    void setz(int newz); // Mutator function
//
    int getz();

    ~point3DClass();
};
/////////////////////////////////////
//// Member function implementation///
/////////////////////////////////////
point3DClass::point3DClass(int x, int y, int z)
{
  setx(newx);
  sety(newy;)
  setz(newz);

    z = 0;
}

void point3DClass::setz(int newz)
{
    z = newz;
}


int point3DClass::getz()
{
    return z;
}


point3DClass::~point3DClass()
{
   cout << " Object Going Out of Scope!" << endl;
}

point3DClass(int newx, int newy, int newz)
{
    x=newx;
    y=newy;
    z=newz;
}


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

    point3DClass z(1,2,3);

system("pause");
return 0;
}

Topic archived. No new replies allowed.