刚学c++,几个比较基础的关于类程序

1、#include<iostream>
using namespace std;
class Rectangle{
 private:
    float xl,yl,xr,yr;
 public :
    void setRectangle();
    float perimeter( );
    float area( );
    int inRectangle(float x ,float y);
};
void Rectangle::setRectangle(){
 cin>>xl>>yl>>xr>>yr;
 
}
float Rectangle::perimeter(){
 cout<<"周长:"<<((xr-xl)+(yr-yl))*2.0<<endl;
}
float Rectangle::area(){
 cout<<"面积:"<<(xr-xl)*(yr-yl)<<endl;
}
int Rectangle::inRectangle(float x,float y){
 cin>>x>>y;
 if(x>xl && x<xr && y>yl && y<yr)
      cout<<"inRectangle"<<endl;
      else cout<<"outRectangle"<<endl;
}
int main(){
 Rectangle a;
 float q,w;
 cout<<"请输入x,y的范围:";
 a.setRectangle();
 a.perimeter();
 a.area();
 cout<<"请输入待判断点的坐标:";
 a.inRectangle( q, w);
}
2、
#include <iostream>
using namespace std;
class Score
{ private:
    int num,math,english,programming;
 double average;
  public:
    void inscore( );
    void showscore( );
};
void Score::inscore( )
{
   cin>>num>>math>>english>>programming;
   average=(math+english+programming)/3;
}
void Score::showscore()
{
   cout<<"num:"<<num<<"  math:"<<math<<"  english:"<<english<<"  programming:"<<programming<<"  average:"<<average<<endl;
}
int main()
{   Score *s;  int n,i;
    cout<<"请输入学生人数:";
 cin>>n;
    s=new Score[n];
    cout<<"请输入学生信息:"<<endl;
    for (i=0; i<n; i++)
  s[i].inscore();  //s[i]还可以表示成*(s+i)
    cout<<"学生信息:"<<endl;
 for (i=0; i<n; i++)
  s[i].showscore();
 return 0;
}

3、
#include<iostream>
using namespace std;
class  Time
{
   public:
     void setTime( int h, int m, int s);
     void addTime(int s1);
     void showTime( );
   private:
      int hour, minute, second;
};
void Time::setTime(int h, int m, int s){
 hour=h;
 minute=m;
 second=s;
}
void Time::showTime( ){
 cout<<hour<<":"<<minute<<":"<<second<<endl;
}
void Time::addTime(int s1){
 int i,t;
 second+=s1;
 t=second/60;
 for(i=0;i<t;i++){
 if(second>=60){
  second=second-60;
  minute++;
     if(minute>=60){
      minute=minute-60;
      hour++;
  }
    }
}
}
int main(){
 Time t;
 t.setTime(10,25,56);
 t.showTime();
 t.addTime(100);
 t.showTime();
 return 0;
 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值