您的位置:首页 > 编程语言 > C语言/C++

effective c++ 条款23 perfer nonmember nonfreind function to member function

2015-09-24 17:16 417 查看
/*
* main.cpp
*
*  Created on: Sep 23, 2015
*      Author: lili0506
*/

#include"Rational.h"

int main()
{
Rational oneFourth(1,4);
Rational result;
result = oneFourth * 2;
result = 2 * oneFourth;
}


/*
* Rational.cpp
*
*  Created on: Sep 23, 2015
*      Author: lili0506
*/

#include "Rational.h"

Rational::Rational(int n, int d) :
numerator(n),
denominator(d)
{
// TODO Auto-generated constructor stub

}

Rational::~Rational()
{
// TODO Auto-generated destructor stub
}
int Rational::getDenominator() const
{
return denominator;
}

void Rational::setDenominator(int denominator)
{
this->denominator = denominator;
}

int Rational::getNumerator() const
{
return numerator;
}

void Rational::setNumerator(int numerator)
{
this->numerator = numerator;
}

Rational operator*(const Rational & l,const Rational &r)
{
return Rational(l.getDenominator() * r.getDenominator(), l.getNumerator() * l.getNumerator());

}


/*
* Rational.h
*
*  Created on: Sep 23, 2015
*      Author: lili0506
*/

#ifndef RATIONAL_H_
#define RATIONAL_H_

class Rational
{
public:
Rational(int n = 0, int d = 1);
virtual ~Rational();

int getDenominator() const;

void setDenominator(int denominator);

int getNumerator() const;
void setNumerator(int numerator);

private:
int numerator;
int denominator;
};

Rational operator*(const Rational & l,const Rational &r);

#endif /* RATIONAL_H_ */


makefile文件

.SUFFIXES: .cpp .o

CC=g++

SRC=Rational.cpp main.cpp
OBJ= $(SRC:.cpp=.o)
TRAGET=main

all:$(TRAGET)

main:$(OBJ)
$(CC) -o $@ $^

.cpp.o:
$(CC) -o $@ -c $<

clean:
rm -rf $(OBJ) $(TRAGET)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: