分类: C/C++
2011-08-02 21:44:50
1.写一个日期类,要求能设置日期和显示日期
/*
* data.cpp
*
* Created on: 2011-8-2
* Author: lzy
*/
#include
using namespace std;
class Data
{
private:
int year, month, date;
public:
void SetData(int y, int m, int d);
void DisplayData(void);
};
void Data::SetData(int y, int m, int d)
{
year = y; month = m; date = d;
}
void Data::DisplayData(void)
{
cout<<year<<"-"<<month<<"-"<<date<<endl;
}
int main(void)
{
int y, m, d;
cout<<"输入日期:";
cin>>y>>m>>d;
Data t;
t.SetData(y,m,d);
t.DisplayData();
return 0;
}
2.写一个长方形的类,要求求出长方形的周长和面积
/*
* main.cpp
*
* Created on: 2011-8-2
* Author: lzy
*/
#include
using namespace std;
class Rectangle
{
private:
int width, height;
public:
void setWH(int w, int h){width = w; height = h;}
void getGrith(void){cout<<"周长:"<<(width+height)*2<<endl;}
void getArea(void){cout<<"面积:"<<(width*height)<<endl;}
};
int main(void)
{
Rectangle r;
int w, h;
cout<<"输入长宽";
cin>>w>>h;
r.setWH(w,h);
r.getGrith();
r.getArea();
return 0;
}
3.写一个宠物猫的类,猫的属性数据包括名字 性别 年龄 体重 颜色实现对这些数据的修改和显示,编写主要程序测试
class Cat
{
private:
char name[20], ***[5];
int age;
float weight;
char clour[10];
public:
void setName(char *str){strcpy(name, str);}
void getName(void){cout<<"名字:"<<name<<endl;}
void set***(char *str){strcpy(***, str);}
void get***(void){cout<<"性别:"<<name<<endl;}
void setAge(int n){age = n;}
void getAge(void){cout<<"年龄:"<<age<<endl;}
void setWeight(float n){weight = n;}
void getweight(void){cout<<"体重:"<<weight<<endl;}
void setClour(char *str){strcpy(clour,str);}
void getClour(void){cout<<"着色:"<<clour<<endl;}
};
4.写一个包四个整数的类,要求能求出这四个整数的最大最小数和及平均值。
/*
* main.cpp
*
* Created on: 2011-8-2
* Author: lzy
*/
#include
using namespace std;
#define NUMCONT 4
class Num
{
private:
int num[NUMCONT];
public:
void set(int no[]);
int max(void);
int min(void);
float avg(void);
};
void Num::set(int no[])
{
int i = NUMCONT;
while(i--)
num[i] = no[i];
}
int Num::max(void)
{
int *p=num, i;
for(i=1; i<NUMCONT; i++)
*p<num[i]?p=num+i:NULL;
return *p;
}
int Num::min(void)
{
int *p=num, i;
for(i=1; i<NUMCONT; i++)
*p>num[i]?p=num+i:NULL;
return *p;
}
float Num::avg(void)
{
int sum = 0, i;
for(i=0; i<NUMCONT; i++)
sum += num[i];
return sum/4;
}
int main(void)
{
int num[NUMCONT], i;
for(i = 0; i < NUMCONT; i++)
{
cout<<"输入第"<<i+1<<"数:";
cin>>num[i];
}
Num no;
no.set(num);
cout<<"最大的数是:"<<no.max()<<endl;
cout<<"最小的数是:"<<no.min()<<endl;
cout<<" 平均值是:"<<no.avg()<<endl;
return 0;
}