Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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");

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote