C++ if I had an array of classes named Customer and a function called CreateCust
ID: 3838020 • Letter: C
Question
C++
if I had an array of classes named Customer and a function called CreateCustomer with the following as its arguments:
(cName is declared in a header file as a private string member of the Customer class.)
void Customer::CreateCustomer()
{
cout << "Enter Customer Name: ";
cin >> cName;
}
Will I be able to access "cName" throughout different arrays? If so, how?
Further Clarification:
(My main would look like)
int main()
{
Customer x[3];
x[0].CreateCustomer();
x[1].CreateCustomer(); //how do I use the value of "cName" here for, lets say another function.
}
Explanation / Answer
Customer is class and x[3] is an array of objects of class Customer.
If cName is declared private , it cannot be accessed as x[0].cName; . If you want to access cName , you can use accessor method getCName() which is public method.
public:
string getCName()
{
return cName;
}
now you can access cName in main()
string name = x[0].getCName();
You can use the setCName() method to set the value of cName
public:
void setCName(string name)
{
cName = name;
}
main()
{
x[0].setCName("John Smith");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.