Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
#include<iostream>
#include<math.h>
using namespace std;
class TwoD
{
private:
double x,y;
public:
TwoD(){
x=y=0.0; cout<<"TwoD default constructor"<<endl;
}
TwoD(double i, double j): x(i), y(j){
cout<<"TwoD constructor with two arguments"<<endl;
}
void setX(double newX) { x= newX;}
void setY(double newY) { y= newY;}
double getX() const {return x;}
double getY() const {return y;}
double getDistance(const TwoD& otherPoint) const;
};
double TwoD::getDistance(const TwoD& otherPoint) const{
double point1[2], point2[2];
double dx, dy;
double distance;
point1[0]=x;
point1[1]=y;
point2[0]=otherPoint.getX();
point2[1]=otherPoint.getY();
dx=point2[0]-point1[0];
dy=point2[1]-point1[1];
distance=sqrt(dx*dx + dy*dy);
return distance;
}
class ThreeD: public TwoD
{
private:
double z;
public:
//default constructor that calls default constructor of TwoD automatically
//and also sets the value of z to 0.0
ThreeD():z(0.0){}
//constructor taking values for x,y and z. first two values are passed to TwoD constructor
//last value is used to initialize z
ThreeD(double i, double j, double k):TwoD(i,j), z(k){}
void setZ(double newZ) {z=newZ;}
double getZ() const{ return z;}
double getDistance(const ThreeD& otherPoint) const;
};
//required method to find distance between two ThreeD points
double ThreeD::getDistance(const ThreeD& otherPoint) const{
//everything similar to getDistance() of TwoD class, but here, we consider
//z values too
double point1[3], point2[3];
double dx, dy, dz;
double distance;
point1[0]=getX();
point1[1]=getY();
point1[2]=getZ();
point2[0]=otherPoint.getX();
point2[1]=otherPoint.getY();
point2[2]=otherPoint.getZ();
dx=point2[0]-point1[0];
dy=point2[1]-point1[1];
dz=point2[2]-point1[2];
//finding distance and returning it
distance=sqrt(dx*dx + dy*dy + dz*dz);
return distance;
}
int main(){
//creating a ThreeD object using default constructor
ThreeD p1;
double x,y,z;
cout<<"This program asks for the coordinates of two points"<<endl;
cout<<"in 3D space and calculates their distance."<<endl;
cout<<"Please enter the xyz coordinates for the first point:"<<endl;
//reading x,y,z values
cin>>x>>y>>z;
//updating using setter methods
p1.setX(x);
p1.setY(y);
p1.setZ(z);
//asking and reading values for second point
cout<<"Please enter the xyz coordinates for the second point:"<<endl;
cin>>x>>y>>z;
//initializing another ThreeD object, using parameterized constructor
ThreeD p2(x,y,z);
//displaying distance.
cout<<"Distance is: "<<p1.getDistance(p2)<<endl;
return 0;
}
/*OUTPUT*/
TwoD default constructor
This program asks for the coordinates of two points
in 3D space and calculates their distance.
Please enter the xyz coordinates for the first point:
1 1 1
Please enter the xyz coordinates for the second point:
2 3 4
TwoD constructor with two arguments
Distance is: 3.74166
Question 2 - Programming Exercise 1. Make a directory for this lab and change into it....
NEEDS TO BE IN PYTHON: (The Point class) Design a class named Point to represent a point with x- and y-coordinates. The class contains: - Two private data fields x and y that represent the coordinates with get methods. - A constructor that constructs a point with specified coordinates, with default point (0, 0). - A method named distance that returns the distance from this point to another point of the Point type. - A method named isNearBy(p1) that returns...
4.a)
4.b>
4.c)
C++ Programming Lab Exercise 09 Inheritance. Friend Functions, and Polymorphism (virtual functions) 4.a) Run the following code and observe the output. #include <iostream> #include <string> using namespace std; class Vehicle public: void print() { cout << "Print: I am a vehicle. \n"; } void display() { cout << "Display: I am a vehicle. \n"; } }; class Car: public Vehicle { public: void print() { cout << "Print: I am a car.\n"; } void display() { cout...
Inheritance Problem: (C++) (The MyPoint class) Design a class named MyPoint to represent a point with x-and y-coordinates. The class contains: Two data fields x and y that represent the coordinates. A no-arg constructor that creates a point (0, 0). A constructor that constructs a point with specified coordinates using passed in arguments. Two get functions for data fields x and y, respectively. A method named distance that returns the distance from this point to another point of the MyPoint...
Extend the code from Lab6.A template is given to you with CircleHeader.h, Circle.cpp and CircleMain.cpp Use the same Circle UML as below and make extensions as marked and as needed. Given below is a UML for the Painting Class. You will need to write PaintingHeader.h and Painting.cpp. The Painting Class UML contains a very basic skeleton. You will need to flesh out this UML and add more instance variables and methods as needed. You do NOT need to write a...
Exercise 2: There can be several constructors as long as they differ in number of parameters or data type. Alter the program so that the user can enter just the radius, the radius and the center, or nothing at the time the object is defined. Whatever the user does NOT include (radius or center) must be initialized somewhere. There is no setRadius function and there will no longer be a setCenter function. You can continue to assume that the default...
Please answer all the questions thank you
1) (Classes – 20 Points) Consider the following class declaration for Time to complete the questions below: DO NOT WRITE MORE THAN ASKED FOR. class Time private: int hours; int minutes; public: Time(); Time (int , int m = 0); void addMin(int m); void addHr(int h); void reset(int h = 0, int m = 0); Time operator+(const Time & t) const; Time operator-(const Time & t) const; Time operator*(double n) const; friend Time...
Create a MyPoint class to model a point in a two-dimensional space. The MyPoint class contains: • Two data fields x and y that represent the coordinates. • A no-arg constructor that creates a point (0,0). • A constructor that constructs a point with specified coordinates. • Two get functions for data fields x and y, respectively. • Two set functions for data fields x and y, respectively. • A function named distance that returns the distance from this point...
This is a C++ programming question. Please provide the correct,
workable code. Use the following three programs to help solve the
problem. Provide comments throughout the code.
Problem to solve:
Code 1: Complex.h
#pragma once
#ifndef COMPLEX_H
#define COMPLEX_H
class Complex
{
private:
double real;
double imag;
public:
// initialize the complex number to 0.0
Complex() : real(0.0), imag(0.0) {}
// initialize the complex number at declaration or
new
Complex(double r, double i) :...
C++
Question 1) Consider a class Point that models a 2-D point with x and y coordinates. Define the class point that should have the following Private data members x and y (of type int), with default values of 0 A constant ID of type int A private static integer data member named numOfPoints This data member should be o Incremented whenever a new point object is created. o Decremented whenever a point object is destructed. A default constructor An...