Changing a variable in an object

Hello,
How can i change a variable that had been created through a constructor??

I have a Constructor1(FirstName, LastName, PhoneNumber);
and Constructor2(PhoneNumber);

I tried to modify just the phone number, but i think it creates a second Object and doesn't make it happened.
I tried to use pointers on constructors, but got lost !
can somebody just try to explain me this thing with a small example?

i was not able to find something through the different forum.
closed account (LNboLyTq)
Can you elaborate more on that?
Can you show your code ?
I assume that you're talking about changing the value of a data member of an object that's already been created?

If so, then you should add a method to the class, that allows calling code the change the value of that data member. Such methods are often known as "setter" methods.
Hey, thank you ! here is the code :
I just try to create a contact through a constructor, than modify one of its variable.
This is my Contact.h
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
#ifndef CONTACT_H
#define CONTACT_H

#include <iostream>

using namespace std;

class Contact{
	public:  
		//Constructors
		Contact(string fname, string lname, string phone, string address, string city, string state);
		Contact(string fname, string lname, string phone);
		
		//Accessor
		void setFirstName(string fname);
		void setLastName(string lname);
		void setPhone(string phone);
		void setAddress(string address);
		void setCity(string city);
		void setState(string state);
		string getFirstName();
		string getLastName();
		string getPhone();
		string getAddress();
		string getCity();
		string getState();

	private:
		//Variables
		string fname, lname, phone, address, city, state;
};
#endif 

Here is my Contact.cpp
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

#include "Contact.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

//Constructor with: fname, lname, phone, address, city, state.
Contact::Contact(string myFname, string myLname, string myPhone, string myAddress, string myCity, string myState){
	Contact::setFirstName(myFname);
	Contact::setLastName(myLname);
	Contact::setPhone(myPhone);
	Contact::setAddress(myAddress);
	Contact::setCity(myCity);
	Contact::setState(myState);
}
//Constructor with: lname, fname, phone.
Contact::Contact(string myFname, string myLname, string myPhone){
	Contact::setFirstName(myFname);
	Contact::setLastName(myLname);
	Contact::setPhone(myPhone);
}
//Accessor Setters
void Contact::setFirstName(string sFname){
	Contact::fname = sFname;
}
void Contact::setLastName(string sLname){
	Contact::lname = sLname;
}
void Contact::setPhone(string sPhone){
	Contact::phone = sPhone;
}
void Contact::setAddress(string sAddress){
	Contact::address = sAddress;
}
void Contact::setCity(string sCity){
	Contact::city = sCity;
}
void Contact::setState(string sState){
	Contact::state = sState;
}
//Accessor Getters
string Contact::getFirstName(){
	return Contact::fname;
}
string Contact::getLastName(){
	return Contact::lname;
}
string Contact::getPhone(){
	return phone;
}
string Contact::getAddress(){
	return address;
}
string Contact::getCity(){
	return city;
}
string Contact::getState(){
	return state;
}

And the main cpp file UseContact.cpp
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
#include "Contact.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(){	

	string fname;
	string lname;
	string address;
	string phone;
	string city;
	string state;
	
	Contact myContact1(fname, lname, phone, address, city, state);
	Contact myContact2(fname, lname, phone);
	
	char ch;
	int op;
	do{
		cout<<"\t\tMake your choice "<<endl;
		cout<<"\t-1- Add Contact"<<endl;
		cout<<"\t-2- List Contact"<<endl;
		cout<<"\t-3- Modify Contact"<<endl;
		cout<<"\t-4- Find Contact"<<endl;
		cout<<"\t-5- Quit"<<endl;
		cout<<"\t\tChoice :> ";
			cin>>op;
		cin.ignore();
		switch(op){ 
			case 1:  //Calling Adding function
				cout<<"\t\t----- Add Contact -----"<<endl;
					cout<<"First Name :> ";
						getline(cin,fname);
					cout<<"Last Name :> ";
						getline(cin,lname);
					cout<<"Phone # :> ";
						getline(cin,phone);
					cout<<"Address :> ";
						getline(cin,address);
					cout<<"city :> ";
						getline(cin,city);
					cout<<"state :> ";
						getline(cin,state);
					
					myContact1.setFirstName(fname);
					myContact1.setLastName(lname);
					myContact1.setPhone(phone);
					myContact1.setAddress(address);
					myContact1.setCity(city);
					myContact1.setState(state);
					
				break;
			
			case 2: //calling listing function
				cout<<"\t\t----- List Contact -----"<<endl;
					cout<<myContact1.getFirstName()<<" "<<myContact1.getLastName()<<"\t"<<myContact1.getPhone()<<endl;
					cout<<myContact1.getAddress()<<endl;
					cout<<myContact1.getCity()<<", "<<myContact1.getState()<<endl;
					
			
				break;
			
			case 3: //calling modifying function
				cout<<"\t\t----- Modify Contact -----"<<endl;
					cout<<"New First Name:> ";
						getline(cin,fname);
					cout<<"New Last Name :> ";
						getline(cin,lname);
					cout<<"New Phone # :> ";
						getline(cin,phone);
						
					myContact2.setFirstName(fname);
					myContact2.setLastName(lname);
					myContact2.setPhone(phone);
				break;
			
			case 4: //calling finding function
				
				break;
			case 5: //quit the program
					cout<<"\t\tQuit The programm!!!"<<endl;
					exit(1);
			
			default: //if the user tape the wrong letter
					cout<<"\t\tWrong Choice !!!"<<endl;
				break;
		}
		cout<<"\tWould you like to continue the program??? (Y/n) :> ";
			cin>>ch;
		
	}
	while(ch=='y' || ch=='Y');
	
	return 0;
}
Topic archived. No new replies allowed.