一、日期类
#include<iostream>
using namespace std;
class date
{
int year;
int month;
int day;
public:
date(int a=1, int b=1, int c=1)
{
if(b<0||b>12||c<0||c>31)
{
cout<<"您输入的参数有误,请重新输入月日:"<<endl;
cin>>b>>c;
}
year=a;
month=b;
day=c;
}
~date()
{
}
void display();
};
void date::display()
{
cout<<year<<"-"<<month<<"-"<<day<<endl;
}
void main()
{
date a;
a.display();
date b(2017,13,28);
b.display();
}
二、集合类
#include<iostream>
using namespace std;
class atest
{
int a[10];
public:
atest(int *b)
{
for(int i=0;i<10;i++)
*(a+i)=*(b+i);
}
void display();
int checkele(int b);
};
void atest::display()
{
for(int i=0;i<10;i++)
cout<<*(a+i)<<(i%5==4?'\n':'\t');
}
int atest::checkele(int b)
{
for(int i=0;i<10;i++)
if(*(a+i)==b)
return i+1;
return -1;
}
void main()
{
int b[10]={1,2,3,4,5,6,7,8,9,10};
atest a(b);
a.display();
int n;
cout<<"请输入要查找的元素:\n";
cin>>n;
cout<<"您要查找的元素位于:"<<a.checkele(n)<<endl;
}
三、职工类
#include<iostream>
using namespace std;
#include<string.h>
class staff
{
int idnum;
char *name;
int gender;
float basicin;
float bouns;
public:
staff(int a=0,char *b=0,int c=3, float d=0, float e=0)
{
idnum=a;
if(b==0)
name=0;
else
{
name=new char[strlen(b)+1];
strcpy(name,b);
}
gender=c;
basicin=d;
bouns=e;
}
~staff()
{
delete []name;
}
void changetotal(float a,float b);
void display()
{
cout<<"ID number"<<'\t'<<"Name"<<'\t'<<"Gender"<<'\t'<<"Basic income"<<'\t'<<"Bouns"<<'\t'<<"Total"<<endl;
cout<<idnum<<'\t'<<name<<'\t';
if(gender==1)
cout<<"man";
else if(gender==2)
cout<<"woman";
else
cout<<"unknow";
cout<<'\t'<<basicin<<'\t'<<bouns<<'\t'<<basicin+bouns<<endl;
}
};
void staff::changetotal(float a,float b)
{
basicin=a;
bouns=b;
}
void main()
{
char a[20]="张三";
staff b(100,a,1,1782.3,2000);
b.display();
b.changetotal(1000,1000);
b.display();
}