how to call function of a class in other function of the same class in c++

this is my 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
#include <iostream> 
#include <conio.h>
using namespace std;
class complex
{
	float num1,num2,num3,num4;
public:
	complex()
	{
		num1=0;num2=0;num3=0;num4=4;
	}
	complex(float,float,float,float);
	void add();
	void subtract( );
	void multiply();
	void print();
};
 complex::complex(float a,float b,float c,float d)
{
	
	num1=a;
	num2=b;
	num3=c;
	num4=d;
 
 }
void complex::add()
{
	num1+num3;
	cout<<" + i";
	num2+num4;
}
void complex::subtract()
{
	num3-num1;
	cout<<" + i";
	num4-num2;
}
void complex::multiply()
{
	num1*num3;
	cout<<" + i";
	num1*num4;
	cout<<" + i";
    num2*num3;
	cout<<"-";
    num2*num4;
}
void complex:: print()
{
	cout<<"The addition is :"<<endl;
	cout<<add()<<endl;
	cout<<"The subtraction is :"<<endl;
	cout<<subtract()<<endl;
	cout<<"the multiplication is :"<<endl;
	cout<<multiply()<<endl;
}
int main()
{
	float a,b,c,d,e,f,g,h=0;
	cout<<"please enter the first real number. "<<endl;
	cin>>a;
	cout<<"please enter the second complex number ."<<endl;
	cin>>b;
	cout<<"please enter the 3rd real number. "<<endl;
	cin>>c;
	cout<<"please enter the 4th complex number ."<<endl;
	cin>>d;
	
	complex i (a,b,c,d);
	i.add();
	i.subtract();
	i.multiply();
	i.print();
	getch();
	return 0;
}




i want to call know wht should b the parameters of functions of class in the main.i want to call i.add, i.subtract...etc but i am having errors tht i dont know.. :(
Last edited on
Your math functions don't return a value. They need to return floats.
i dont know how to return 2 floats from a one function :(
i am totally noob
Maybe you add two more member variable (like result1/result2 (I'm convinced that you'll find better names for it)), store the results of your operations there, and then use getters to access this variables
i didnt understand :(
Example:
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
class complex
{
	float num1,num2,num3,num4;
	float result_real, result_im;
public:
	complex()
	{
		num1=0;num2=0;num3=0;num4=4;
	}
	complex(float,float,float,float);
	void add();
	void subtract( );
	void multiply();
	void print();

	float get_result_real() const { return result_real; }
	float get_result_im() const { return result_im; } 
};
...

void complex::add()
{
	result_real = num1+num3; // assuming 1/3 are real
	cout<<" + i";
	result_im = num2+num4; // assuming 2/4 are imaginary
}

...

	i.add();
	cout<<"add result: " << i.get_result_real() << " + " << i.get_result_im() << "i" << endl;

...
what ur program actually does?
for example,
Does the function add() will add

num1+num3 and return its value and

also add num2+num4 and return its value

or its just add all the numbers num1+num2+num3+num4 and return its value?
I may be misunderstanding the intention. This would be my (simplified) version of the program.
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
#include <iostream>
#include <conio.h>

using namespace std;

class complex
{
    float real;
    float imag;
public:
    complex()
    {
        real=0;imag=0;
    }
    complex(float,float);
    void add(complex & );
    void print();
};

complex::complex(float a,float b)
{
    real=a;
    imag=b;
}

void complex::add(complex &rhs)
{
    real += rhs.real;
    imag += rhs.imag;
}

void complex:: print()
{
    cout << '(' << real;
    cout << " + ";
    cout << imag;
    cout << 'i' << ')';
}

int main()
{
    float a,b;

    cout << "Please enter the first complex number. " << endl;
    cout << "real part:      ";
    cin >> a;
    cout << "imaginary part: ";
    cin >> b;
    complex first(a, b);

    cout << "\nPlease enter the second complex number. " << endl;
    cout << "real part:      ";
    cin >> a;
    cout << "imaginary part: ";
    cin >> b;
    complex second(a, b);

    first.print();
    cout << endl;
    second.print();
    cout << endl;

    first.add(second);

    cout << "first + second = ";
    first.print();
    cout << endl;

    getch();
    return 0;
}
Topic archived. No new replies allowed.