1.面积类
设计一个 JX类表示矩形,矩形的长和宽作为其两个数据成员。编写一个求矩形面积的函数area(),并将它声明为JX的一个友元函数。利用area()求出该矩形的面积。
主函数、运行结果、源代码如下:
#include<iostream>
using namespace std;
class JX
{
friend int area(JX j);
private:
int x,y,z;
public:
JX(int a,int b)
{
x=a;
y=b;
}
void print()
{
cout<<"length:"<<x<<endl;
cout<<"width:"<<y<<endl;
}
};
int area(JX j)
{
int a,b;
a=j.x;
b=j.y;
return a*b;
}
int main()
{
JX j(12,5);
j.print();
int i;
i=area(j);
cout<<"area:"<<i<<endl;
}
2.工资类的计算
生成一个GZ类表示工资,用静态成员表示个人所得税占工资比例。类的每个成员包含一个私有数据成员 money,表示当月工资。提供一个 calSDS()成员函数,计算个人应交所得税,并从money中扣除。
个人所得税税率对照表
实例化三个不同的 GZ对象 people1、people2和 people3,工资分别为 1200.0、5600.0和 480000。分别计算三人实际应发的工资数并打印结果。
(1)用静态数据成员表示个人所得税占工资的比例;
(2)编写计算个人应交所得税成员函数calSDS();
(3)生成GZ类的三个对象;
(4)对三个对象分别计算实际应发的工资数;
(5)显示程序结果。
#include<iostream>
using namespace std;
class GZ
{
private:
float money;
public:
void calSDS();
static float SDS1;
static float SDS2;
static float SDS3;
static float SDS4;
static float SDS5;
static float SDS6;
static float SDS7;
static float SDS8;
static float SDS9;
GZ(float x)
{
money=x;
}
};
float GZ::SDS1=0.05;
float GZ::SDS2=0.10;
float GZ::SDS3=0.15;
float GZ::SDS4=0.20;
float GZ::SDS5=0.25;
float GZ::SDS6=0.30;
float GZ::SDS7=0.35;
float GZ::SDS8=0.40;
float GZ::SDS9=0.45;
void GZ::calSDS()
{
float x=0.0;
cout<<"这个人工资:"<<money<<endl;
x=money-800.0;
float SDS=0.0;
if(x>100000)
{
SDS=(x-100000)*SDS9;
x=100000;
}
if(x>80000)
{
SDS=SDS+(x-80000)*SDS8;
x=80000;
}
if(x>60000)
{
SDS=SDS+(x-60000)*SDS7;
x=60000;
}
if(x>40000)
{
SDS=SDS+(x-40000)*SDS6;
x=40000;
}
if(x>20000)
{
SDS=SDS+(x-20000)*SDS5;
x=20000;
}
if(x>5000)
{
SDS=SDS+(x-5000)*SDS4;
x=5000;
}
if(x>2000)
{
SDS=SDS+(x-2000)*SDS3;
x=2000;
}
if(x>500)
{
SDS=SDS+(x-500)*SDS2;
x=500;
}
if(x>0)
{
SDS=SDS+(x-0)*SDS1;
x=0;
}
cout<<"这个人应该缴的税是:"<<SDS<<endl;
cout<<"这个人的实际工资是:"<<money-SDS<<endl;
}
int main()
{
GZ people1(1200.0),people2(5600.0),people3(480000);
cout<<"First people: "<<endl;
people1.calSDS();
cout<<"Second people: "<<endl;
people2.calSDS();
cout<<"Third people: "<<endl;
people3.calSDS();
return 0;
}