Pages

C++ Programming Tutorials - 19 (C++ Objects Memory Allocation & using Arrays in Classes)

tut23.cpp=
// This tut contains C++ Objects Memory Allocation & using Arrays in Classes
#include<iostream>
using namespace std;

class Store {
    int itemId[50];
    int itemPrice[50];
    int counter;

    public:

    void initCounter(void){counter=0;}
    void setPrice();
    void displayPrice();    
};

void Store :: setPrice(void){
    cout<<"Enter itemId of an item "<<counter +1<<":"<<endl;
    cin>>itemId[counter];
    cout<<"Enter price of an item "<<counter +1<<":"<<endl;
    cin>>itemPrice[counter];
    counter++;
    cout<<endl;
}

void Store :: displayPrice(void)
{
    for (int i = 0i < counteri++)
    {
        cout<<"Displaying price of item  with itemId "<<itemId[i]<<" is : "<<itemPrice[i]<<endl;
        cout<<endl;
    }    
}

int main()
{
    Store shop;
    shop.initCounter();
    // shop.setPrice();
    // shop.setPrice();
    // shop.setPrice();
    // shop.displayPrice();
    int no_of_items;
    cout<<"Enter no of items shop :"<<endl;
    cin>>no_of_items;
    for (int i = 0i < no_of_itemsi++)
    {
        shop.setPrice();
        cout<<endl;
    }
    
    shop.displayPrice();

    return 0;
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Top 10 Python Packages Every Developer Should Learn

Top 10 Python Packages Every Developer Should Learn There are more than  200,000 Python packages  in the world (and that’s just counting tho...