(Pointers to objects)
Define a class 'Item' that is used to store and display the information regarding the item
number and its price. Write a program to store and display the details of one
items by using both normal object and pointer to object separately. Display
appropriate message wherever necessary.
#include<iostream>
using namespace std;
class Item{
private:
int number;
float price;
public:
void inputData(){
cout<<"Enter the number of items: ";
cin>>number;
cout<<"Enter the price of the items: "; cin>>price;
}
void display(){
cout<<"Number of items is: "<<number<<"\nPrice of items is: "<<price<<endl;
}
};
int main(){
Item i1;
i1.inputData();
i1.display();
Item * i2;
i2 = &i1;
i2->display();
}
Comments
Leave a comment