# How can I make a new class Point3D which inherits from the Point.docx

• 15

This preview shows page 1 - 6 out of 15 pages.

How can I make a new class Point3D which inherits from the Point2D class. The class has one more data member for the z coordinate. There are two constructors, a default one and one with 3 coordinates. Add the new member functions getZ, setZ and moveAlongZ. Override moveToOrigin and printLocation to do the right thing for all 3 coordinates. I need to make a few Point3D objects and exercise their functionality. Point2D.h: class Point2D { public: Point2D(); Point2D(int xCoord, int yCoord); int getX(); int getY(); void setX(int xCoord); void setY(int yCoord);
void moveHorizontally(int distance) void moveVertically(int distance) void moveToOrigin(); void printLocation(); private: int x, y; }; Point2D.cpp: #include<iostream> #include \"Point2D.h\" using namespace std; Point2D::Point2D() { x = 0; y = 0; } Point2D::Point2D(int xCoord, int yCoord) { x=xCoord; y=yCoord; } int Point2D::getX() { return x; } int Point2D::getY() { return y; } void Point2D::setX(int xCoord) {
x = xCoord; } void Point2D::setY(int yCoord) { y = yCoord; } void Point2D::moveHorizontally(int distance) { x+=distance; } void Point2D::moveVertically(int distance) { y+=distance; } void Point2D::moveToOrigin() { x = y = 0; } void Point2D::printLocation() { cout<<\"Point located at (\"<<x<<\", \"<<y<<\")\"<<endl; } main.cpp: #include<iostream> #include \"Point2D.h\" using namespace std; int main() { Point2D cp1; cp1.printLocation(); Point2D cp2(2, 3);
cp2.printLocation(); cp1.setX(5); cp1.setY(3); cp1.printLocation(); cp1.moveHorizontally(10); cp1.moveVertically(-10); cp1.printLocation(); cp1.moveToOrigin(); cp1.printLocation(); cp2.moveHorizontally(8); cp2.moveVertically(7); cp2.printLocation(); return 0; }
• • • 