Explore BrainMass
Share

Classes and objects

The code is attached below.
Provide operator overloading functions to facilitate the following operations on any complex numbers A, B and C:
a. A == B; overload Boolean operator == to test if A and B are equal
b. C = A*B; overload multiplication operator *
c. C = A/B; overload division operator /
d. A = B; overload assignment operator =
e. cin >> A >> B; overload stream operator >> to take user input for real and imaginary part of the number (You might want to create a friend function to facilitate
this)
Also, write a static function for the complexNumber class which initializes numCount variable. Your function takes an integer argument provided by user. Assign this:
argument to numCount variable.
#include <iostream>
using namespace std;class complexNumber {
private:
static int numCount; int Re, Imag;
public:
complexNumber(){numCount++;} // constructor with no argument
complexNumber(int Real, int Imaginary){numCount++; cout << "constructor
with argument is called" << endl;
Re = Real; Imag = Imaginary;}// constructor with arguments
~complexNumber(){cout << "destructor is called" <<endl; numCount--;}
complexNumber (complexNumber &anycomplex) {numCount++; cout <<endl;
cout <<"Copy constructor called" << endl; Re = anycomplex.Re; Imag =
anycomplex.Imag;} // copy constructor
void printComplex(){ cout <<"number of active complex numbers= " <<
numCount << " and present number.....= " << Re <<" + j" << Imag << endl;}
complexNumber operator+(complexNumber &b){complexNumber temp; cout <<
"overloaded + iscalled" << endl; temp.Re = Re + b.Re; temp.Imag = Imag +
b.Imag; return temp;}
complexNumber operator++(){complexNumber temp; cout << "overloaded
++(pre) called" << endl; temp.Re = ++Re; temp.Imag = ++Imag; return temp;}
complexNumber operator++(int){complexNumber temp; cout << "overloaded
++(post) called" << endl; temp.Re = Re++; temp.Imag = Imag++; return temp;}
friend ostream & operator<< (ostream &out, complexNumber
&somecomplex){cout << "overloaded << called" <<endl; out <<
somecomplex.Re<<"+ j" << somecomplex.Imag; return out;}
};
int complexNumber::numCount = 0 ;
void printC(complexNumber a) {a.printComplex();}
int main (){
complexNumber A, B(1,2), C(2,3);
B.printComplex();
++ B;
printC(C);
A = B + C;
A = B++;
A.printComplex();
B.printComplex();
cout << A << B;
cin.get();
return 0;
}

Problem 2: Look at the following class definition:
class Employee{
private: static int numEmployee; // number of employees
char * firstName; // first name of the employee
char * lastName; // last name of employee
public: // include your methods (constructors, destructors etc..
here)
};
Provide
a. constructor; should take two arguments for firstName and lastName
b. copy constructor
c. destructor
d. Overload assignment operator =
Note that we are using a pointer to character array, therefore, we must use deep copy; also, to facilitate that you must utilize the new/delete operator to create/destroy a dynamic array while designing your methods. You should adjust the numEmployee variable while creating/destroying an object.
e. Provide a method that prints the number of employee, first name and last name of the employee. Show the use of above functions in your main program.

© BrainMass Inc. brainmass.com August 17, 2018, 12:09 am ad1c9bdddf

Solution Preview

There are some points I want to make. Your code for complexNumber uses integers for real and imaginary parts of a complex ...

Solution Summary

The expert examines classes and objects for assignment operators.

$2.19