实验目录:点击打开链接
1、第一题
定义三角形类,完成:为三边置值、取三边的值并输出、求三角形周长、求三角形面积、输出三角形周长和面积。要求使用构造函数为三边赋值。提示:可以定义一个无参的构造函数,在此构造函数中通过键盘输入的方法输入三边的值。
#include<bits/stdc++.h>
using namespace std;
class Triangle
{
double a,b,c;
public:
Triangle()
{
cout<<"请输入三角形边长:"<<endl;
cin>>a>>b>>c;
}
double l()
{
return a+b+c;
}
double s()
{
double d;
d=(a+b+c)/2.0;
return (sqrt(d*(d-a)*(d-b)*(d-c)));
}
void print()
{
cout<<"周长"<<l()<<endl;
cout<<"面积"<<s()<<endl;
}
};
int main()
{
Triangle t;
t.print();
return 0;
}
2.第二题
2、定义计数器类Counter。要求具有以下成员:计数器值;可进行增值和减值记数;可提供记数值。要求使用有参构造函数为计数器赋初值。编写主函数对这个类进行测试。
#include<bits/stdc++.h>
using namespace std;
class Count
{
int num;
public:
Count(int i);
void increase();
void decrease();
void print();
};
Count::Count(int i)
{
num=i;
}
void Count::increase()
{
cout<<"增值"<<endl;
num++;
}
void Count::decrease()
{
cout<<"减值"<<endl;
num--;
}
void Count::print()
{
cout<<"计数器值"<<endl;
cout<<num<<endl;
}
int main()
{
int num;
cout<<"请输入计数器初值:"<<endl;
cin>>num;
Count c(num);
c.increase();
c.print();
c.decrease();
c.print();
return 0;
}
第三题:
定义一个阶乘类Cfactorial实现阶乘的计算和显示。
#include<bits/stdc++.h>
using namespace std;
class Cfactorial
{
long long num,ans;
public:
Cfactorial(long long i);
void solve();
void print();
};
Cfactorial:: Cfactorial(long long i)
{
num=i;
ans=num;
}
void Cfactorial::solve()
{
cout<<"增值"<<endl;
if(num==0||num==1) {ans=1;return ;}
long long t=num;
while(t--)
{
if(t==0) break;
ans=ans*t;
}
}
void Cfactorial::print()
{
cout<<"!"<<num<<"=";
cout<<ans<<endl;
}
int main()
{
long long num;
cout<<"请输入阶乘值:"<<endl;
cin>>num;
Cfactorial c(num);
c.solve();
c.print();
return 0;
}
第四题
4、设计一个时间类,其中数据成员:年 月 日;成员函数有三个:(1)设置年月日的具体值;(2)判断该年是否为闰年;(3)将年月日输出。编写主函数,实现并测试这个类。
#include<bits/stdc++.h>
using namespace std;
class Time
{
int y,m,d;
public:
void in()
{
cout<<"请输入年月日:"<<endl;
cin>>y>>m>>d;
}
void judge()
{
if((y%4==0&&y%100==0)||y%400==0)
cout<<"该年是瑞年"<<endl;
else
cout<<"该年不是瑞年"<<endl;
}
void out()
{
cout<<y<<"年"<<m<<"月"<<d<<"日"<<endl;
}
};
int main()
{
Time t;
t.in();
t.out();
t.judge();
return 0;
}
第五题
5 、定义一个 dog 类,包含 age 、 weight 属性,以及对这些属性的操作方法。实现并测试这个类#include<iostream>
using namespace std;
class Dog
{int age;
int weight;
public:
void set(int x,int y);
int get_age();
int get_weight();
};
void Dog::set(int x,int y)
{age=x;
weight=y;
}
int Dog::get_age(){return age;}
int Dog::get_weight(){return weight;}
int main()
{Dog a;
a.set(3,50);//或使用构造函数初始化
cout<<"age="<<a.get_age()<<endl;
cout<<"weight="<<a.get_weight()<<endl;
return 0;
}
第六题
设计并测试一个名为Rectangle 的矩形类,其属性为矩形的左下角与右上角两个点的坐标,能计算矩形的面积。提示:一种方法,可以定义一个矩形类,在类中定义4个数据成员,分别表示左下角和右上角两个点的x、y坐标,这时,在主函数中只需定义一个矩形类的对象即可;第二种方法,可以定义一个坐标类,在类中定义2个数据成员,分别表示x坐标和y坐标,这时,在主函数中需要定义两个坐标类对象,即左下角坐标和右上角坐标对象。
#include<bits/stdc++.h>
using namespace std;
class Rectangle
{
double x1,y1,x2,y2;
public:
friend istream&operator >>(istream &in,Rectangle &r)
{
cout<<"请输入矩形的右上角点的坐标"<<endl;
in>>r.x1;in>>r.y1;
cout<<"请输入矩形的左下角点的坐标"<<endl;
in>>r.x2;in>>r.y2;
return in;
}
double s();
void outs();
};
double Rectangle::s()
{
return (x1-x2)*(y1-y2);
}
void Rectangle::outs()
{
cout<<"面积为"<<s()<<endl;
}
int main()
{
Rectangle r;
cin>>r;
r.outs();
return 0;
}