Explore BrainMass
Share

Overloaded constructor

This content was STOLEN from BrainMass.com - View the original, and get the already-completed solution here!

Write the statements to create an overloaded constructor for the class named Car. The constructors should support 0, 1, or 2 arguments. The first argument will denote the engine size and contain an Integer. The default value is 360. The second argument will denote the color and should have a type of Automobile.Color. The default value is the enumeration White. Declare constants named cintDefaultEngineSize and ccolDefaultColor to store the default values. If no arguments are supplied, then use the default value. If one argument is supplied to the constructor, then assume that the argument is defining the engine size. If two arguments are supplied to the constructor, then assume that the first argument contains the engine size and the second contains the color. For each of the constructors, store the default or specified arguments in the hidden variables named mintEngineSize and mcolDefaultColor.

© BrainMass Inc. brainmass.com October 24, 2018, 10:47 pm ad1c9bdddf
https://brainmass.com/computer-science/security/overloaded-constructor-170564

Solution Preview

class Car
{
enum AutomobileColor
{
WHITE,
BLACK,
BLUE,
SILVER,
YELLOW,
} ;
static const int cintDefaultEngineSize = ...

$2.19
See Also This Related BrainMass Solution

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.

View Full Posting Details