I have a constructor initializing member data and I have a member function to set values

Im using a constructor to initialize member variables and I have a member function that set values I was thinking I could use either or but when I call set function to set values the values are set but when i call accesor functions the values passed in to the constructor are retrieved. Why is this?

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
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();
		float getrealpart1();
		float getrealpart2();
		float getimagnrypart1();
		float getimagnrypart2();
		float getimagnryadd();
		float getimagnrysub();
		float getrealadd();
		float getrealsub();
};// 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 * sqrt(-1.0)) + (m_fimagnry2 * sqrt(-1.0));
		}

		void cComplex::setsubcompnum()
		{
			m_frealsub = m_frealpart1 - m_frealpart2;
		    m_fimagnrysub = (m_fimagnry1 * sqrt(-1.0)) - (m_fimagnry2 * sqrt(-1.0));
		}
		 float cComplex::getrealpart1(){return m_frealpart1;}
		 float cComplex::getrealpart2(){return m_frealpart2;}
		 float cComplex::getimagnrypart1(){return m_fimagnry1;}
		 float cComplex::getimagnrypart2(){return m_fimagnry2;}
         float cComplex::getimagnryadd(){return m_fimagnryadd;}
		 float cComplex::getimagnrysub(){return m_fimagnrysub;}
		 float cComplex::getrealadd(){return m_frealadd;}
		 float cComplex::getrealsub(){return m_frealsub;}// complexnumbers.cpp : Defines the entry point for the console application.
//

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

int main()
{
	using namespace std;
	float temp1,temp2;

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

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

		comNumber.setcompnum(75.0,75.0,75.0,75.0);

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


	cin.clear();
	cin.ignore(255,'/n');
	cin.get();
	
	return 0;
}
Last edited on
Topic archived. No new replies allowed.