C++面向对象编程实例

实例一、四位运算符操作

main.cpp.

 #include <iostream>
#include "operator_1.h"
#include <stdio.h>
#include <iomanip> using namespace std; int main()
{
double r = ;
operator_1 op; op.setOperator_1('*');
op.setOperator_1Parameter(100.0, 3.0); if (op.result(r)){
cout<<setiosflags(ios::fixed)<<r<<endl;//位数不够,末尾添0
}
else {
cout <<"calculate erro!" << endl;
} return ;
}

operator_1.cpp

 #include "operator_1.h"

 bool operator_1::setOperator_1(char op){
bool ret = false;
if((op == '*')||(op == '/')||( op == '+')||( op == '*')){
ret == true;
op_1 =op;
}
else {
return ret;
}
} void operator_1::setOperator_1Parameter(double a, double b){
m0p = a;
m1p = b;
} bool operator_1::result(double & r){
bool ret = true;
switch (op_1) {
case '+':
r = m0p + m1p;
break;
case '-':
r = m0p - m1p;
break;
case '*':
r = m0p * m1p;
break;
case '/':
r = m0p / m1p;
break;
default:
return ret= false;
break;
}
}

operator_1.h

 #ifndef OPERATOR_H
#define OPERATOR_H class operator_1
{
public:
bool setOperator_1(char op);
void setOperator_1Parameter(double a, double b);
bool result(double & r);
private:
double m0p;
double m1p;
char op_1;
};
#endif // OPERATOR_H
上一篇:python面向对象编程实例解析


下一篇:例说 AD中ROOM的使用