The code defines a Book class with member variables to store a book ID, price, and a static count variable. It includes member functions to set the book data, display the book data, and display the count of Book objects. Main creates 3 Book objects, sets their data, and calls the static count display function twice to show the incrementing number of objects.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
28 views2 pages
Static Member Function Code
The code defines a Book class with member variables to store a book ID, price, and a static count variable. It includes member functions to set the book data, display the book data, and display the count of Book objects. Main creates 3 Book objects, sets their data, and calls the static count display function twice to show the incrementing number of objects.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2
#include<iostream>
using namespace std;
class Book{ int bookid; float price; static int count; //static data member public:
void getdata(int i,float j)
{ bookid=i; price=j; ++count; //count the number of objects } void showdata() { cout<<"Book ID: "<<bookid<<"\t"; cout<<"Price: "<<price<<"\n"; } static void showcount() //Static member function { cout<<"Count of objects of class Book: "<<count<<"\n"; } };
int Book::count=0; // initializing the static data member to 0
int main() { Book B1, B2; B1.getdata(198, 2550.3); B2.getdata(174, 3756.89); Book::showcount(); //calling static member function B1.showdata(); B2.showdata(); Book B3; //another object of the class is created B3.getdata(345,5432.4); Book::showcount(); //calling static member function again B1.showdata(); B2.showdata(); B3.showdata(); return 0; }