How do I make my code modular - include header, source and main files

So I need to create a modular class (a header, source and main file to implement this program). I have started the header file but not sure how to define the getters and setters.

Header file code
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
class Complex
{
	private:
		int real;
		int imag;

	public;
//Constructors
	Complex();       
       	Complex (int r = 0, int i =0); real(r) imag(i) {}; // not sure why the curly brackets are here??

//Destructor
        ~Complex();

//Operators
         Complex operator + (Complex &v);
 	 Complex operator += (Complex &v);
	 Complex operator -= (Complex &v);
	 Complex operator / (Complex &v);
	 Complex operator * (Complex &v);

//Setter
         void Setnumber(int a, int b);
//Getter	 
         int Getnumber
	 int 
}

//header file ends here

//Working code but not modular starts here

  #include <iostream>
using namespace std;
 
class Complex
{
    private:
        int real;
        int imag;    
 
    public:
        Complex(int r = 0, int i = 0): real(r), imag(i) {};
 
        void setComplex(void)
        {
            cout << "Enter the real and imaginary parts : ";
            cin >> this->real;
            cin >> this->imag;
        }  
        Complex add(const Complex& c) // plus operator
        {
            Complex comp;
            comp.real = this->real + c.real;
            comp.imag = this->imag + c.imag;
            return comp;
        }
        Complex subtract(const Complex& c) //minus operator
        {
            Complex comp;
            comp.real = this->real - c.real;
            comp.imag = this->imag - c.imag;
            return comp;
        }
	 Complex addAssign(const Complex& c) //Add aand assign operator
        {
            Complex comp;
            comp.real = this->real += c.real;
            comp.imag = this->imag += c.imag;
            return comp;
        }
	  Complex subAssign(const Complex& c) //subtract and assign operator
        {
            Complex comp;
            comp.real = this->real -= c.real;
            comp.imag = this->imag -= c.imag;
            return comp;
        }
	  Complex division(const Complex& c) //division operator
        {
            Complex comp;
            comp.real = this->real -= c.real;
            comp.imag = this->imag -= c.imag;
            return comp;
        }
	  Complex multiplication(const Complex& c) //multiplication operator
        {
            Complex comp;
            comp.real = this->real -= c.real;
            comp.imag = this->imag -= c.imag;
            return comp;
        }
        void printComplex(void)
        {
            cout << "Real      : " << this->real << endl
                 << "Imaginary : " << this->imag << endl;
        }
};
 
int main()
{
    Complex a, b, c, d, e, f, g;
 
    cout << "Setting first complex number " << endl;
    a.setComplex();
    cout << "Setting second complex number " << endl;
    b.setComplex();
    /* Adding two complex numbers */
    cout << "Addition of a and b    : " << endl;
    c = a.add(b);
    c.printComplex();
    /* Subtracting two complex numbers */
    cout << "Subtraction of a and b : " << endl;
    d = a.subtract(b);
    d.printComplex();
     /* Add and assign two complex numbers */
    cout << "Subtraction of a and b : " << endl;
    d = a.addAssign(b);
    d.printComplex();
     /* Subtract aand assign two complex numbers */
    cout << "Subtraction of a and b : " << endl;
    e = a.subAssign(b);
    e.printComplex();
     /* Multiply two complex numbers */
    cout << "Subtraction of a and b : " << endl;
    f = a.multiplication(b);
    f.printComplex();
     /* Divide two complex numbers */
    cout << "Subtraction of a and b : " << endl;
    g = a.division(b);
    g.printComplex();
}
See this:

http://www.cplusplus.com/doc/tutorial/classes/

for how to implement class functions.

For more infos read this:

http://www.cplusplus.com/forum/articles/10627/
So far I have created my header, source and main files
I think my header is good, I am questioning my use of the length and print function, I am unsure how to correctly implement in the sources and call in the main. Any ideas?

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184

// Header File Starts here 
class Complex
{
	private:
		int real;
		int imag;
		// aditional func here length/absolute value functuion...

	public;
//Constructors
	Complex();       
       	Complex (int r = 0, int i =0); real(r), imag(i) {}; 

//Destructor
        ~Complex();

//Operators
         Complex operator + (Complex &c);
 	 Complex operator += (Complex &c);
	 Complex operator -= (Complex &c);
	 Complex operator / (Complex &c);
	 Complex operator * (Complex &c);

//Setter
         void Setr(int r);
	 void seti (int i);

//Getter	 
         int Getr() {return r;}
	 int Geti() {return i;}

//Member Func ?? Not sure if necessary 
 void Print ();
 int length (); 
}

//Source Code
#include "HW4Header.h"

#include<iostream>
#include<cmath>

using namespace std;

Complex Complex:: operator + (Complex &c) // plus operator
        {
            Complex comp;
            comp.real = this->real + c.real;
            comp.imag = this->imag + c.imag;
            return comp;
        }
/*Complex Complex operator += (Complex &c) //minus operator
        {
            Complex comp;
            comp.real = this->real - c.real;
            comp.imag = this->imag - c.imag;
            return comp;
        }*/
Complex Complex:: operator += (Complex &c) //Add aand assign operator
        {
            Complex comp;
            comp.real = this->real += c.real;
            comp.imag = this->imag += c.imag;
            return comp;
        }
Complex Complex:: operator -= (Complex &c) //subtract and assign operator
        {
            Complex comp;
            comp.real = this->real -= c.real;
            comp.imag = this->imag -= c.imag;
            return comp;
        }
Complex Complex::  operator / (Complex &c) //division operator
        {
            Complex comp;
            comp.real = this->real / c.real;
            comp.imag = this->imag / c.imag;
            return comp;
        }
Complex Complex::  operator * (Complex &c) //multiplication operator
        {
            Complex comp;
            comp.real = this->real * c.real;
            comp.imag = this->imag * c.imag;
            return comp;
        }
double Complex() // How do I define the function properly here ??
{
	Complex comp;
	comp.real = this->real*c.real;
	comp.imag = this ->imag*c.imag;

	return (sgrt(pow(r,2)+pow(i,2))); //not sure if the variables are defined 
//accurately here,  can I have two return statements here
	//return comp;
}

void Print()
{
        return cout<<"Real Part is r"<<r<<"Imaginary Part is i"<<i<<endl; 
// What does th print function really do??
}

Complex::Complex ()
{
	real=0; imag=0;
}

/*Complex::Complex(int r, int i)
{ no need to define, already defined... 
	real=r; imag=i;
}*/

Complex::~Complex()
{
	//Destructor
}

//Main File starts here 

#include "HW4Header.h"

#include<iostream>
#include<cmath>

using namespace std;
  
int main()
{
    Complex a(1,3); // assume (realpart, imaginary)
    Complex b(2,4);
    Complex c;
    Complex d;
    Complex e;
    Complex f; 
    Complex g;

    cout << "Setting first complex number " << endl;
    a.setr();
    cout << "Setting second complex number " << endl;
    b.seti();

    /* Adding two complex numbers */
    cout << "Addition of a and b    : " << endl;
    c = a+b;
    c.printComplex();

    /* Subtracting two complex numbers */
    cout << "Subtraction of a and b : " << endl;
    d = a-b;
    d.printComplex();

     /* Add and assign two complex numbers */
    cout << "Add and Assign a and b : " << endl;
    d = a+=(b);
    d.printComplex();

     /* Subtract aand assign two complex numbers */
    cout << "Subtraction and Assign of a and b : " << endl;
    e = a-=(b);
    e.printComplex();

     /* Multiply two complex numbers */
    cout << "Multiplication of a and b : " << endl;
    f = a*b;
    f.printComplex();

     /* Divide two complex numbers */
    cout << "Division of a and b : " << endl;
    g = a/b;
    g.printComplex();

     /*Print out lenght // How do I implement the lenght function ???
      cout<<"Lenght of complex numbers"<<endl;
      return */

      Length = g.Length
      {
      cout<<"Output the lenght of two vecrors"<<Length<<endl;
      }
      
       }
Last edited on
not sure if the variables are defined
Honestly, I don't think that I can help you. How can I explain whether something is there or not?
Line 9: remove. With both a default constructor and a constructor where all arguments have defaults, it's ambiguous which one should be called.
Line 10: public should be followed by a colon, not a semicolon.
Line 13: ); should be ):
Line 36: You need a semicolon after all declarations.

Getr() and Geti(): These should be returning the real and imaginary parts of the class. Check what those values are named.

Lines 88-97. It's hard to know how to define the function without knowing what it's supposed to do. It's declared like a default constructor. Lines 91&92 look like they are trying to compute the dot function. Line 94 looks like it's computing the absolute value of the complex number. I suggest that you comment out this entire function. The only reason not to delete it is because you might want to use some of the code later.

Ideally, the Print() function would be replaced by a << operator, but let's leave that for now. You should change it to be a member function.

Okay, those are enough compiler errors for now. The basic problem here is that you wrote a lot of code before testing any of it. Here is a copy with most of the functionality and main program #if 'ed out. Get this working. Then move the #if 0 in main() down past the next section. Get the newly exposed section working and repeat the process until everything works. Keep in mind that at each step the any problems you have are NOT in the section that is #if'ed out.
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Header File Starts here 
class Complex
{
  private:
    int real;
    int imag;
    // aditional func here length/absolute value functuion...

public:
//Constructors
    Complex(int r = 0, int i = 0):
      real(r), imag(i)
    {
    };

//Destructor
    ~Complex();

//Operators
    Complex operator +(Complex & c);
    Complex operator +=(Complex & c);
    Complex operator -=(Complex & c);
    Complex operator /(Complex & c);
    Complex operator *(Complex & c);

//Setter
    void Setr(int r);
    void seti(int i);

//Getter         
    int Getr()
    {
	return real;

    }
    int Geti()
    {
	return imag;
    }

//Member Func ?? Not sure if necessary 
    void Print();
    int length();
};

//Source Code
// #include "HW4Header.h"

#include<iostream>
#include<cmath>

using namespace std;

void
Complex::Print()
{
    return cout << "Real Part is r" << r << "Imaginary Part is i" << i << endl;
// What does th print function really do??
}

/*Complex::Complex(int r, int i)
{ no need to define, already defined... 
	real=r; imag=i;
}*/

Complex::~Complex()
{
    //Destructor
}

//Main File starts here 


#if 0

Complex
Complex::operator +(Complex & c)	// plus operator
{
    Complex comp;
    comp.real = this->real + c.real;
    comp.imag = this->imag + c.imag;
    return comp;
}

/*Complex Complex operator += (Complex &c) //minus operator
        {
            Complex comp;
            comp.real = this->real - c.real;
            comp.imag = this->imag - c.imag;
            return comp;
        }*/
Complex
Complex::operator +=(Complex & c)	//Add aand assign operator
{
    Complex comp;
    comp.real = this->real += c.real;
    comp.imag = this->imag += c.imag;
    return comp;
}

Complex
Complex::operator -=(Complex & c)	//subtract and assign operator
{
    Complex comp;
    comp.real = this->real -= c.real;
    comp.imag = this->imag -= c.imag;
    return comp;
}

Complex
Complex::operator /(Complex & c)	//division operator
{
    Complex comp;
    comp.real = this->real / c.real;
    comp.imag = this->imag / c.imag;
    return comp;
}

Complex
Complex::operator *(Complex & c)	//multiplication operator
{
    Complex comp;
    comp.real = this->real * c.real;
    comp.imag = this->imag * c.imag;
    return comp;
}

double
Complex()			// How do I define the function properly here ??
{
    Complex comp;
    comp.real = this->real * c.real;
    comp.imag = this->imag * c.imag;

    return (sgrt(pow(r, 2) + pow(i, 2)));	 //not sure if the variables are defined 
//accurately here,  can I have two return statements here
    //return comp;
}
#endif

using namespace std;

int
main()
{
    Complex a(1, 3);		// assume (realpart, imaginary)
    Complex b(2, 4);
    Complex c;
    Complex d;
    Complex e;
    Complex f;
    Complex g;

    cout << "a is ";
    a.Print();
    cout << "b is ";
    b.Print();
    cout << "c is ";
    c.Print();
    
#if 0    
    cout << "Setting first complex number " << endl;
    a.setr();
    cout << "Setting second complex number " << endl;
    b.seti();

    /* Adding two complex numbers */
    cout << "Addition of a and b    : " << endl;
    c = a + b;
    c.printComplex();

    /* Subtracting two complex numbers */
    cout << "Subtraction of a and b : " << endl;
    d = a - b;
    d.printComplex();

    /* Add and assign two complex numbers */
    cout << "Add and Assign a and b : " << endl;
    d = a += (b);
    d.printComplex();

    /* Subtract aand assign two complex numbers */
    cout << "Subtraction and Assign of a and b : " << endl;
    e = a -= (b);
    e.printComplex();

    /* Multiply two complex numbers */
    cout << "Multiplication of a and b : " << endl;
    f = a * b;
    f.printComplex();

    /* Divide two complex numbers */
    cout << "Division of a and b : " << endl;
    g = a / b;
    g.printComplex();

    /*Print out lenght // How do I implement the lenght function ???
       cout<<"Lenght of complex numbers"<<endl;
       return */

    Length = g.Length {
	cout << "Output the lenght of two vecrors" << Length << endl;
    }
#endif
    
}


Changed a few things, having an issue with the class definition not sure whats wrong there

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Header File Starts here 

#include<iostream>
#include<cmath>

using namespace std;

class Complex // not sure why this error show up here ??
{
  private:
    double real;
    double imag;
    
    // aditional func here length/absolute value functuion...

public:
//Constructors
    Complex();
i    Complex (double r, double i);

//Destructor
    ~Complex();

//Operators
    Complex operator +(Complex & c);
    Complex operator +=(Complex & c);
    Complex operator -=(Complex & c);
    Complex operator /(Complex & c);
    Complex operator *(Complex & c);

//Setter
    void SetR(int r){real=r;}
    void SetI(int i){imag=i;}

//Getter         
    double GetR(){return real;}
    double GetI(){return imag;}

//Member Func ?? Not sure if necessary 
    void Print();
    double length();
}; // HEADER ENDS HERE

//Source Code
// #include "HW4Header.h"

//#include<iostream>
//#include<cmath>

//using namespace std;

//void Complex::Print()
{
    //return cout<<"Real Part is r"<<real<<"Imaginary Part is i"<<imag<<endl; // What does th print function really do??
}

/* Complex::Complex(double r, double i)
      
    {
      real =r; 
      imag =i;
    }*/

/*Complex::Complex(int r, int i)
{ no need to define, already defined... 
	real=r; imag=i;
}*/

Complex::~Complex()
{
    //Destructor
}

//Main File starts here 



//Default
Complex::Complex()
{

	real = 0;
	imag = 0;
}

Complex::Complex(double r, double i)
{

	real = r;
	imag = i;
}

Complex Complex::operator +(Complex &c)	// plus operator
{
   
    return (c.getR()+getR, c.getI()+getI());
}

Complex Complex::operator +=(Complex &c)	//Add aand assign operator
{
    real += c.getR(); imag +=c.getI;
    return *this;
}

Complex Complex::operator -=(Complex &c)	//subtract and assign operator
{
   real -= c.getR(); imag -= c.getY();
    return *this;
}

Complex Complex::operator /(Complex & c)	//division operator
{
    return Complex (c.getR()/getR(),c.getI()/getI() );
}

Complex Complex::operator *(Complex & c)	//multiplication operator
{
    return Complex (c.getR()*getR(),c.getI()*getI() );
}

double Complex::length()
{
    //comp.real = this->real * c.real;
    //comp.imag = this->imag * c.imag;

    return (sgrt(pow(real, 2) + pow(imag, 2)));
}

void Complex::Print()
{
   cout<<"Real Part ="<<getR()<<endl;
   cout<<"Imaginary PArt ="<<getI()<<endl;
   cout<<"Lenght ="<<length()<<endl;
}
//#endif

//using namespace std;

//#if 0

int main()
{
    Complex a(1, 3);		// assume (realpart, imaginary)
    Complex b(2, 4);
    Complex c;
    Complex d;
    Complex e;
    Complex f;
    Complex g;

    cout << "a is ";
    a.Print();
    cout << "b is ";
    b.Print();
    cout << "c is ";
    c.Print(); 
    
//#if 0    
   /* cout << "Setting first complex number " << endl;
    a.setr();
    cout << "Setting second complex number " << endl;
    b.seti();*/

    /* Adding two complex numbers */
    cout << "Addition of a and b    : " << endl;
    c = a + b;
    c.Print();

    /* Subtracting two complex numbers */
    cout << "Subtraction of a and b : " << endl;
    d = a - b;
    d.Print();

    /* Add and assign two complex numbers */
    cout << "Add and Assign a and b : " << endl;
    d = a += (b);
    d.Print();

    /* Subtract aand assign two complex numbers */
    cout << "Subtraction and Assign of a and b : " << endl;
    e = a -= (b);
    e.Print();

    /* Multiply two complex numbers */
    cout << "Multiplication of a and b : " << endl;
    f = a * b;
    f.Print();

    /* Divide two complex numbers */
    cout << "Division of a and b : " << endl;
    g = a / b;
    g.Print();

    /*Print out lenght // How do I implement the lenght function ???
       cout<<"Lenght of complex numbers"<<endl;
       return */

   // Length = g.Length {
//	cout << "Output the lenght of two vecrors" << Length << endl;
  //  }
//#endif
    
}


Line 19: get rid of the i in column 1.

Line 36: You want to return a Complex object.

Line 53,55: If you're going to comment out the function, you need to comment out the {}

Line 96,107,113,118,131: Check your capitalization. Your function declaration says GetR(). Your function call is getr().

Line 96,101,113,118,132: Ditto for GetI().

Line 96: Your second GetR call needs ().

Line 101: Your GetI call needs ().

Line 107: Check your spelling. GetY() should be GetI().

Line 171: You haven't defined the - operator.

Note 1: These are all very obvious error that should have been pointed out by your compiler.

Note 2: It's not necessary to call getters within your class. Every member function of your class has access to the member variables of the class.
Last edited on
You define methods GetR() and GetI() but you call then with getR() and getI(). All names in C++ are case sensitive so you need to be consistent. I'd change to getR() and getI() because it involves fewer edits.

Line 96 should be return Complex(c.getR()+getR(), c.getI()+getI());

Line 101 should be real += c.getR(); imag +=c.getI();

Line 107: what is getY()?

There's a pattern here: GetR(), getR, getY(), these are all just incorrect method names. Programming requires you to be very specific, much more so that human interactions. Pay close attention to names and be sure to us the right names, the right way.

Lines 111 to 119: the math is wrong. You should review how to multiply and divide complex numbers.

Line 126: sgrt should be sqrt While this code works fine, you should know that it's faster to square a number by multiplying it by itself. The pow() function works with any real numbers and is thus pretty complex. So this would be better written as return (sqrt(real*real + imag*imag));

Line 171: You haven't defined operator- yet.

Thank you all, i think I just got a little impatient with this one still working on it
Topic archived. No new replies allowed.