Question

c++ Please write an implementation of the copy constructor. Please write an implementation of the assignment...

c++

Please write an implementation of the copy constructor.
Please write an implementation of the assignment operator.

Problem #3: The implementation below will CRASH because it’s missing a copy constructor and an assignment operator. #include <iostream> using namespace std;

class Triangle { public: Triangle() { p = new Point[3]; } Triangle(int x1,int y1,int x2, int y2,int x3,int y3) { p = new Point[3]; p[0].x = x1; p[0].y = y1; p[1].x = x2; p[1].y = y2; p[2].x = x3; p[2].y = y3; } Triangle::~Triangle() { delete [] p; } private: struct Point { int x,y; Point(int px=0,int py=0): x(px), y(py) { } }; Point *p; };

int main() { Triangle *array[3]; array[0] = new Triangle(1,1,1,3,3,1); array[1] = new Triangle(2,2,2,6,6,2); array[2] = new Triangle(3,3,3,9,9,3); Triangle c2 = *array[0]; c2 = *array[1];

for(int i=0;i<3;i++) delete array[i]; }


0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
using namespace std;

class Triangle {
   public:
  
   Triangle() {
       p = new Point[3];
       }
  
   Triangle(int x1,int y1,int x2, int y2,int x3,int y3)
   {
       p = new Point[3];
       p[0].x = x1;
       p[0].y = y1;
       p[1].x = x2;
       p[1].y = y2;
       p[2].x = x3;
       p[2].y = y3;
      
   }
  
   Triangle(const Triangle &t) // copy constructor
   {
       p = new Point[3];
       p[0].x = t.p[0].x;
       p[0].y = t.p[0].y;
       p[1].x = t.p[1].x;
       p[1].y = t.p[1].y;
       p[2].x = t.p[2].x;
       p[2].y = t.p[2].y;
      
   }
  
   void operator =(const Triangle &t) // assignment operator
   {
       p = new Point[3];
       p[0].x = t.p[0].x;
       p[0].y = t.p[0].y;
       p[1].x = t.p[1].x;
       p[1].y = t.p[1].y;
       p[2].x = t.p[2].x;
       p[2].y = t.p[2].y;
      
   }
  
   ~Triangle() {
       delete [] p;
       }
  
   private:
  
   struct Point {
      
       int x,y;
      
       Point(int px=0,int py=0): x(px), y(py) { }
       };
      
       Point *p;
  
  
};

int main() {
  
   Triangle *array[3];
  
   array[0] = new Triangle(1,1,1,3,3,1);
  
   array[1] = new Triangle(2,2,2,6,6,2);
  
   array[2] = new Triangle(3,3,3,9,9,3);
  
   Triangle c2 = *array[0];
  
   c2 = *array[1];

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

delete array[i];
  
  
}

Do ask if any doubt. Please upvote.

Add a comment
Know the answer?
Add Answer to:
c++ Please write an implementation of the copy constructor. Please write an implementation of the assignment...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Must be in Python 3.6 (please have a screen shot on the code) ex 2.14 :...

    Must be in Python 3.6 (please have a screen shot on the code) ex 2.14 : # Enter three points for a triangle x1, y1, x2, y2, x3, y3 = eval(input("Enter three points for a triangle: ")) # Compute the length of the three sides side1 = ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)) ** 0.5 side2 = ((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 -...

  • Problem #1 Write a user-defined function called mysecant. (Not an an please!) The purpose of the...

    Problem #1 Write a user-defined function called mysecant. (Not an an please!) The purpose of the function is: Given two points, compute the line between them and so where the corresponding y3-0. If there is no solution, Hint: The governing equations are: (y3-y1)-(y2-y1) / (x2-x1) * (x3-x1) if y3-0, then solve for x3, and so, x3-x1-y1 * (x2-x1) / (y2-y Inputs: x1, y1, x2, y2 Output: x3

  • Find an orthogonal change of variables that eliminates the cross product terms in the quadratic form...

    Find an orthogonal change of variables that eliminates the cross product terms in the quadratic form Q, and express Q in terms of the new variables. 7x{ + 6x2 + 5x3 - 4X1X2 + 4x2X3 A substitution x = Py that eliminates cross-product terms is X1 = o A substitution x = Py that eliminates cross-product terms is Xi = – -}y.+3y2– žys, x2 = - - Žy2+3y2 +3v3, x3 = - {yı+ {y2– žv3. The new quadratic form is...

  • I need the solution of this question asap 3, Cov(X1, X2) = 2, Cov(X2, X3) =...

    I need the solution of this question asap 3, Cov(X1, X2) = 2, Cov(X2, X3) = -2, 5. Let Var(x1) = Var(X3) = 2, Var(X2) Cov(X1, X3) = -1. i) Suppose Y1 = X1 - X2. Find Var(Y1). ii) Suppose Y2 = X1 – 2X2 – X3. Find Var(Y2) and Cov(Y1, Y2). Assuming that (X1, X2, X3) are multivariate normal, with mean 0 and covariances as specified above, find the joint density function fyy, y,(91, y2). iii) Suppose Y3 =...

  • = = 3, Cov(X1, X2) = 2, Cov(X2, X3) = -2, Let Var(X1) = Var(X3) =...

    = = 3, Cov(X1, X2) = 2, Cov(X2, X3) = -2, Let Var(X1) = Var(X3) = 2, Var(X2) Cov(X1, X3) = -1. i) Suppose Y1 = X1 - X2. Find Var(Y1). ii) Suppose Y2 = X1 – 2X2 – X3. Find Var(Y2) and Cov(Yı, Y2). Assuming that (X1, X2, X3) are multivariate normal, with mean 0 and covariances as specified above, find the joint density function fxı,Y,(y1, y2). iii) Suppose Y3 = X1 + X2 + X3. Compute the covariance...

  • Q2 Suppose X1, X2, X3 are independent Bernoulli random variables with p = 0.5. Let Y;...

    Q2 Suppose X1, X2, X3 are independent Bernoulli random variables with p = 0.5. Let Y; be the partial sums, i.e., Y1 = X1, Y2 = X1 + X2, Y3 = X1 + X2 + X3. 1. What is the distubution for each Yį, i = 1, 2, 3? 2. What is the expected value for Y1 + Y2 +Yz? 3. Are Yį and Y2 independent? Explain it by computing their joint P.M.F. 4. What is the variance of Y1...

  • Create a UML diagram with 3 lines per class/interface including all constructors. public class Point {...

    Create a UML diagram with 3 lines per class/interface including all constructors. public class Point { public double X, Y; public Point() { this(0, 0); } public Point(double newX, double newY) { X = newX; Y = newY; } public static double distance(Point A, Point B) { return Math.sqrt(Math.pow(A.X-B.X, 2) + Math.pow(A.Y-B.Y, 2)); } } public interface Polygon { public int getNumberOfSides();    public double getPerimeter();    public double getArea();    } public abstract class Simple_polygon implements Polygon{ public Point...

  • matlab 3. Filename: area2d.m The area of a triangle whose three vertices are points (x1, yı),...

    matlab 3. Filename: area2d.m The area of a triangle whose three vertices are points (x1, yı), (X2, y2), and (x3, y3) can be found using the absolute value of the following equation: A = 3 [x: (– yz) – x2\1 – yz) + x3V1 – Yz)] Write a MATLAB function area2d that calculates the area of a triangle using the following syntax: A = area2d (x,y) where x and y are vectors such that x = [X1, X2, X3] and...

  • [C#] I need to convert this if/else statements into Switch I have the code for the...

    [C#] I need to convert this if/else statements into Switch I have the code for the if/else but now need to turn it into a switch. The Console.WriteLine and all that is fine. I just need to convert the if/else.      int x1, x2, y1, y2;                 Console.WriteLine("What is smallest value of x:");                 x1 = Convert.ToInt32(Console.ReadLine());                 Console.WriteLine("What is largest value of x:");                 x2 = Convert.ToInt32(Console.ReadLine());                 Console.WriteLine("What is smallest value of y:");                 y1 = Convert.ToInt32(Console.ReadLine());                 Console.WriteLine("What is largest value of y:");                 y2 =...

  • To rewrite in case not clear: c) Compute P(X2 | Y2) d) Are the sets of...

    To rewrite in case not clear: c) Compute P(X2 | Y2) d) Are the sets of events X and Y independent? Question 5 The following table presents the probabilities associated with the outcomes for the sets of bivariate events X and Y. The marginal probabilities are in the margins of the table. Y1 Y2 X1 ? 0.09 ? 0.41 X2 0.10 ? 0.03 X3 ? 0.11 ? 0.41 0.25 Y3 Assume P(Y3|X1) = 36/123 and P(Yl)=0.36. a) Compute P(Yin X2)....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT