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
25
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 understand it from your point of view. I read up on classes and well didn't really get them a while ago either. In the beginning classes seemed kind of tedious and well pointless for otherwise simple things. However, after I read up more and more and started to use them more and go more and more in depth I found them very important, useful, and overall awesome.

Classes pretty much allow you to define your own specialized type/object. With that specialized object you'd have special functions for the user. With that you can make specialized objects and use them multiple times, and pretty much everywhere without having to copy paste, except for the header file of course... Not only that but like I said using them multiple times, you could have an array of your special type so for this question an array of customer_info each storing their own specialized data. There especially great for thinks such as containers or just to store large amounts of important data. Think about the vector type in the STL. It is in class, particulary a template class but no matter it is in a class. And within that class it defines many functions for your ease and use just for you. Another thing would be if you wanted to make your own superINT class which could store HUGE, beyond 64 bit numbers in one or way or another depending on how you implenented it. Or what if you wanted to make your own specialized linked list class just for your self. Not only that but classes have super cool features such as inheritance, operator overloading, virtual functions, etc. With those you can customize your class even more. Thus with classes you can have your very own special type with its very own important, versatile purpose. Whilst classes may not seem a whole lot important right here, trust me they will be in the future and they will save from which may seem like tedious things to otherwise simple and cool things.

Anyways here how you would use a class in this particular situation.
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.
    }
};

NOTE:
You are going to have to implement more functions for the rest of the variables/info so just see what and how I did it.
Also make sure to make your class object in main or whatever function it will be present in order to use it. customer_info customer; and then use and access the functions within the class via the dot operator customer.setfirstname("some string");

Also I assume your in a c++ class so I'm not sure if he isn't allowing you to have these member variables public. Otherwise In that case change them to public and you won't even need these functions although this is the more not really correct in a sense but definitely more proper and safe way.

Anyways good luck man and I hope you finish the rest of this program in no time!
Last edited on
yeah I am in a c++ class, thank you so much for this answer and for explaining it a little more. That answer helped so much, I'm going to use the code you wrote out for me (again thank you so much for providing an example) and if it's ok post what my finished result looks like back here. He's pretty much allowing us to do whatever we want so long as we do the basic stuff, so this should work fine. Thanks again so much, best answer I've ever gotten on this page hands down!
Hey so does this not ask the customer for their first and last name? it works with what i already have set up and just puts the information I have into classes, or what?
Topic archived. No new replies allowed.