Creating Shopping Page using Functions and Class – Objects in C++
Hello Learners,
In this tutorial, we are going to create a Shopping Page using C++ Language. This Program will definitely clear so many of your doubts regarding Class and Objects and their implementation in Functions. This program will include all kinds of functions, concept of global as well as local variables, different kinds of loops, switch statements, and implementation of class & object. Let us get our hands on them by this very practical example that we use in our daily lifestyle.
C++ Program to Create a Shopping Environment
In this program, we will first create a class named Item and then inside the class, we will declare some variables which will be private automatically. Variables will be itemcode and itemprice for item code and price of item respectively. Private means it can not be accessed by the outside world. Then, in the public domain, we will create signature/declaration of some member functions and that will be defined outside the class.
Also, inside the class, we will create an inline function of count variable. Why inline? Because this function only contains count=0, i.e. initialization of count variable. Count is the variable used for controlling the overall number of items.
Member functions are as follows:
- addItem for adding item in your cart
- removeItem for removing any item from your cart
- displayTotal for displaying the total price of all items
- displayItems for displaying all items present in your cart.
In some of the functions, we will use for loops to add, remove, display items. The variable i will control the loop and that will be declared globally because every function needs them. Sum and a are two other variables that will be containing the total amount and item code respectively. Both of them will be local variables because only one function( displayTotal and removeItem respectively) needs them.
Following it, the functions will be defined outside the class therefore scope resolution operators are there. It is basically the syntax.
Execution of the program starts from the Main function so at first, we will create an object order1 of class, and then associating with order1 we will call init function containing the initialization of count. A loop will be there to provide the shopping environment to the user. For adding, removing, and displaying the items we have to ask about it every time. do-while loop will be the one doing the job. The condition of the loop will be such that it should quit when the user will ask for it.
Inside the loop, there will be a switch-case statement so that only one case works when the user asks for it. You will understand it properly in the program.
Now, above is the explanation of the program, let’s see the source code:
#include<iostream> using namespace std; int i; class Item { int itemcode[50]; float itemprice[50]; int count; public: void init() { count=0; } void addItem(); void removeItem(); void displayTotal(); void displayItem(); }; void Item::addItem() { cout<<"Enter item code"; cin>>itemcode[count]; cout<<"Enter item price"; cin>>itemprice[count]; count++; } void Item::removeItem() { int rem; cout<<"Enter item code"; cin>>rem; for(i=0;i<count;i++) if(itemcode[i]==rem) itemprice[i]=0; } void Item::displayTotal() { int sum=0; cout<<"Your total bill is"; for(i=0;i<count;i++) sum=sum+itemprice[i]; cout<<" "<<sum; } void Item::displayItem() { for(i=0;i<count;i++) { cout<<itemcode[i]<<" "; cout<<itemprice[i]<<"\n"; } } int main() { Item order1; order1.init(); int a; do{ cout<<"\nWelcome to shopping site xyz..!!\n"; cout<<"Select one of the following: \n"; cout<<"press 1 to add item\n"; cout<<"press 2 to remove item\n"; cout<<"press 3 to display total price of item\n"; cout<<"press 4 to display all items\n"; cout<<"press 5 to quit\n"; cin>>a; switch(a) { case 1: order1.addItem(); break; case 2: order1.removeItem(); break; case 3: order1.displayTotal(); break; case 4: order1.displayItem(); break; case 5: break; } } while(a!=5); return 0; }
Here is the output for a better understanding:
Welcome to shopping site xyz..!! Select one of the following: press 1 to add item press 2 to remove item press 3 to display total price of item press 4 to display all items press 5 to quit 1 Enter item code:233 Enter item price:500 Welcome to shopping site xyz..!! Select one of the following: press 1 to add item press 2 to remove item press 3 to display total price of item press 4 to display all items press 5 to quit 1 Enter item code:567 Enter item price:1000 Welcome to shopping site xyz..!! Select one of the following: press 1 to add item press 2 to remove item press 3 to display total price of item press 4 to display all items press 5 to quit 3 Your total bill is 1500 Welcome to shopping site xyz..!! Select one of the following: press 1 to add item press 2 to remove item press 3 to display total price of item press 4 to display all items press 5 to quit 4 233 500 567 1000 Welcome to shopping site xyz..!! Select one of the following: press 1 to add item press 2 to remove item press 3 to display total price of item press 4 to display all items press 5 to quit 2 Enter item code:567 Welcome to shopping site xyz..!! Select one of the following: press 1 to add item press 2 to remove item press 3 to display total price of item press 4 to display all items press 5 to quit 4 233 500 567 0 Welcome to shopping site xyz..!! Select one of the following: press 1 to add item press 2 to remove item press 3 to display total price of item press 4 to display all items press 5 to quit 5 ---------------------------------------------------------------------------------
I hope it was easy enough to understand. Feel free to ask anything regarding this in the comment section. Also, please see my other blogs as well. It will clear more concepts.
Thank you..!!
Regards,
Isha Rani Prasad
Codesppedy Pvt Ltd
Leave a Reply