c++定义简单的个人所得税类
时间: 2023-12-30 21:05:12 浏览: 86
用C#写的个人所得税计算器
4星 · 用户满意度95%
个人所得税类可定义为一个包含以下属性和方法的类:
属性:
- salary:工资收入
- bonus:奖金收入
- deduction:扣除项(例如社保、公积金等)
- tax_rate:税率
方法:
- \_\_init\_\_(self, salary, bonus, deduction):构造函数,初始化属性值
- calculate_tax(self):计算个人所得税,返回税额
- set_tax_rate(self, tax_rate):设置税率
- get_salary(self):获取工资收入
- get_bonus(self):获取奖金收入
- get_deduction(self):获取扣除项
- get_tax_rate(self):获取税率
示例代码:
```
class PersonalIncomeTax:
def __init__(self, salary, bonus, deduction):
self.salary = salary
self.bonus = bonus
self.deduction = deduction
self.tax_rate = 0.1 # 默认税率为10%
def calculate_tax(self):
total_income = self.salary + self.bonus
taxable_income = total_income - self.deduction
tax = taxable_income * self.tax_rate
return tax
def set_tax_rate(self, tax_rate):
self.tax_rate = tax_rate
def get_salary(self):
return self.salary
def get_bonus(self):
return self.bonus
def get_deduction(self):
return self.deduction
def get_tax_rate(self):
return self.tax_rate
```
使用示例:
```
# 创建个人所得税对象
pit = PersonalIncomeTax(salary=10000, bonus=5000, deduction=2000)
# 获取工资、奖金、扣除项、税率
print(pit.get_salary()) # 10000
print(pit.get_bonus()) # 5000
print(pit.get_deduction()) # 2000
print(pit.get_tax_rate()) # 0.1
# 设置税率为20%
pit.set_tax_rate(0.2)
# 计算个人所得税
tax = pit.calculate_tax()
print(tax) # 2600.0
```
阅读全文