using namespace std; extern int i = 0; class Date { public:set(int y,int m, int d); add(); print(); private:
欧阳学创编
欧阳学创编
int year; int month; int day; };
Date::set(int y, int m, int d) {
year = y; month = m; day = d; }
Date::add() {
switch(month) { case 1: case 3:
欧阳学创编
欧阳学创编
case 5: case 7: case 8:
case 10: if(day < 31) day += 1; else if(day == 31) {
day = 1; month += 1; }
break;
case 12: if(day < 31) day += 1; else if(day == 31) {
day = 1;
欧阳学创编
欧阳学创编
month = 1; year += 1; }
break; case 4: case 6: case 9:
case 11: if(day < 30) day += 1;
else if(day == 30) {
day = 1; month += 1; } break;
case 2: if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
欧阳学创编
欧阳学创编
{
if(day < 29) day += 1;
else if(day == 29) {
day = 1; month+= 1; } } else {
if(day < 28) day += 1;
else if(day == 28) { day = 1;
欧阳学创编
欧阳学创编
month += 1; } } break; } }
Date::print() {
cout << day << \"/\" << month << \"/\" << year << endl; }
void main() {
Date d1; int year; int month; int day;
欧阳学创编
欧阳学创编
cout << \"请输入年月日:\"; cin >> year >> month >> day; d1.set(year, month, day); d1.print(); d1.add();
cout << \"下一天是:\" ; d1.print(); }
2.定义一个时间类Time,能提供和设置由时、分、秒组成的时间,并编出应用程序,定义时间对象,设置时间,输出该对象提供的时间。 答案: //Time.h class Time { public: Time();
欧阳学创编
欧阳学创编
~Time();
bool SetTime(int h,int m,int s); void GetTime(int &h,int &m,int &s); void Print(); private:
int iHour,iMinute,iSecond; };
/***********************************/ //Time.cpp
#include #include \"Time.h\" using namespace std; Time::Time() {iHour=1;iMinute=1;iSecond=1; }
欧阳学创编
欧阳学创编
Time::~Time(){}
bool Time::SetTime(int h,int m,int s) {
if((h<0)||(h>23)) return false; if((m<0)||(m>59)) return false; if((s<0)||(s>59)) return false;
iHour =h;iMinute= m;iSecond =s;return true; }
void Time::GetTime(int &h,int &m,int &s) {
h=iHour;m=iMinute;s=iSecond; }
void Time::Print() {
cout<欧阳学创编欧阳学创编
/**********************************/ //mainfunction.cpp #include #include \"Time.h\" using namespace std; void main() { Time t;t.SetTime(23,44,59); t.Print(); }
3. 编写一个矩形的类,要求类中具有长和宽2个数据成员,具有构造函数、计算矩形的面积即及周长这3个成员函数。 答案:
class rectangle {
欧阳学创编
欧阳学创编
float width; float heigth; public:
rectangle(float w, float h) {width=w;heigth=h;}
float area(){return width*heigth;}
float circulm(){return 2*(width+heigth);} };
4. 设计一个人的类,要求包含姓名、身份证号、年龄、以及人的总数和所有人的平均年龄这5个数据成员,然后编写一个测试程序,输入n个人的
信息(姓名、身份证号和年龄),要求输出这些人的信息和平均年龄。 答案:
#include #include using namespace std;欧阳学创编
欧阳学创编
const int n=3; class Person {
private:
char name[20]; char id[18]; int age; public:
static int count; //人数 static float aveage; Person(void){}
Person(const char* names,char *ids,int ages) {
strcpy(name,names);//初始化姓名 strcpy(id,ids);
age = ages;//初始化年龄
欧阳学创编
欧阳学创编
aveage+=age; count++; }
void display() {
cout<<\"姓名:\"<friend void ModifyAveage();//修改平均年龄的友元函数 };int Person::count = 0; float Person::aveage=0; void ModifyAveage() {
Person::aveage=Person::aveage/Person::count;
欧阳学创编
欧阳学创编
}
void main() {
int ages,i;
char namestr[20],idstr[18]; Person per[n]; for (i=0;icout<<\"请输入姓名:\"; cin>>namestr;cout<<\"请输入身份证号码:\"; cin>>idstr;
cout<<\"请输入的年龄:\"; cin>>ages;
Person temp(namestr,idstr,ages); per[i]=temp;
欧阳学创编
欧阳学创编
}
ModifyAveage(); for (i=0;icout<<\"人的数量:\"<5. 编写一个圆的类,再使用派生类的机制,编写一个圆柱的类,求每个类都包含具有初始化功能的构造函数和相关数据的输出函数。 答案:#include #define pi 3.1416 using namespace std; class circle {protected:
欧阳学创编
欧阳学创编
float r; float area; public:
circle() {r=0;}
circle(float radius){r=radius;} void print() {
area=r*r*pi;
cout<<\"r=\"<class column:circle {protected: float h; float v;
欧阳学创编
欧阳学创编
public :
column(float radius,float height):circle(radius) {h=height;} void print() {
circle::print();
cout<<\"h=\"<void main() {column col (3.0,2.0); col.print(); }
6.重载一个函数模板,要求它可以返回两个值中的较大者,也可以求三个数的最大值。 答案:
欧阳学创编
欧阳学创编
// 求两个任意类型值中的最大者 template inline T const& max (T const& a, T const& b) {
return a < b ? b : a; }
// 求3个任意类型值中的最大者 template inline T const& max (T const& a, T const& b, T const& c) {
return ::max (::max(a,b), c); }
7.下面是整型安全数组类的一部分:试完善类的定义,使下段程序能够正常运行且不会出现内存泄漏。 #include class Array{ public:欧阳学创编
欧阳学创编
Array(int x){ count=x;
p=new int[count]; }
int & operator [](int x){return *(p+x);} protected: int count; int *p; } 答案:
#include class Array{ public: Array(int x); Array(Array &);欧阳学创编
欧阳学创编
~Array();
void SetAt(int pos, int value); int GetAt(int pos); protected: int count; int *p; };
Array::Array(int x) {
count=x;
p=new int[count]; }
Array::Array(Array &a){ int i;
count=a.count;
欧阳学创编
欧阳学创编
p=new int[count]; for (i=0;iArray::~Array() {delete p; }
void Array::SetAt(int pos, int value) {
*(p+pos)=value; }
int Array::GetAt(int pos) {
return *(p+pos); }
欧阳学创编
欧阳学创编
8、编写类String 的构造函数、析构函数和赋值函数 已知类String 的原型为: class String { public:
String(const char *str = NULL); // 普通构造函数 String(const String &other); // 拷贝构造函数 ~ String(void); // 析构函数
String & operate =(const String &other); // 赋值函数 private:
char *m_data; // 用于保存字符串 };
请编写String 的上述4 个函数。 答案:
String::String(const char *str) {
欧阳学创编
欧阳学创编
cout<<\"构造函数被调用了\"<m_data=new char[1]; *m_data='\\0'; } else {int length=strlen(str);
m_data=new char[length+1]; strcpy(m_data,str); } }
String::~String(void) {
delete m_data;
欧阳学创编
欧阳学创编
cout<<\"析构函数被调用了\"<String::String(const String &other) {cout<<\"赋值构造函被调用了\"<String & String::operator =(const String &other) {if(this==&other) {
return *this; }
delete []m_data;
欧阳学创编
欧阳学创编
int length=strlen(other.m_data); m_data=new char[length+1]; strcpy(m_data,other.m_data); return *this; };
9、写出下面程序的运行结果: #include class A { public:A(){cout<<\"A\";} ~A(){cout<<\"A\";} };
void main() { A a[5];
欧阳学创编
欧阳学创编
} 答案:
AAAAAAAAAA
10、写出下面程序的运行结果: #include class A{ public:virtual void show(){cout<<\"A\"<class D:public A{ public:virtual void show(){cout<<\"D\"<void fun(A& a){a.show();} void main(){ D d;欧阳学创编
欧阳学创编
fun(d); } 答案: D
1设计一个类string要求string类包括私有成员函数,char *str,在构造函数中初始化这个数据。写出它的两个构造函数(有参和拷贝),
析构函数,并能显示的声明和是调用构造函数(使用数组作为数据成员不给分) #include using namespace std; class String { private: char *str; int len; public:欧阳学创编
欧阳学创编
void showstr() {
cout<<\"String:\"<String(char *p) {len=strlen(p); if(len!=0)
{str=new char[len+1];strcpy(str,p); }
欧阳学创编
欧阳学创编
cout<<\"构造函数\"<~String() {if(str!=NULL) {delete[]str; str=NULL; len=0;}
cout<<\"析构函数\"<String(const String &r) {len=r.len; if(len!=0) {
str=new char[len+1];
欧阳学创编
欧阳学创编
strcpy(str,r.str); }
cout<<\"拷贝构造函数\"<void main() {String s1(\"nihao\"); s1.showstr(); String s2=s1; s2.showstr(); }
2编写 一个求两个正整数m和n最大公约数的程序并测试程序。
#include void main() {欧阳学创编
欧阳学创编
int m,n,t,r; cin>>m>>n; while (n!=0) { r=m%n; m=n; n=r; }
cout<<\"最大公约数是:\"<3编写一个求一个整形数组a[n]最大值所在下标的函数,并在主函数中调用该函数 1---------------------------- #include #define n 5int max(int a[],int l) {
int i,k,m;
欧阳学创编
欧阳学创编
m=a[0];k=0; for(i=0;ivoid main() {int a[n],i,M; for (i=0;i>a[i]; M=max(a,n); cout<4将fibnacial 序列1,1,2,3,5,a[n]中,并显示出相应的元素 #include #include欧阳学创编
的前20项存放在……欧阳学创编
void main() {
int i,a[20]={1,1}; for (i=2;i<20;i++) a[i]=a[i-1]+a[i-2]; for (i=0;i<20;i++) {if(i%5==0) cout<cout<cout<5设计一个类person要求person类包括私有成员函数,char *Name,在构造函数中初始化这个数据。写出它的两个构造函数(一般参数和对象的引用),析构函数,并能显示的声明和是调用构造函数(使用数组作为数据成员不给分)
欧阳学创编
欧阳学创编
#include using namespace std; class Person { private: char *Name; int len; public:void showName() {
cout<<\"Person:\"<Person () { len=0;Name=NULL;
欧阳学创编
欧阳学创编
}
Person (char *x) {
len=strlen(x); if(len!=0)
{Name=new char[len+1]; strcpy(Name,x); }
cout<<\"构造函数\"<~Person () {if(Name!=NULL) {delete[]Name; Name=NULL; len=0;
欧阳学创编
欧阳学创编
}
cout<<\"析构函数\"<Person (Person &xm) {len=xm.len; if(len!=0) {
Name=new char[len+1]; strcpy(Name,xm.Name); }
cout<<\"拷贝构造函数\"<void main() {欧阳学创编
欧阳学创编
Person s1(\"Liming\"); s1.showName (); Person s2=s1; s2.showName(); }
6 输出100~200之间能被3整除,但不能被7整除的数及其个数。
#include #include void main() {int i,j,count;int a[100]; count=0;
for(i=100,j=0;i<=200 && j<100;i++) if (i%3==0 && i%7!=0) { a[j]=i;
欧阳学创编
欧阳学创编
j++; count++; }
for (i=0;iif(i%6==0)cout<cout<cout<<”个数为:”<7 完成下列函数,实现将a[n]中的元素按逆序重新存放。 1---------------------------- #include #include #define N 5欧阳学创编
欧阳学创编
void swap(int a[],int n) { int i,t;
for (i=0;i{t=a[i];a[i]=a[n-i-1];a[n-i-1]=t;} for (i=0;ivoid main() {int a[N],i; for(i=0;i>a[i]; swap(a,N); cout<欧阳学创编欧阳学创编
2-------------------------- #include #include #define n 5 void main() {