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)
相关文章推荐
- C++的引用类型
- 细谈select函数(C语言)
- C++中template的.h文件和.cpp文件的问题
- 关于全局变量笔记
- c++中rand()函数返回值范围
- C++中输入输出的十六进制八进制
- c++中的构造函数
- 详解C++编程中的文件流与字符串流
- 数据机构与算法分析:C语言描述 第五章——Priority Queues(Heaps)中的5.4.2 Event Simiulation
- C++二分法在数组中查找关键字的方法
- C++中的重载
- 黑马程序员---C语言学习笔记之数组的基本概念和分类
- 解读C++编程的相关文件操作
- C++开源矩阵计算工具——Eigen的简单用法
- C语言strcpy,strncpy的用法
- C++中的string常用函数用法总结
- C++ atof函数
- vc++ ^ 符号
- C++中与输入相关的istream类成员函数简介
- C++ - 栈对象的empty和size