C++ creates a class named Invoice

Exercise 3.13

Specific requirements are as follows:

      1) (Invoice class) Create a class called Invoice (invoice), which can be used by the store to represent an invoice for a product sold in the store.

      2) An Invoice object should include 4 parts of information as data members: part number (type: string), part description (type: string), sales volume (type: int) and unit price (type: int).

      3) This class must have a constructor that initializes the aforementioned 4 data members, and provide a setting function and an obtaining function for each data member.

      4) Also provide a member function named getInvoiceAmount to calculate the invoice amount (that is, the product of sales volume and unit price), and return the value in int type. If the sales volume is negative, it should be set to 0; if the unit price is negative, it should be set to 0.

      5) Write a test program to demonstrate the performance of the Invoice class.

The specific code is implemented as follows:

1) (Invoice class) Create a class called Invoice (invoice), which can be used by the store to represent an invoice for a product sold in the store.

class Invoice
{

};

2) An Invoice object should include 4 parts of information as data members: part number (type: string), part description (type: string), sales volume (type: int) and unit price (type: int).

private:
	string num;//零件号
	string des;//零件描述
	int sales;//售出量
	int price;//单价
	int sum;//用来放总数

3) This class must have a constructor that initializes the aforementioned 4 data members, and provide a setting function and an obtaining function for each data member.

public:
		void setNum( string number )
		{
			num = number;
		}
		string getNum()
		{
			return num;
		}
		
		void setDes( string describe )
		{
			des = describe;
		}
		string getDes()
		{
			return des;
		}
		
		void setSales( int sales1)
		{
			sales = sales1;
		}
		int getSales()
		{
			return sales;
		}
		
		void setPrice( int price1 )
		{
			price=price1;
		}
		int getPrice()
		{
			return price;
		}
		

4) Also provide a member function named getInvoiceAmount to calculate the invoice amount (that is, the product of sales volume and unit price), and return the value in int type. If the sales volume is negative, it should be set to 0; if the unit price is negative, it should be set to 0.

int getInvoiceAmount()
		{
			if(sales < 0)
			{
				sales=0;
				sum=0;
			}
			else
			{
				return sum=price*sales;
			}
		}

 5) Write a test program to demonstrate the performance of the Invoice class.

int main()
{
	Invoice a;
	a.setNum("001");
	cout << a.getNum() << endl;
	
	a.setDes("sss");
	cout << a.getDes() << endl;
	
	a.setSales(66);
	cout << a.getSales() << endl;
	
	a.setPrice(500);
	cout << a.getPrice() << endl;
	
	cout << a.getInvoiceAmount() << endl;
	
	return 0;
}

The entire running code is as follows:

#include<iostream>
#include<string>

using namespace std;

class Invoice
{
	public:
		void setNum( string number )
		{
			num = number;
		}
		string getNum()
		{
			return num;
		}
		
		void setDes( string describe )
		{
			des = describe;
		}
		string getDes()
		{
			return des;
		}
		
		void setSales( int sales1)
		{
			sales = sales1;
		}
		int getSales()
		{
			return sales;
		}
		
		void setPrice( int price1 )
		{
			price=price1;
		}
		int getPrice()
		{
			return price;
		}
		
		int getInvoiceAmount()
		{
			if(sales < 0)
			{
				sales=0;
				sum=0;
			}
			else
			{
				return sum=price*sales;
			}
		}
		
	private:
	string num;//零件号
	string des;//零件描述
	int sales;//售出量
	int price;//单价
	int sum;//用来放总数

};

int main()
{
	Invoice a;
	a.setNum("001");
	cout << a.getNum() << endl;
	
	a.setDes("sss");
	cout << a.getDes() << endl;
	
	a.setSales(66);
	cout << a.getSales() << endl;
	
	a.setPrice(500);
	cout << a.getPrice() << endl;
	
	cout << a.getInvoiceAmount() << endl;
	
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_74287172/article/details/130351166