问题描述:
设计一个能身测试高体重是否标准的面向对象的程序,然后我与陆云杰同学一同完成。结对编程的过程很棒,在这里对云杰同学表示感谢!
代码:
#include <iostream>
#include "Test.h"
using namespace std;
void fun_out(double W,double w);
int main()
{
double h,w;
char s;
while(1)
{
cout<<"请输入身高,体重,性别(m(男性)f(女性)):";
cin>>h>>w>>s;
if(h==0&&w==0&&s=='0')
break;
Test t(h,w,s);
t.fun();
cout<<"请继续输入测试!(输入'0 0 0'退出)"<<endl;
}
return 0;
}
#ifndef TEST_H
#define TEST_H
class Test
{
private:
double height;
double weight;
char sex;
public:
Test(double h=0,double w=0,char s='a'):height(h),weight(w),sex(s) {};
void fun();
};
#endif // TEST_H
#include "Test.h"
#include <iostream>
using namespace std;
void fun_out(double W,double w);
void Test::fun()
{
double Weight;
if(sex=='m')
{
Weight=height-100;
}
else Weight=height-105;
fun_out(Weight,weight);
};
void fun_out(double W,double w)
{
if(w>W*1.2)
cout<<"您超重了!"<<endl;
else if(w<W*0.8)
cout<<"您太瘦啦!"<<endl;
else cout<<"您体重正常!继续保持!"<<endl;
}
运行结果: