Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with the following. Point2D class, Point3D class inherits from Point2D class private variables x, y. Z is new private variable in Point3D class

Need help with the following.

Point2D class, Point3D class inherits from Point2D class private variables x, y. Z is new private variable in Point3D class Functions setX(), getX(), setY(), getY() are inherited from Point2D Functions setZ() getZ() are in Point3D only Functions getPoint() , setpoint(), distanceFromOrigin() are overridden the functions with the same name from Point2D and use functions in definition functions with the same name in Point3D constructors Point3D in definition use constructors Point2D In Driver we have array of Point3D objects : 5 points are inputted from input file , see in.txt file as an example

**in.txt**

1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 3 4 0 12 0 5

**output**

enter input fileName: in.txt A. Test pp getX,getY,getZ 0. 1.1 2.2 3.3 1. 4.4 5.5 6.6 2. 7.7 8.8 9.9 3. 3 4 0 4. 12 0 5 B. Test point getPoint and distance to origin 0. 1 2.2 3.3 distance to origin = 4.09023 1. 1 5.5 6.6 distance to origin = 8.64928 2. 1 8.8 9.9 distance to origin = 13.2834 3. 1 4 0 distance to origin = 4.12311 4. 1 0 5 distance to origin = 5.09902 C. Test print() and distance to origin 0. x=1.1 y=2.2, z=3.3 distance to origin = 4.11582 1. x=4.4 y=5.5, z=6.6 distance to origin = 9.65246 2. x=7.7 y=8.8, z=9.9 distance to origin = 15.3212 3. x=3 y=4, z=0 distance to origin = 5 4. x=12 y=0, z=5 distance to origin = 13

**driver. cpp**

#include

#include

#include "Point3D.h"

using namespace std;

int main()

{

ifstream fin;

string fileName;

cout<<"enter input fileName: ";

cin>>fileName;

fin.open(fileName.c_str());

Point3D pp[5];

Point3D point[5];

double x,y,z;

for (int i=0;i<5;++i)

{

// Enter x, y and z from input file";

fin>>x>>y>>z;

pp[i].setX(x);

pp[i].setY(y);

pp[i].setZ(z);

point[i].setPoint(x,y,z);

}

cout<<"A. Test pp getX,getY,getZ "; //here test getX() and getY() functions

for (int i=0;i<5;++i)

{

cout<

<

<

<<" ";

}

cout<<"B. Test point getPoint and distance to origin "; //here test getPoint() function

for (int i=0;i<5;++i)

{

point[i].getPoint(x,y,z);

cout<

}

cout<<"C. Test print() and distance to origin ";

for (int i=0;i<5;++i)

{

cout <

pp[i].print();

cout<<" distance to origin = "

<

<<" ";

}

fin.close();

}

**point2D.cpp**

#include

#include

#include "Point2D.h"

Point2D::Point2D()

{

x = 0;

y = 0;

}

Point2D::Point2D(int x, int y)

{

this->x = x;

this->y = y;

}

Point2D::Point2D(Point2D& other)

{

x = other.x;

y = other.y;

}

double Point2D::getX()

{

return x;

}

double Point2D::getY()

{

return y;

}

void Point2D::getPoint(double& x1,double& y1)

{

x1 = getX();

y1 = getY();

}

void Point2D::setX(double x)

{

this->x = x;

}

void Point2D::setY(double y)

{

this->y = y;

}

void Point2D::setPoint(double x1, double y1)

{

setX(x1);

setY(y1);

}

double Point2D::distanceToOrigin()

{

return sqrt(x*x + y*y);

}

void Point2D::print()

{

Point2D::print();

}

Point2D::~Point2D()

{}

**point3D.cpp**

include "Point2D.h"

#include "Point3D.h"

#include

#include

Point3D::Point3D() : Point2D()

{

z=0;

}

Point3D::Point3D(double x1, double y1, double z1) :

Point2D(x1, y1)

{

z=z1;

}

Point3D::Point3D(Point3D& other) :

Point2D(other)

{

this->z = other.z;

}

double Point3D::getZ()

{

return z;

}

void Point3D::getPoint(double& x1,double& y1, double& z1 )

{

Point2D::getPoint(x1, y1);

z1=getZ();

}

void Point3D::setZ(double z1)

{

z=z1;

}

void Point3D::setPoint(double x1, double y1, double z1)

{

Point2D::setPoint(x1, y1);

setZ(z1);

}

double Point3D::distanceToOrigin()

{

int Point2DDistance = Point2D::distanceToOrigin();

return sqrt((Point2DDistance*Point2DDistance) + (getZ()*getZ()));

}

void Point3D::print()

{

Point2D::print();

}

Point3D::~Point3D()

{}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Distributed Relational Database Architecture Connectivity Guide

Authors: Teresa Hopper

4th Edition

0133983064, 978-0133983067

More Books

Students also viewed these Databases questions