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;
}
运行结果:
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
运行结果: