Answer to Question #323889 in C++ for khan

Question #323889

We want to create a class of Product that contains multiple private data members such as

 quantity: An integer that holds a count value.

 objCount: A static integer that holds that count of objects.

 serialNo: An integer that holds the serial number of objects of a specific product (assume

the single object of Product class).

 Define a constructor that can accept two arguments i.e., totalQuantityOfProduct and

serialNumberOfProduct; and assign it to the respective data members of the class.

Moreover, the static member be shall be initialized from the outside of the class with zero

by using scope resolution operator.

 Define operator = that add the value of quantity to the left hand operand. i.e. c2=c1 (the

quantity of object c2 shall be incremented with the quantity of c1).

 Define unary operator - that inverts the value of quantity for product class and should

allow the statements like c1 -= 4;

 Execute the provided testCases.


1
Expert's answer
2022-04-05T09:30:20-0400
#include <iostream>

using namespace std;

class Product
{
	int quantity;
	static int Objcount;
	int serialNo;
public:
	Product():quantity(0), serialNo(0){ ObjIncrement(); }
	Product(int totalQuantityOfProduct, int serialNumberOfProduct)
	:quantity(totalQuantityOfProduct), serialNo(serialNumberOfProduct)
	{
		ObjIncrement();
	}

	Product(Product& p)
	{
		quantity = p.quantity;
		serialNo = p.serialNo;
		ObjIncrement();
	}
	Product& operator= (Product& p)
	{
		quantity = p.quantity;
		serialNo = p.serialNo;
		return *this;
	}
	Product& operator-= (int value)
	{
		quantity -= value;
		return *this;
	}
	static void ObjIncrement() { Objcount++; }
	void Display()
	{
		cout << "\nProduct quantity: " << quantity
			<< "\nProduct serialNo: " << serialNo
			<< "\nProduct Objcount:" << Objcount;
	}
};

int Product::Objcount = 0;

int main()
{
	Product a(5, 123456789);
	a.Display();
	Product b = a;
	b.Display();
	b -= 3;
	b.Display();
}



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
APPROVED BY CLIENTS