首页 > 编程语言 >C++编程练习||Account类:创建一个名为Account的类,银行可以使用它表示客户的银行帐户||Invoice类:建一个名为Invoice(发票)的类,商店可以使用它表示店中售出一款商品的一张

C++编程练习||Account类:创建一个名为Account的类,银行可以使用它表示客户的银行帐户||Invoice类:建一个名为Invoice(发票)的类,商店可以使用它表示店中售出一款商品的一张

时间:2024-02-02 19:32:10浏览次数:25  
标签:Account string int price sale Invoice 名为

1.Account类

题目:创建一个名为Account的类,银行可以使用它表示客户的银行帐户。

这个类应该包括一个类型为double的数据成员,表示帐户余额。这个类提供一个构造函数,它接受初始余额并用它初始化数据成员。这个构造函数应当确认初始余额的有效性,保证它大于或等于0。否则,余额应设置为0。成员函数credit将一笔金额加到当前余额中。debit将从这个Account中取钱,并保证取出金额不超过此Account的余额。如果不是这样,余额不变,函数打印一条信息,指出”Debit amount exceeded account balance.”。成员函数getBalance将返回当前余额。主函数中,创建一个账户,余额为50元,输入取钱的金额,输出相应结果。再创建一个余额为负数的账户,输出相应信息。

注意:输出数据的第一行之前有一个空行

#include <iostream>

using namespace std;

class Account {
	private:
		double save;

	public:
		Account(double c_save) {
			if(c_save >= 0.0) {
				save = c_save;
			} else {
				cout << "\nError:Initial balance cannot be negative." << endl << endl;
				save = 0.0;
			}
		}
		
		void debit(double money) {
			if ((save - money) >= 0) {
				save -= money;
			} else {
				cout << "Debit amount exceeded account balance." << endl << endl;
			}
		}
		
		void credit(double money) {
			save += money;
		}
		
		double getBalance() {
			return save;
		}
		
};

int main() {
	Account account1(50.0);

	double withdrawalAmount;
//    cout<<"\nEnter withdrawal amount for account1:";
	cin>>withdrawalAmount;
	cout<<"\nattempting to subtract "<<withdrawalAmount<<" from account1 balance\n";

	account1.debit(withdrawalAmount);
	cout<<"account1 balance:$"<<account1.getBalance()<<endl;

	Account account2(-2);
	cout<<"account2 balance :$"<<account2.getBalance()<<endl;
	account2.credit(156.4);
	cout<<"\nattempting to add "<<156.4<<" to account2\n";
	cout<<"account2 balance :$"<<account2.getBalance()<<endl;

	return 0;
}

运行结果:

C++编程练习||Account类:创建一个名为Account的类,银行可以使用它表示客户的银行帐户||Invoice类:建一个名为Invoice(发票)的类,商店可以使用它表示店中售出一款商品的一张_构造函数

C++编程练习||Account类:创建一个名为Account的类,银行可以使用它表示客户的银行帐户||Invoice类:建一个名为Invoice(发票)的类,商店可以使用它表示店中售出一款商品的一张_成员函数_02

C++编程练习||Account类:创建一个名为Account的类,银行可以使用它表示客户的银行帐户||Invoice类:建一个名为Invoice(发票)的类,商店可以使用它表示店中售出一款商品的一张_构造函数_03

2.Invoice类

建一个名为Invoice(发票)的类,商店可以使用它表示店中售出一款商品的一张发票。

一个Invoice对象应当包括作为数据成员的4部分信息:零件号(类型string)、零件描述(类型string)、售出量(类型int)和单价(类型int)。

这个类必须具有一个初始化前述的4个数据成员的构造函数。对每个数据成员都提供一个设置函数和一个获取函数。

此外,还要提供一个名为getInvoiceAmount的成员函数,计算发票额(即售出量与单价乘积),并以int类型返回该值。如果售出量是负数,那么应该设置为0。如果单价是负数,那么应该设置为0.

#include <iostream>

#include <string>

using namespace std;


class Invoice {

	private:

  string id;

  string des;

  int sale;

  int price;


	public:

  Invoice(string a_id, string a_des, int a_sale, int a_price) {

  	id = a_id;

  	des = a_des;

  	sale = a_sale;

  	price = a_price;

  }

  

  string getPartNumber() {

  	return id;

  }

  

  string getPartDescription() {

  	return des;

  }

  

  int getQuantity() {

  	return sale;

  }

  

  int getPricePerItem() {

  	return price;

  }

  

  int getInvoiceAmount() {

  	return sale * price;

  }

  

  void setPartNumber(string a_id) {

  	id = a_id;

  }

  

  void setPartDescription(string a_des) {

  	des = a_des;

  }

  

  void setQuantity(int a_sale) {

  	if (a_sale > 0) {

    sale = a_sale;

  	} else {

    sale = 0;

    cout << endl << "quantity cannot be negative. quantity set to 0." << endl;

  	}

  }

  

  void setPricePerItem(int a_price) {

  	if (a_price > 0) {

    price = a_price;

  	} else {

    price = 0;

  	}

  }


};

int main() {

	// create an Invoice object

	Invoice invoice("12345", "Hammer", 100, 5);


	// display the invoice data members and calculate the amount

	cout << "Part number: " << invoice.getPartNumber() << endl;

	cout << "Part description: " << invoice.getPartDescription() << endl;

	cout << "Quantity: " << invoice.getQuantity() << endl;

	cout << "Price per item: $" << invoice.getPricePerItem() << endl;

	cout << "Invoice amount: $" << invoice.getInvoiceAmount() << endl;


	// modify the invoice data members

	invoice.setPartNumber( "123456" );

	invoice.setPartDescription( "Saw" );

	invoice.setQuantity( -5 ); // negative quantity, so quantity set to 0

	invoice.setPricePerItem( 10 );

	cout << "\nInvoice data members modified.\n";


	// display the modified invoice data members and calculate new amount

	cout << "Part number: " << invoice.getPartNumber() << endl;

	cout << "Part description: " << invoice.getPartDescription() << endl;

	cout << "Quantity: " << invoice.getQuantity() << endl;

	cout << "Price per item: $" << invoice.getPricePerItem() << endl;

	cout << "Invoice amount: $" << invoice.getInvoiceAmount() << endl;

	return 0; // indicate successful termination

} // end main

运行结果:

C++编程练习||Account类:创建一个名为Account的类,银行可以使用它表示客户的银行帐户||Invoice类:建一个名为Invoice(发票)的类,商店可以使用它表示店中售出一款商品的一张_构造函数_04



标签:Account,string,int,price,sale,Invoice,名为
From: https://blog.51cto.com/u_16532251/9561212

相关文章

  • 在写布局样式的时候,什么时候命名为area,什么时候为container,什么时候为wrapper,什么时
    在编写布局样式时,对于类名的选择如area、container、wrapper和box等具有语义的名称是非常重要的,它们可以帮助开发者和维护者更好地理解HTML结构与功能。以下是一些最佳实践以及何时使用这些类名的理由:container:通常用于包裹整个页面或特定区块的主要内容容器。理由:这......
  • 解决centos7修改网卡名为eth0仍显示ens33的问题
    1.进入/etc/sysconfig/network-scripts修改网卡配置文件中的DEVICE=与NAME=参数为eth0保存退出后再修改网卡配置文件名mvifcfg-ens33ifcfg-eth02.重新生成grub2文件编辑/etc/default/grub配置文件,在GRUB_CMDLINE_LINUX这个参数后面加入:net.ifnames=0biosdevnam......
  • acme.sh 签发证书如果提示 Error creating new account 试试这个解决方法
    24年1月18日用 acme.sh命令签发ssl证书,使用的--issue参数。acme.sh--issue--dnsdns_dp-dxxx.com-dwww.xxx.com--force会提示错误:Error,cannotgetdomaintoken"type":"dns-01","url":"https://acme.zerossl.com/v2/DV90/chall/-qe......
  • 为文件一键命名为其md5值
    为文件一键命名为其md5值不知道怎么给文件命名?一键命名为其md5不就是了!单文件拖拽版无需启动,直接将文件拖拽到.bat文件上@echooffsetlocalenabledelayedexpansion::获取文件完整路径set"file_path=%~1"::检查文件是否存在ifnotexist"!file_path!"(echoF......
  • HFM account维度
    account维度,顾名思义,是记录科目使用的,核算系统使用不同的科目记录不同的业务数据,科目在hfm系统中分资产、负债、收入、费用、权益,flow,balance,lable等,不同的科目具有不同的属性,其一是向上汇总的时候,计算规则,其二是日记账调整时候借方贷方+-不同,TypeParentAccountAccountType......
  • NullInjectorError: R3InjectorError(AccountModule)[ModalHelper -> NzModalService
    异常: 出现异常背景:增加包@delon/chart后运行项目出现此异常 解决方法:造成的原因是出现了不同版本的ng-zorro-antd方式一:删除项目下node_modules、package-lock.json或yarn.lock文件后重新安装依赖方式二:找到package.json中定义以外的ng-zorro-antd版本删除,此处发现......
  • SQLC - ERROR: relation "accounts" does not exist
    Copiedtheexampleofsqlc.yamlgeneratedby'sqlcinit'from https://docs.sqlc.dev/en/stable/tutorials/getting-started-postgresql.html#.Changeditasfollowing:version:"2"sql:-engine:"postgresql"queries:&quo......
  • 对扩展名为py的文件进行打包成exe执行文件
    第一步:我们使用的打包工具是pyinstaller,需要将这个包安装上,安装命令:pipinstallpyinstaller第二步:需要有一个执行所需功能的Python脚本,根据程序功能需要可以有一个程序中变量的读取的配置文件default.cfg。 第三步:执行打包        执行命令:pyinstal......
  • ServiceAccount ClusterRole ClusterRoleBinding
    RoleBinding的作用是把ServiceAccount绑定到Role上,Role规定了可以对资源做的操作,把ServiceAccount绑定到Role上就表示拿到这个ServiceAccount的程序就有了权限对资源做这些操作。当然,有ClusterRole和ClusterRoleBinding,ClusterRole可以在包括所有NameSpce和集群级别的资源或非资......
  • 如何在voj上用自己的账号提交(Submit with your own account)
    用voj交题的时候大部分情况都是voj用一个虚拟的bot账号帮你提交到对应题目所在的oj上进行判题但有些oj平台并不喜欢这种方式,它想让用户去用自己真实的账号提交,比如pta,洛谷,计蒜客......在voj上用自己的账号提交的方法如下:拿pta举例:在上图中我们发现我们需要一个叫做PTASes......