memory allocation and using arrays in classes

 //the statement that jab class likhte h to bilkul bi memory nhi khai jati and

//objects bnane pe hi memory allocate hoti hai is not completlety true (partially true)
/*jab hum class bnate hai to kuch memory (for example functions ki) sare functions ke liye same allocate ki
jati hai
otherwise har object ke liye alg memory allot hogi*/


#include<iostream>
using namespace std;


class shop {
    int itemId[100];
    int itemPrice[100];
    int counter;

    public:

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

};

void shop :: setPrice(void){
    cout<<"enter id of your item no"<< counter + 1 <<endl;
    cin>>itemId[counter];
    cout<<"enter price of your item"<<endl;
    cin>>itemPrice[counter];
    counter++;
}

void shop :: displayPrice(void){

    for (int i=0; i<=counter; i++){
        cout<<"the price of item with item id"<<itemId[i]<<" is "<<itemPrice[i]<<endl;
    }
}
int main(){
    shop dukaan;
    dukaan.initCounter();
    dukaan.setPrice();
    dukaan.setPrice();
    dukaan.setPrice();
    dukaan.displayPrice();
    return 0;
}

Comments

Popular posts from this blog

Function Overloading in C++

oops recall, basic operations and nesting of member functions

OBJECT ORIENTED PROGRAMMING, classes public and private access modifiers (syntax)