Starting from:

$25

CSE241-PS6 Solved

PS # 6 

Define a class which name is Complex for complex numbers. A complex number is a + b*i where for our purposes, a and b are numbers of type double, and i  is a number that represents the quantity  . Represent a complex number as two values of type double. Name the member variables real and  maginary. Include a constructor with two parameters of type double that can be used to set the member variables of an object to any values. Include a constructor that has only a single parameter of type double; call this parameter realPart and define the constructor so that the object will be initialized to realPart + 0*i . Include a default constructor that initializes an object to 0 (that is, to 0 + 0*i ). Overload all the following operators so that they correctly apply to the type Complex :  

== , + , - , * , >> , and << .

 You should also write a test program to test your class. Hints: To add or subtract two complex numbers, add or subtract the two member variables of type double . The product of two complex numbers is given by the following formula:

(a + b*i)*(c + d*i) = = (a*c - b*d) + (a*d + b*c)*i  

In the interface file, you should define a constant i as follows:

const Complex i(0, 1);

2) 

Cumulatively modify the example belown as follows.  

a.       In Display 8.7 , replace the private char members first and second with an array of char of size 100 and a private data member named size . Provide a default constructor that initializes size to 10 and sets the first 10 of the char positions to '#'. (This only uses 10 of the possible 100 slots.) Provide an accessor function that returns the value of the private member size .  

Test.

b.       Add an operator[] member that returns a char& that allows the user to Access or to set any member of the private data array using a non-negative index that is less than size .

Test.

c.       Add a constructor that takes an int argument, sz , that sets the first sz members of the char array to '#' .

Test.

d.       Add a constructor that takes an int argument, sz , and an array of char of size sz . The constructor should set the first sz members of the private data array to the sz members of the argument array of char .  


3) 


Define a class which name is Rational for rational numbers. A rational number is a number that can be represented as the quotient of two integers. For example, 1/2, 3/4, 64/2, and so forth are all rational numbers. (By 1/2 and so on we mean the everyday fraction, not the integer division this expression would produce in a C++ program.) Represent rational numbers as two values of type int , one for the numerator and one for the denominator. Include a constructor with two arguments that can be used to set the member variables of an object to any legitimate values. Also include a constructor that has only a single parameter of type int ; call this single parameter wholeNumber and define the constructor so that the object will be initialized to the rational number wholeNumber /1. Include a default constructor that initializes an object to 0 (that is, to 0/1). Overload the input and output operators >> and << . Numbers are to be input and output in the form 1/2 , 15/32 , 300/401 , and so forth. Note that the numerator, the denominator, or both may contain a minus sign, so -1/2 , 15/-32 , and -300/-401 are also possible inputs. Overload all the following operators so that they correctly apply to the type Rational :

== , < , <= , > , >= , + , - , * , and / . Write a test program to test your class.  

More products