类定义
#ifndef SIMPLECAlCULATOR_H
#define SIMPLECAlCULATOR_H
// class SimpleCalculator definition
class SimpleCalculator
{
public:
/* Write prototype for add member function */
SimpleCalculator(double,double);
void setDate(double, double);
double adding(double, double) const;
double subtract( double, double ) const;
double multiply( double, double ) const;
double dividing(double, double) const;
/* Write prototype for divide member function */
private:
double a;
double b;
}; // end class SimpleCalculator
#endif函数(方法)
#include "SimpleCalculator.h"
/* Write definition for add member function */
// function subtract definition
SimpleCalculator::SimpleCalculator(double a,double b)
{
setDate(a,b);
}
void SimpleCalculator::setDate(double one,double two)
{
a = one;
b = two;
}
double SimpleCalculator::adding(double a, double b) const
{
return a + b;
}
double SimpleCalculator::subtract( double a, double b ) const
{
return a - b;
} // end function subtract
// function multiply definition
double SimpleCalculator::multiply( double a, double b ) const
{
return a * b;
} // end function multiply
double SimpleCalculator::dividing(double a, double b) const
{
return a / b;
}
/* Write definition for divide member function */主函数
#include
using std::cout;
using std::endl;
#include "SimpleCalculator.h"
int main()
{
double a = 10.0;
double b = 20.0;
SimpleCalculator sc(a, b);
/* Instantiate an object of type Simplecalculator */
cout << "The value of a is: " << a << "\n";
cout << "The value of b is: " << b << "\n\n";
/* Write a line that adds a and b through your SimpleCalculator
object; assign the result to a variable named addition */
double addition = sc.adding(a, b);
cout << "Adding a and b yields " << addition << "\n";
double subtraction = sc.subtract( a, b );
cout << "Subtracting b from a yields " << subtraction << "\n";
double multiplication = sc.multiply( a, b );
cout << "Multiplying a and b yields " << multiplication << "\n";
/* Write a line that divides a and b through the
SimpleCalculator object; assign the result to a
variable named division */
double division = sc.dividing(a, b);
cout << "Dividing a by b yields " << division << endl;
return 0;
}
京公网安备 11010502036488号