Adding a Default Constructor

Hello, I am new to C++ and have been reviewing the forum and watching video tutorials to assist me in creating a constructor for my program listed below, that will print (1,1) if the user hits the enter key without entering an number. I have made a few attempts but currently the 1,1 prints at program execution. Any guidance provided would be greatly appreciated.

#include <iostream>
#include <string>

using namespace std;

// Class Creation

class Complex
{
// Public Member Functions

public :

int add();
int subtract ();


//Constructor for 1.1

Complex(){
cout<< "1,1"<<endl;

}

// Private Data

private :

double realPart1;
double imanginaryPart1;
double realPart2;
double imanginaryPart2;
double realPart3;
double imanginaryPart3;
double realPart4;
double imanginaryPart4;

};

int main()

{
Complex Default;

int realPart1,imaginaryPart1;
int realPart2,imaginaryPart2;
int realPart3,imaginaryPart3;
int realPart4,imaginaryPart4;


// User Input

cout << "Enter a Complex number with the real and imaginary parts separated by the enter key:";
cin >> realPart1 >> imaginaryPart1;

cout << "Enter a Complex number with the real and imaginary parts separated by the enter key:";
cin >> realPart2>> imaginaryPart2;

// Calculations

realPart3 = realPart1 + realPart2;
imaginaryPart3 = imaginaryPart1 + imaginaryPart2;

realPart4 = realPart1 - realPart2;
imaginaryPart4 = imaginaryPart1 - imaginaryPart2;

// Output to User

cout <<"The sum of two complex numbers is ("<<realPart3<<","<<imaginaryPart3<< ") and the difference is ("<<realPart4<<","<<imaginaryPart4<<")";
cout << "\n";

return 0;

}
The constructor will be called when the object is created. In that case at the beginning of the program. If you want it later you need another function.
Here is an example of how your constructor can look like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class RealImag
{
    private:
        // You only need these two variables
        double realPart;
        double imaginaryPart;      
   
   public:
   // For explanation look below
   RealImag(double realArg = 1.1, double imagArg = 1.1)
   {
        setReal(realArg);
        setImaginary(imagArg);
   }

  void setReal(double realArg)
  {
	if (realArg > 0.0)
	{ realPart = realArg; }
  }

  void setImaginary(double imagArg)
  {
	if (imagArg > 0.0)
	{ imaginaryPart = imagArg; }
  }
   // --- rest of your code ---

};


Default arguments are given, remeber, if you assign a default value for one parameter, you have to provide it for all the others as well.

If you don't, a message similar to this: 'RealImag::RealImag': missing default parameter for parameter 1/2 will be output and the code will not compile. The reason this works is that a default constructor is a constructor that either has no parameters, or if there are parameters, they all have to have default values.
Otherwise the compiler will complain that there is no default constructor. And in such case, you would explicitly have to provide your default constructor:

RealImag()
{ realPart = 0; imagPart = 0; }

To be able to define your class object without passing any parameters to it.
RealImag img;

In case the constructor is written as demonstrated above, you can do this:

1
2
3
4
5
6
7
RealImag nums;

	cout << "Enter a complex number separated by a space (Ex: 1.7 2.4): ";
	cin >> realOne >> imaginaryOne;

	nums.setReal(realOne);
	nums.setImaginary(imaginaryOne);


And this:

 
	RealImag numsOne(5.7, 4.3); 


When defining an object in your main function.
Last edited on
Thank you both for your input. I am working on modifying my code to accommodate your suggestions. Seeing it laid out definitely helps to make the mental connection.
Topic archived. No new replies allowed.