Code should be done in C++ and be able to compile/run in Visual Studio 2012 or l
ID: 671729 • Letter: C
Question
Code should be done in C++ and be able to compile/run in Visual Studio 2012 or later. Thank in advance for your help!
Create two classes. The first holds customer data—specifically, a customer number and zip code. The second, a class for cities, holds the city name, state, and zip code. Additionally, each class contains a constructor that takes parameters to set the field values. Create a friend function that displays a customer number and the customer’s city, state, and zip
code. Write a brief main()function to test the classes and friend function. Save the file as Customer.cpp.
Explanation / Answer
Program:
#include <iostream>
using namespace std;
class cities; // forward declaration
class customerData
{
int cno;
string zipcode;
public:
customerData(int c, string z):cno(c),zipcode(z)
{
}
friend void display( customerData&, cities&);
};
class cities
{
string cityName;
string state;
string zipcode;
public:
cities(string cn,string st,string zip):cityName(cn),state(st),zipcode(zip)
{
}
friend void display( customerData&, cities&);
};
void display(customerData& cust, cities& address )
{
cout<<endl<<"id :"<<cust.cno;
cout<<endl<<"City :"<<address.cityName;
cout<<endl<<"State :"<<address.state;
cout<<endl<<"Zipcode:"<<address.zipcode;
}
int main()
{
int cn;
string zip;
string cit;
string stat;
cout<<endl<<"Enter cutomer number and zip code of the customer:";
cin>>cn>>zip;
customerData cust=customerData(cn,zip);
cout<<endl<<"Enter city name, state and zip code of the customer:";
cin>>cit>>stat>>zip;
cities address=cities(cit,stat,zip);
cout<<endl<<"Customer Details";
display(cust,address);
return 0;
}
Output:
Enter cutomer number and zip code of the customer:
121 655178
Enter city name, state and zip code of the customer:
Alabama coulmbia 655178
Customer Details
id :121
City :Alabama
State :coulmbia
Zipcode:655178
--------------------------------