Question

In: Computer Science

class Point3d { public: Point3d(double x, double y, double z); Point3d(const point3d& p); void setX(double x);...

class Point3d { public: Point3d(double x, double y, double z);

Point3d(const point3d& p);

void setX(double x);

void setY(double y);

void setZ(double z);

double getX() const;

double getY() const;

double getZ() const;

point3d& operator=(const point3d& rhs); private: double x; double y; double z;

};

Given the Point class above, complete the following:

1. Write just the signature for the overloaded addition operator that would add the respective x,y, and z from two point3d objects.

2. What is the output of the code (assuming copy constructor and copy assignment operators are properly implemented).

point3d p1{3,4,5};

point3d p2{p1};

p2.setY(2*p2.getX());

point3d* p3 = new point3d{p1.getX(), p2.getY(), p2.getZ()};

p3 = &p1; cout << *p3.getZ() << endl;

p1.setY(2*p1.getZ());

p3.setX(p2.getZ());

p1 = p2; cout << p1.getX() + “, ” p1.getY() + “, ” p1.getZ() << endl;

cout << p2.getX() + “, ” p2.getY() + “, ” p2.getZ() << endl;

cout << p3.getX() + “, ” p3.getY() + “, ” p3.getZ() << endl;

delete p3;

Solutions

Expert Solution

Given code:

.

class Point3d
{
public:
Point3d(double x, double y, double z);
Point3d(const Point3d &p);
void setX(double x);
void setY(double y);
void setZ(double z);
double getX() const;
double getY() const;
double getZ() const;
Point3d &operator=(const Point3d &rhs);

private:
double x;
double y;
double z;
};

.

Q->1. Write just the signature for the overloaded addition operator that would add the respective x,y, and z from two Point3d objects.
Answer:
Point3d &operator+(const Point3d other);
.

Q->2. What is the output of the code (assuming copy constructor and copy assignment operators are properly implemented).

Point3d p1{3, 4, 5};

Point3d p2{p1};

// p1 and p2 are both same points (3,4,5) till this line

// set p2.y = 2 * 3 = 6

p2.setY(2 * p2.getX());

// now p2 will be (3,6,5)

Point3d *p3 = new Point3d{p1.getX(), p2.getY(), p2.getZ()};

// p3 points to (3,6,5) till this line

p3 = &p1;

// p3 will be same as p1 due to above line ie (3,4,5)

cout << (*p3).getZ() << endl;

// prints 5

p1.setY(2 * p1.getZ());

// above line sets p1.y = 2 * 5 = 10

// now p1 is (3,10,5)

p3->setX(p2.getZ());

// above line sets p3.x = 5, it also modifies p1

// now p3 will be (5,4,5)

p1 = p2;

// now p1 = p2 = (3,6,5)

// and p3 points to p1, it also points to (3,6,5)

// print all points

cout << p1.getX() + ", " p1.getY() + ", " p1.getZ() << endl;

cout << p2.getX() + ", " p2.getY() + ", " p2.getZ() << endl;

cout << p3.getX() + ", " p3.getY() + ", " p3.getZ() << endl;

// above lines prints:

// 3, 6, 5

// 3, 6, 5

// 3, 6, 5

delete p3;

.

Final output through running program::

.


Related Solutions

#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int,...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int, int); }; class line{ private: point ps; point pe; public: void print()const; void setf(int, int, int, int); }; class rectangle{ private: line length[2]; line breadth[2]; public: void print()const; void setf(int, int, int, int, int, int, int, int); }; int main(){ rectangle r1; r1.setf(3,4,5,6, 7, 8, 9, 10); r1.print(); system("pause"); return 0; } a. Write function implementation of rectangle, line and point. b. What is...
Study the following class definition: class Car { public: Car(double speed); void start(); void accelerate(double speed);...
Study the following class definition: class Car { public: Car(double speed); void start(); void accelerate(double speed); void stop(); double get_speed() const; private: double speed; }; Which of the following options would make logical sense in the definition of the void accelerate(double speed)function? Group of answer choices this->speed = this->speed; this->speed = speed; this.speed = speed; speed1 = this->speed; Flag this Question Question 131 pts The Point class has a public function called display_point(). What is the correct way of calling...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y;...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y; public MyPoint() { this(0, 0); } public MyPoint(double x, double y) { this.x = x; this.y = y; } // Returns the distance between this MyPoint and other public double distance(MyPoint other) { return Math.sqrt(Math.pow(other.x - x, 2) + Math.pow(other.y - y, 2)); } // Determines if this MyPoint is equivalent to another MyPoint public boolean equals(MyPoint other) { return this.x == other.x &&...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y;...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y; public MyPoint() { this(0, 0); } public MyPoint(double x, double y) { this.x = x; this.y = y; } // Returns the distance between this MyPoint and other public double distance(MyPoint other) { return Math.sqrt(Math.pow(other.x - x, 2) + Math.pow(other.y - y, 2)); } // Determines if this MyPoint is equivalent to another MyPoint public boolean equals(MyPoint other) { return this.x == other.x &&...
COMPLETE JAVA CODE public class Point2 { private double x; private double y;    /** *...
COMPLETE JAVA CODE public class Point2 { private double x; private double y;    /** * Create a point with coordinates <code>(0, 0)</code>. */ public Point2() { complete JAVA code this.set(0.0, 0.0); COMPLETE CODE }    /** * Create a point with coordinates <code>(newX, newY)</code>. * * @param newX the x-coordinate of the point * @param newY the y-coordinate of the point */ public Point2(double newX, double newY) { complete Java code this.set(newX, newY); }    /** * Create a...
Base Class class TransportationLink { protected: string _name; double _distance; public: TransportationLink(const string &name, double distance);...
Base Class class TransportationLink { protected: string _name; double _distance; public: TransportationLink(const string &name, double distance); const string & getName() const; double getDistance() const; void setDistance(double); // Passes in the departure time (as minute) and returns arrival time (as minute) // For example: // 8 am will be passed in as 480 minutes (8 * 60) // 2:30 pm will be passed in as 870 minutes (14.5 * 60) virtual unsigned computeArrivalTime(unsigned minute) const = 0; }; #endif Derived Classes...
import java.util.Random; class Conversions { public static void main(String[] args) { public static double quartsToGallons(double quarts)...
import java.util.Random; class Conversions { public static void main(String[] args) { public static double quartsToGallons(double quarts) { return quarts * 0.25; } public static double milesToFeet(double miles) { return miles * 5280; } public static double milesToInches(double miles) { return miles * 63360; } public static double milesToYards(double miles) { return miles * 1760; } public static double milesToMeters(double miles) { return miles * 1609.34; } public static double milesToKilometer(double miles) { return milesToMeters(miles) / 1000.0; } public static double...
Which of the following are correct for P(x,y,z)= xyz + x(yz)' + x'(y + z) +...
Which of the following are correct for P(x,y,z)= xyz + x(yz)' + x'(y + z) + (xyz)' ? 1) P(0, 0, 1) = 0 2) P(0, 1, 0) = 1 3) P(0, 0, 0) = 1 4) P(1, 1, 1) = 1 5) P(1, 0, 0) = 0
import java.util.Scanner; public class Lab9Q3 { public static void main (String [] atgs) { double mass;...
import java.util.Scanner; public class Lab9Q3 { public static void main (String [] atgs) { double mass; double velocity; double totalkineticEnergy;    Scanner keyboard = new Scanner (System.in); System.out.println ("Please enter the objects mass in kilograms"); mass = keyboard.nextDouble();    System.out.println ("Please enter the objects velocity in meters per second: "); velocity = keyboard.nextDouble();    double actualTotal = kineticEnergy (mass, velocity); System.out.println ("Objects Kinetic Energy: " + actualTotal); } public static double kineticEnergy (double mass, double velocity) { double totalkineticEnergy =...
C++ QUESTION ABOUT FIXING CODE AND RUNNING TESTS: class SpeakerSystem { public: void vibrateSpeakerCones(unsigned signal) const...
C++ QUESTION ABOUT FIXING CODE AND RUNNING TESTS: class SpeakerSystem { public: void vibrateSpeakerCones(unsigned signal) const { cout << "Playing: " << signal << "Hz sound..." << endl; cout << "Buzz, buzzy, buzzer, bzap!!!\n"; } }; class Amplifier { public: void attachSpeakers(const SpeakerSystem& spkrs) { if(attachedSpeakers) cout << "already have speakers attached!\n"; else attachedSpeakers = &spkrs; }    void detachSpeakers() { // should there be an "error" message if not attached? attachedSpeakers = nullptr; }    void playMusic( ) const...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT