Answer to Question #320120 in C++ for khan

Question #320120

Create a class by the name of ‘Product’. Create the following private members

name(String), price(Int) and quantity(Int). Define a parameter-less and a

parameterized constructor. Define the following two member functions outside the class,

void getData()

static void counter()

static int countProduct()

The class would contain a static int ‘totalProduct’, having initial value equal to zero.

Each time an instance of the product class is created, the ‘counter()’ should be called.

The ‘counter()’ increments the static member by 1 value. Suppose I create 5 products,

when I execute the following code it should display 5.

Product::totalProducts;


1
Expert's answer
2022-03-29T05:19:13-0400
#include<iostream>
#include<string>

using namespace std;

static void counter();
static int countProduct();

class Product
{
	string name;
	int price;
	int quantity;
	
public:
	static int totalProduct;
	Product()
	{
		counter();
	}
	Product(string _name, int _price, int _quantity) 
	{
		counter(); 
	}
	void getData()
	{
		cout << "Please, enter the name of product: ";
		cin >> name;
		cout << "Please, enter the price of product: ";
		cin >> price;
		cout << "Please, enter the quantity of product: ";
		cin >> quantity;
	}
};

int Product::totalProduct = 0;
static void counter(){Product::totalProduct++;}
static int countProduct() {return Product::totalProduct;}

int main()
{
	Product a("chair",20,25);
	Product b;
	b.getData();
	Product c("sofa",500,2);
	Product d;
	d.getData();
	Product e("table", 300, 7);
	cout << "The total number of product is " << countProduct();
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog