由买买提看人间百态

topics

全部话题 - 话题: cfraction
(共0页)
a****m
发帖数: 693
1
来自主题: Programming版 - 再请教一个class输出的问题
总是说不能display, 如果把所有的class里面的改成public, 就可以直接用num and
denum
输出到 display, 但是如果private,怎么都不行,谢谢。
#include
using namespace std;
class CFraction
{
private: //private method to get GCD
int num;
int denum;
public:
CFraction(int num, int denum);
int getnum();
int getdenum();
void display();
};
//constructor...
int CFraction::getnum(){
return num;
}
int CFraction::getdenum(){
return denum;
}
CFraction::CFraction( int a, int b )
{
num = a;
denum = b;
}
void CFraction::display() {
cout << ... 阅读全帖
a****m
发帖数: 693
2
#include
using namespace std;
class CFraction
{
public: //public method to get GCD
int iNumerator;
int iDenominator;
double dValue;
//constructor...
CFraction( int iNumeratorParam, int iDenominatorParam )
{
iNumerator = iNumeratorParam;
iDenominator = iDenominatorParam;
dValue = iNumerator/ iDenominator;
}
//getGCD
int getGCD( int iNumerator, int iDenominator )
{
int remainder = iDenominator % iNumerator;
if ( remainder != 0 )
return getGCD( remainder,iNumerator );
}
//Reduce (wha... 阅读全帖
a****m
发帖数: 693
3
#include
#include "GCD.hpp"
using namespace std;
class CFraction
{
public: //private method to get GCD
int iNumerator;
int iDenominator;
//constructor...
CFraction( int iNumeratorParam, int iDenominatorParam )
{
iNumerator = iNumeratorParam;
iDenominator = iDenominatorParam;
}
//getGCD
int getGCD( int iNumerator, int iDenominator )
{
int remainder = iDenominator % iNumerator;
if ( remainder != 0 )
return getGCD( remainder,iNumerator );
else return 0;
}
CFraction reduce()
{
int iGC... 阅读全帖
a****m
发帖数: 693
4
CFraction reduce()
{
int iGCD = getGCD(int iNumerator, int iDenominator);
// 总是在这一行出问题: expected primary-expression before "int"
//It seems that it does not need redefine the type, getGCD(inum, idenom)
iNumerator=iNumerator/iGCD;
iDenominator=iDenominator/iGCD;
}
thanks all you guys!
(共0页)