Need help with my code
May 6, 2011 at 7:50pm UTC
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 needed 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.
I was wondering if anyone would be able to tell me if i have done this correctly within my code. If not how can i do it?
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 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
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 << " 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;
}
Last edited on May 6, 2011 at 8:45pm UTC
May 6, 2011 at 8:00pm UTC
Topic archived. No new replies allowed.