1. 创建一个Rectangle类,具有两个数据成员width和length 以及一个成员函数DisplayRectangle();
(1) 为Rectangle类创建无参数的、带参数的构造函数;
#include <iostream>
using namespace std;
class Rectangle
{
private:
int width, length;
public:
Rectangle()
{
width = 0;
length = 0;
}
Rectangle(int w, int l)
{
width = w;
length = l;
}
void DisplayRectangle();
};
void Rectangle::DisplayRectangle()
{
cout<<"width="<<width<<",";
cout<<"length="<<length<<endl;
}
int main()
{
Rectangle r1,r2(2,3);
r1.DisplayRectangle();
r2.DisplayRectangle();
return 0;
}
(2) 为Rectangle类创建具有默认参数的构造函数。
#include <iostream>
using namespace std;
class Rectangle
{
private:
int width, length;
public:
Rectangle(int w = 0,int l = 1)
{
width = w;
length = l;
}
void DisplayRectangle();
};
void Rectangle::DisplayRectangle()
{
cout<<"width="<<width<<",";
cout<<"length="<<length<<endl;
}
int main()
{
Rectangle r1,r2(2,3);
r1.DisplayRectangle();
r2.DisplayRectangle();
return 0;
}
2.编写一个构造三角形的应用程序,让用户输入三角形三边的长度,然后程序确定用户输入的三条边的长度能否构成三角形,如果能够构成三角形,确认所构成三角形的类型(类型包括:直角三角形、等腰三角形、等边三角形)。需要定义一个代表三角形的类和主函数。
#include <iostream>
#include <iomanip>
using namespace std;
class Box
{
private:
int a, b, c;
public:
void input()
{
cin>>a>>b>>c;
}
void max1()
{
int t;
if(a < b)
{
t = a;
a = b;
b = t;
}
if(a < c)
{
t = a;
a = c;
c = t;
}
if(b < c)
{
t = b;
b = c;
c = t;
}
}
void Isright()
{
int f = 0;
max1();
if(b+c>a&&a+c>b&&a-c<b&&a-b<c&&b-c<a)
{
if(a*a == b*b + c*c)
{
f = 1;
}
else if(a==b&&b==c)
{
f = 2;
}
else if(a==b||b==c||a==c)
f = 3;
else
f = 4;
}
if(f==0)
{
cout<<"这不是三角形"<<endl;
}
else if(f==1)
cout<<"这是直角三角形"<<endl;
else if(f==2)
cout<<"这是等边三角形"<<endl;
else if(f==3)
cout<<"这是等腰三角形"<<endl;
else
cout<<"这是三角形"<<endl;
}
};
int main()
{
Box r;
r.input();
r.Isright();
return 0;
}
3. 创建Point类,记录系统中创建点对象的数目,并计算两个点之间的距离。
要求:
(1) 修改构造和析构函数,在构造函数和析构函数中体现点数目的变化;
(2) 增加一个显示点的数目的静态成员函数 showcount();
(3) 添加一个友元函数,使其计算并显示任意两点之间的距离。
#include <iostream>
#include <math.h>
using namespace std;
int num = 0;
class point
{
private:
int x, y;
public:
point(int x1, int y1)
{
x = x1;
y = y1;
num++;
}
~point()
{
num--;
}
static void showcount()
{
cout<<num<<endl;
}
friend void distanc(point &c1,point &c2);
};
void distanc(point &c1, point &c2)
{
double c;
c = (c1.x-c2.x)*(c1.x-c2.x) + (c1.y-c2.y)*(c1.y-c2.y);
cout<<sqrt(c)<<endl;
}
int main()
{
point c1(1,1),c2(1,-2);
c1.showcount();
distanc(c1, c2);
return 0;
}
4.一个电子公司要生产数字时钟,需开发一个程序模拟数字时钟。时钟以秒为单位更新时间。当第一次启动时钟的时候,要提示用户设定当前的时间值。用户要键入一个1-12的数字代表小时,两个不大于59的数字分别代表分和秒值。无效的时分秒将分别被设定为12、00、00。为了加速程序的开发,公司做了一个简单的模拟程序,显示12:00:00—11:59:59范围内的时间值。但没有表明是AM还是PM。
#include <iostream>
#include <iomanip>
using namespace std;
class time
{
private:
int hour, minute, sec;
public:
time(int a, int b, int c)
{
if(a<1||a>12)
a = 12;
if(b<0||b>59)
b = 0;
if(c<0||c>59)
c = 0;
hour = a;
minute = b;
sec = c;
}
void display()
{
cout<<setw(2)<<setfill('0')<<hour<<":";
cout<<setw(2)<<setfill('0')<<minute<<":";
cout<<setw(2)<<setfill('0')<<sec<<endl;
}
};
int main()
{
int a, b, c;
cin>>a>>b>>c;
time r(a,b,c);
r.display();
return 0;
}
5. 一个圆形游泳池如图所示,现在需要在其周围建一圆形过道,并在其四周围上栅栏。栅栏价格为35元/米,过道造价为20元/平方米。过道宽度为3米,游泳池半径由键盘输入。
要求编程计算并输出过道和栅栏的造价。
#include <iostream>
#include <iomanip>
using namespace std;
#define pi 3.14
class value
{
private:
double r;
public:
value(double a)
{
r = a;
}
void display()
{
double len, squ;
len = (2*r*pi + 2*pi*(r+3))*35;
squ = (pi*(r+3)*(r+3) - pi*r*r)*20;
cout<<"过道造价:"<<squ<<","<<"栅栏造价:"<<len<<endl;
}
};
int main()
{
int a;
cin>>a;
value t(a);
t.display();
return 0;
}