C++编程题:
1. 定义个datetime类,使其对象可以显示当前系统时间和日期 。
#include<time.h>
#include<stdio.h>
class datetime
{
public:
int year;
int month;
int day;
int hour;
int min;
int sec;
datetime()
{
struct tm* ptm;
time_t m;
time(&m);
ptm = localtime(&m);
year = ptm->tm_year+1900;
month = ptm->tm_mon+1;
day = ptm->tm_mday;
hour = ptm->tm_hour;
min = ptm->tm_min;
sec = ptm->tm_sec;
}
void output()
{
printf("%.4d/%.2d/%.2d %.2d:%.2d:%.2d\n",year,month,day,hour,min,sec);
}
};
void main(void)
{
datetime d;
d.output();
}
2、设计一个汽车类Vehicle,包含数据成员车轮数和车重,由它派生出类Car和类Truck,前者包含载客数,后者包含载重量。编写程序实现。
#include<iostream.h>
class vehicle // 定义汽车类
{
protected:
int wheels; // 车轮数
float weight; // 重量
public:
vehicle(int wheels,float weight);
int get_wheels();
float get_weight();
float wheel_load();
void show();
};
class car:public vehicle // 定义小车类
{
int passenger_load; // 载人数
public:
car(int wheels,float weight,int passengers=4);
int get_passengers();
void show();
};
class truck:public vehicle // 定义卡车类
{
int passenger_load; // 载人数
float payload; // 载重量
public:
truck(int wheels,float weight,int passengers=2,float max_load=24000.00);
int get_passengers();
float efficiency();
void show();
};
vehicle::vehicle(int wheels,float weight)
{
vehicle::wheels=wheels;
vehicle::weight=weight;
}
int vehicle::get_wheels()
{
return wheels;
}
float vehicle::get_weight()
{
return weight/wheels;
}
void vehicle::show()
{
cout << "车轮:" << wheels << "个" << endl;
cout << "重量:" << weight << "公斤" << endl;
}
car::car(int wheels, float weight,
int passengers) :vehicle (wheels, weight)
{
passenger_load=passengers;
}
int car::get_passengers ()
{
return passenger_load;
}
void car::show()
{
cout <<" 车型:小车" << endl;
vehicle::show();
cout << "载人:" << passenger_load << "人" << endl;
cout << endl;
}
truck:: truck(int wheels, float weight,int passengers, float max_load):vehicle(wheels,weight)
{
passenger_load=passengers;
payload=max_load;
}
int truck::get_passengers()
{
return passenger_load;
}
float truck::efficiency()
{
return payload/(payload+weight);
}
void truck::show()
{
cout <<"车型:卡车" << endl;
vehicle:: show ();
cout << "载人:" << passenger_load << "人" << endl;
cout << "效率:" << efficiency() << endl;
cout << endl;
}
void main ()
{
car car1(4,2000,5);
truck tru1(10,8000,3,340000);
cout << "输出结果" << endl;
car1. show ();
tru1. show ();
}
3、设计一个点类Point,包含点的坐标x,y 两个数据成员,采用友元类的方式分别计算两点间的水平距离和垂直距离。
#include<iostream>
using namespace std;
class Point;//先声明类型Point
int horizontalDistance(const Point& first, const Point& second);//水平距离函数声明
int verticalDistance(const Point& first, const Point& second);//垂直距离函数声明
class Point
{
private:
int x;//横坐标
int y;//纵坐标
public:
Point(int x=0, int y = 0)//构造函数
{
this->x = x;
this->y = y;
}
friend int horizontalDistance(const Point& first, const Point& second);
friend int verticalDistance(const Point& first, const Point& second);
};
//水平距离函数定义
int horizontalDistance(const Point& first, const Point& second)
{
if(first.x -second.x >=0)
return first.x-second.x;
else
return second.x-first.x;
}
//垂直距离函数定义
int verticalDistance(const Point& first, const Point& second)
{
if(first.y -second.y >=0)
return first.y-second.y;
else
return second.y-first.y;
}
int main()
{
Point a(1,2); //定义三个Point对象
Point b(0,0);
Point c(-1,-1);
//测试
cout<< horizontalDistance(a,b) <<endl;
cout<< horizontalDistance(a,c) <<endl;
cout<< verticalDistance(a,b) <<endl;
cout<< verticalDistance(a,c) <<endl;
return 0;
}
4、设计一个点类Point,包含横、纵两个坐标数据x,y,由它派生出圆类Circle,并添加一个半径数据r,求其面积area。
#include<iostream> //#include<iomanip>//此头文件与下面的setprecision(3)对应,可同时加上,控制输出精度
using namespace std;
#define PI 3.1415926
//点类如下:
class Point
{
public:
Point(){}
Point(float x1,float y1)
{
x=x1;
y=y1;
}
virtual float area()=0;
virtual float perim()=0;//周长
private:
float x;
float y;
};
class Circle:public Point
{
public:
Circle(float x1,float y1,float r1);
float area();
float perim();
private:
float r;
};
Circle::Circle(float x1,float y1,float r1):Point(x1,y1)
{
r=r1;
}
float Circle::area()
{
return PI*r*r;
}
float Circle::perim()
{
return 2*PI*r;
}
int main()
{
Point *p;
Circle circle(0,0,3);//前两个为point(0,0),第三个常量"3"为圆的半径。
p=&circle;
cout<<"Circle的周长是:"<<p->perim()<<endl;
cout<<"Circle的面积是:"<<p->area()<<endl;
}
5、设计一个点类Point,包含两个坐标数据成员x,y和一个构造函数;再设计一个友元函数distance()用于求两点之间的距离。
#include<iostream>
using namespace std;
class Point;//先声明类型Point
int horizontalDistance(const Point& first, const Point& second);//水平距离函数声明
int verticalDistance(const Point& first, const Point& second);//垂直距离函数声明
class Point
{
private:
int x;//横坐标
int y;//纵坐标
public:
Point(int x=0, int y = 0)//构造函数
{
this->x = x;
this->y = y;
}
friend int horizontalDistance(const Point& first, const Point& second);
};
//水平距离函数定义
int horizontalDistance(const Point& first, const Point& second)
{
if(first.x -second.x >=0)
return first.x-second.x;
else
return second.x-first.x;
}
int main()
{
Point a(1,2);//定义三个Point对象
Point b(0,0);
Point c(-1,-1);
cout<< horizontalDistance(a,b) <<endl;
cout<< horizontalDistance(a,c) <<endl;
return 0;
}
6、编写class cA的派生类class cAB,增加成员函数,用于求圆的周长
class cA
{
int r;
public:
cA(int x){ r=x; }
double area( ) { return r*r*3.14; }
int get_r( ) { return r; }
};
class cAB:public Ca
{
cAB(int x):cA(x){ }
double girth( ) { return 2*get_r( )*3.14; }
};
7、定义一个处理日期的类TDate,它有3个私有数据成员:Month,Day,Year和若干个公有成员函数,并实现如下要求:
①构造函数重载;②成员函数设置缺省参数;③定义一个友元函数来打印日期(3分);④可使用不同的构造函数来创建不同的对象。
#include <stdio.h>
class TDate
{
public:
TDate(); //构造函数
TDate(int nMoth,int nDay,int nYear); //构造函数重载
void SetDay(int nDay=1); //三个设置某个成员变量的成员函数,都带有默认值
void SetMonth(int nMonth=1);
void SetYear(int nYear=2001);
void SetDate(int nMoth,int nDay,int nYear);//一个非静态成员函数
friend void PrintDate(TDate cTdate); //友员函数
private:
int m_nMonth;
int m_nDay;
int m_nYear;
};
TDate::TDate()
{
m_nDay=1;
m_nMonth=1;
m_nYear=2000;
}
TDate::TDate(int nMoth,int nDay,int nYear)
{
m_nYear=nYear;
m_nDay=nDay;
m_nMonth=nMoth;
}
void TDate::SetDate(int nMoth,int nDay,int nYear)
{
m_nYear=nYear;
m_nDay=nDay;
m_nMonth=nMoth;
}
void TDate::SetDay(int nDay/*=1*/)
{
m_nDay=nDay;
}
void TDate::SetMonth(int nMonth/*=1*/)
{
m_nMonth=nMonth;
}
void TDate::SetYear(int nYear/*=2000*/)
{
m_nYear=nYear;
}
void PrintDate(TDate cTDate)
{
printf("Date is:%d-%d-%d",cTDate.m_nYear,cTDate.m_nMonth,cTDate.m_nDay);
}
void main()
{
TDate cTdate;
cTdate.SetDate(6,1,2012); //使用成员函数
cTdate.SetDay(10);
TDate CMyDate(6,1,2012);//重载的构造函数生成对象实例
PrintDate(CMyDate); //使用友员函数
}