classes

I'm writing a program to be able to accept information from a "customer" (program user) and then store it all before showing the customer what products our store offers and allowing them to purchase quantities of these products. However, this week I am supposed to use a customer class to replace the simple variables that handle customer details. Below is my customer detail code (not the whole code, just the part that's relevant to this), and I'm super confused as to how to use a class in this situation, and why that would even be helpful. Before anyone links the tutorial on classes on this forum I have read it, and it does not make a whole lot of sense. Any help would be greatly appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main()
{
	string firstname; // need to make 2 variables for the first and last name, the code automatically looks for a space to separate the variables
	string lastname;
	cout << "Enter your first and last name:" << endl;
	cin >> firstname >> lastname;
	string phonenumber; // single variable for a single number, used string instead of int because int can only handle an 8 digit number
	cout << "Enter your phone number with no spaces:" << endl;
	cin >> phonenumber;
	string address; // 7 part address, ie 123 South Shore Road, Miami, Florida 11111 - most common address type in existence.
	cout << "Enter your full address with no punctuation:" << endl;
	cin.ignore();
	getline(cin, address);
	string ccard; // string again, as it can handle multiple characters
	cout << "Enter your credit card number:" << endl;
	cin >> ccard;
	string expdate; // string as it can handle characters and non-numerical characters
	cout << "Enter your credit card expiration date in month/year format:" << endl;
	cin >> expdate;
	string cid; // could have used int but used string again just for homogeneity
	cout << "Enter your CID:" << endl;
	cin >> cid;
	cout << "Thank you for visiting our website today, " << firstname << " " << lastname << ". Below you will find a list of products available for purchase at this time.";
	cout << " Please enjoy your visit  on our website." << endl; // welcome message as required 


I was already given this string of code as a response to my original question:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class customer_info { /* Here we define our very own special type customer_info */
private: //Contain and prevent other from accidently or purposing modifying bypassing our functions below. Usually implement variables here.
    string firstname, lastname;
    string phoneNumber;
    string address; //Declare our member variables for our customers info
    string CCard;
    string expdate;
    string cid;
public: //Here we will have our public interface of which the user can pick and choose from. Usually implement specialized functions.
    void setfirstname(string newfirstname) { //Public function to set the first name.
        firstname = newfirstname; //Initialize our member function to a new name
    }
    string getfirstname() { //Public function to retrieve the first name of this particular customer.
        return firstname; //Return back to the user this customers name.
    }
};


I was wondering if anyone could explain how this code and the first section of code would work together(if at all)? Does one of these replace the other, vice versa, how do I do all of this - I'm still fairly confused on classes and could use any help you guys can offer.
Ok,
You do it like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
        customer_info cust;
	cout << "Enter your first and last name:" << endl;
	cin >> cust.firstname >> cust.lastname;
	cout << "Enter your phone number with no spaces:" << endl;
	cin >> cust.phonenumber;
	cout << "Enter your full address with no punctuation:" << endl;
	cin.ignore();
	getline(cin, cust.address);
	cout << "Enter your credit card number:" << endl;
	cin >> cust.CCard;
	cout << "Enter your credit card expiration date in month/year format:" << endl;
	cin >> cust.expdate;
	cout << "Enter your CID:" << endl;
        cin >> cust.cid;
	cout << "Thank you for visiting our website today, " << cust.firstname << " " << cust.lastname << ". Below you will find a list of products available for purchase at this time.";
	cout << " Please enjoy your visit  on our website." << endl; // welcome message as required  


Try to put as much information into a class, rather than leave it at global scope. Read a tutorial on object-oriented programming. This is a good page to learn it from:
http://www.tutorialspoint.com/cplusplus/cpp_object_oriented.htm

Does this help?
Good luck!
Last edited on
turk,
thanks for the reply! So if i'm using that string of code, am i making those variables as i receive them, and is that a class, or do i still need to make a class and assign that code above to public or private or whatever, or how does all of that work? Reading the link you gave me right now as well. Thanks!
Topic archived. No new replies allowed.