题目1
(给出题目描述)设计一个类CRectangle
代码清单:
#include<iostream>
using namespace std;
class CRectangle
{
public:
CRectangle()
{
m_l=1.0;
m_w=1.0;
}
void get()
{
cin>>m_l;
if(m_l>50)
{
m_l=1.0;
}
cin>>m_w;
if(m_w>50)
{
m_w=1.0;
}
}
double setzc()
{
return 2*(m_l+m_w);
}
private:
double m_l;
double m_w;
};
int main()
{
CRectangle c1;;
c1.get();
cout<<c1.setzc();
return 0;
}
运行结果截图
题目2
(给出题目描述)
定义一个时间类Time
代码清单:
#include <iostream>
#include <string>
using namespace std;
class Time{
private:
int days;
int hours;
int minutes;
public:
Time(int d,int h, int m );
Time Sum(const Time & t);
void Show() const;
};
Time::Time(int d,int h, int m){
days=d;
hours=h;
minutes=m;
}
Time Time::Sum(const Time& t){
int a,b;
Time t4(0,0,0);
t4.minutes=(this->minutes+t.minutes)%60;
a=(this->minutes+t.minutes)/60;
t4.hours=(this->hours+t.hours+a)%24;
b=(this->hours+t.hours+a)/24;
t4.days=(this->days+t.days+b);
return t4;
}
void Time::Show() const{
cout<<days<<" "<<hours<<" "<<minutes;
}
int main(){
int d1,h1,m1,d2,h2,m2;
cin>>d1>>h1>>m1>>d2>>h2>>m2;
Time t1(d1,h1,m1),t2(d2,h2,m2),t3(0,0,0);
t3=t1.Sum(t2);
t3.Show();
return 0;
}
运行结果截图
题目2
CCircle圆类求圆环面积
代码清单:
#include<iostream>
using namespace std;
class CCircle
{
double a1,a2;
public:
void set1(double r1)
{
a1=r1;
}
void set2(double r2)
{
a2=r2;
}
double area()
{
return a2*a2*3.14159-a1*a1*3.14159;
}
};
int main()
{
CCircle c;
double r1,r2;
cin>>r1>>r2;
c.set1(r1);
c.set2(r2);
cout<<"area="<<c.area()<<endl;
return 0;
}
运行结果截图