my set value member function set data member values but the get function return default values

why does my get function return the default member values and not the values my
set function set data member values to?

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
  // comp.h

#ifndef COMP_H
#define COMP_H



class cComplex {
private:
		float m_frealpart1;
		float m_frealpart2;
		float m_fimagnry1;
		float m_fimagnry2;
		float m_frealadd;
		float m_fimagnryadd;
		float m_frealsub;
		float m_fimagnrysub;
public:
		cComplex( float = 0.0, float = 0.0,float = 0.0, float = 0.0);
		void setcompnum(float, float, float,float);
		void setaddcompnum();
		void setsubcompnum();
        inline float getrealpart1(){return m_frealpart1;}
		inline float getrealpart2(){return m_frealpart2;}
		inline float getimagnrypart1(){return m_fimagnry1;}
		inline float getimagnrypart2(){return m_fimagnry2;}
        inline float getimagnryadd(){return m_fimagnryadd;}
		inline float getimagnrysub(){return m_fimagnrysub;}
		inline float getrealadd(){return m_frealadd;}
		inline float getrealsub(){return m_frealsub;}

};


#endif
// comp.cpp
#include "stdafx.h"
#include "comp.h"
#include "math.h"
#include <iostream>

        cComplex::cComplex(float frealprt1,float fimagnryprt1,float frealprt2, float fimagnryprt2 ): m_frealpart1(frealprt1), m_frealpart2(frealprt2)
	                       ,m_fimagnry1(fimagnryprt1), m_fimagnry2(fimagnryprt2),m_frealadd(0),m_fimagnryadd(0),m_frealsub(0),m_fimagnrysub(0)
{
}

		void cComplex::setcompnum(float freal1,float fimagnry1,float freal2,float fimagnry2)
		{  
			using namespace std;

			m_frealpart1 = freal1;
			m_frealpart2 = freal2;
			m_fimagnry1  = fimagnry1;
			m_fimagnry2  = fimagnry2;

			cout<< m_frealpart1<<m_fimagnry1 <<m_frealpart2<<m_fimagnry2<<endl;
		}

		void cComplex::setaddcompnum()
		{   
			
              m_frealadd = m_frealpart1 + m_frealpart2;
		      m_fimagnryadd = m_fimagnry1 + m_fimagnry2;
		}

		void cComplex::setsubcompnum()
		{
			m_frealsub = m_frealpart1 - m_frealpart2;
		    m_fimagnrysub = m_fimagnry1  - m_fimagnry2;
		}
	
// complexnumbers.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include "comp.h"

int main()
{
	using namespace std;

	cComplex comNumber(54.0,36.0,25.0,27.0);
	cComplex cset;

		comNumber.setaddcompnum();
		comNumber.setsubcompnum();
		//getrealpart1();
		//getrealpart2();
		//getimagnrypart1();
		//getimagnrypart2();
		
		cout<<" the complex number added = "<<"("<< comNumber.getrealadd()<<","<<comNumber.getimagnryadd()<<"i)"<<endl;
		cout<<" the complex number subtracted = "<<"("<<comNumber.getrealsub()<<","<< comNumber.getimagnrysub()<<"i)"<<endl;

		cset.setcompnum(75.0,98.0,7.0,25.0);

		cout<<" the complex number added = "<<"("<< cset.getrealadd()<<","<<cset.getimagnryadd()<<"i)"<<endl;
		cout<<" the complex number subtracted = "<<"("<<cset.getrealsub()<<","<< cset.getimagnrysub()<<"i)"<<endl;


	cin.clear();
	cin.ignore(255,'/n');
	cin.get();
	
	return 0;
}
Should you be calling cset.setaddcompnum instead on line 96?

Edit: You might find the complex library useful
http://www.cplusplus.com/reference/complex/complex/
Last edited on
Topic archived. No new replies allowed.