Q:

Write a program that creates a class for complex numbers. A complex number is a number of the form a + bi, where a is the real

0

 Write a program that creates a class for complex numbers. A complex number is a number of the form a + bi, where a is the real part, b is the imaginary part, and i = 1 . The class Complex should have properties for the real and imaginary parts. Overload the disp function to print a complex number. 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Complex.m

classdef Complex

 properties

 realpart = 0;

 imagpart = 0;

 end

 

 methods

 

 function obj = Complex(rp, ip)

 if nargin == 2

 obj.realpart = rp;

 obj.imagpart = ip;

 end

 end

 

 function disp(obj)

 fprintf('%.1f + %.1fi\n', obj.realpart, obj.imagpart)

 end

 end

end

>> compnum = Complex(2,4)

compnum = 

2.0 + 4.0i

>>

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now