命名空间
以下为小甲鱼课件中的内容,分别在main.cpp rational.cpp和 rational.h文件中分别为主调用函数,类的具体实现和类的声明
#include "rational.h"
#include <iostream>
int main()
{
myMath::Rational f1(2, 16);
myMath::Rational f2(7, 8);
// 测试有理数加法运算
std::cout << f1 << " + " << f2 << " == " << (f1+f2) << "\n";
// 测试有理数减法运算
std::cout << f1 << " - " << f2 << " == " << (f1-f2) << "\n";
// 测试有理数乘法运算
std::cout << f1 << " * " << f2 << " == " << (f1*f2) << "\n";
// 测试有理数除法运算
std::cout << f1 << " / " << f2 << " == " << (f1/f2) << "\n";
return 0;
}
#include "rational.h"
#include <iostream>
#include <stdlib.h>
namespace myMath
{
Rational::Rational(int num, int denom)
{
numerator = num;
denominator = denom;
normalize();
}
// normalize() 对分数进行简化操作包括:
// 1. 只允许分子为负数,如果分母为负数则把负数挪到分子部分,如 1/-2 == -1/2
// 2. 利用欧几里德算法(辗转求余原理)将分数进行简化:2/10 => 1/5
void Rational::normalize()
{
// 确保分母为正
if( denominator < 0 )
{
numerator = -numerator;
denominator = -denominator;
}
// 欧几里德算法
int a = abs(numerator);
int b = abs(denominator);
// 求出最大公约数
while( b > 0 )
{
int t = a % b;
a = b;
b = t;
}
// 分子、分母分别除以最大公约数得到最简化分数
numerator /= a;
denominator /= a;
}
// a c a*d c*b a*d + c*b
// - + - = --- + --- = ---------
// b d b*d b*d = b*d
Rational Rational::operator+(Rational rhs)
{
int a = numerator;
int b = denominator;
int c = rhs.numerator;
int d = rhs.denominator;
int e = a*b + c*d;
int f = b*d;
return Rational(e, f);
}
// a c a -c
// - - - = - + --
// b d b d
Rational Rational::operator-(Rational rhs)
{
rhs.numerator = -rhs.numerator;
return operator+(rhs);
}
// a c a*c
// - * - = ---
// b d b*d
Rational Rational::operator*(Rational rhs)
{
int a = numerator;
int b = denominator;
int c = rhs.numerator;
int d = rhs.denominator;
int e = a*c;
int f = b*d;
return Rational(e, f);
}
// a c a d
// - / - = - * -
// b d b c
Rational Rational::operator/(Rational rhs)
{
int t = rhs.numerator;
rhs.numerator = rhs.denominator;
rhs.denominator = t;
return operator*(rhs);
}
std::ostream& operator<<(std::ostream& os, Rational f)
{
os << f.numerator << "/" << f.denominator;
return os;
}
}
// Rational.h
// Create by 小甲鱼
// 这个头文件声明了有理数类(Rational class)
// 类里边对四则运算进行重载,以实现分数运算
#ifndef RATIONAL_H
#define RATIONAL_H
#include <iostream>
namespace myMath
{
class Rational
{
public:
Rational(int num, int denom);// num = 分子, denom = 分母
Rational operator+(Rational rhs); // rhs == right hand side
Rational operator-(Rational rhs);
Rational operator*(Rational rhs);
Rational operator/(Rational rhs);
private:
void normalize(); // 负责对分数的简化处理
int numerator; // 分子
int denominator;// 分母
friend std::ostream& operator<<(std::ostream& os, Rational f);
};
}
#endif
我的问题是:
在主调文件main.cpp文件和在rational.cpp(类的实现文件) 中都进行了#include"rational.h"操作,
主调函数main.cpp是通过何种方式和rational.cpp(类的实现)联系起来的?毕竟只是#include"rational.h" 而没有#include"rational.cpp",
也即只有声明没有定义?
好吧 上一回忘记发悬赏了,有人看没人答。。。。 这次发悬赏。。。。
竟无语凝噎 发表于 2018-12-27 10:45
能在说的简单一些么。。。小白有些不是很懂 , 在linux下,这里您是既编译了main 又编译了。
但我在wi ...
继续往后学吧,不用纠结这些细节,学到后面你自然就会明白
ide帮你做了好多事
1.编译main.cpp
2.编译rational.cpp
3.合并第1步和第2步的结果
sh-4.4$ g++ -c -o main.o main.cpp
sh-4.4$ g++ -c -o rational.o rational.cpp
sh-4.4$ g++ -o main main.o rational.o
sh-4.4$ main.exe
sh: main.exe: command not found
sh-4.4$ ./main.exe
1/8 + 7/8 == 1/1
1/8 - 7/8 == -3/4
1/8 * 7/8 == 7/64
1/8 / 7/8 == 1/7
sh-4.4$
sh-4.4$
sh-4.4$ rm main.exe main.o rational.o
sh-4.4$ g++ -o main main.cpp rational.cpp
sh-4.4$ ./main.exe
1/8 + 7/8 == 1/1
1/8 - 7/8 == -3/4
1/8 * 7/8 == 7/64
1/8 / 7/8 == 1/7
sh-4.4$
g++ -o main main.cpp rational.cpp
你当然可以用这一条命令,编译器在内部帮你做上面的步骤
sh-4.4$ g++ -E main.cpp
# 1 "main.cpp"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "main.cpp"
# 1 "rational.h" 1
# 10 "rational.h"
# 1 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream" 1 3
# 36 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream" 3
# 37 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream" 3
# 1 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/x86_64-pc-cygwin/bits/c++config.h" 1 3
# 229 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/x86_64-pc-cygwin/bits/c++config.h" 3
# 229 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/x86_64-pc-cygwin/bits/c++config.h" 3
namespace std
{
typedef long unsigned int size_t;
typedef long int ptrdiff_t;
typedef decltype(nullptr) nullptr_t;
}
# 533 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/x86_64-pc-cygwin/bits/c++config.h" 3
# 1 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/x86_64-pc-cygwin/bits/os_defines.h" 1 3
# 534 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/x86_64-pc-cygwin/bits/c++config.h" 2 3
.... // 这里省略很多不重要的内容(对当前问题来说不重要)
# 41 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream" 2 3
namespace std
{
# 60 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream" 3
extern istream cin;
extern ostream cout;
extern ostream cerr;
extern ostream clog;
extern wistream wcin;
extern wostream wcout;
extern wostream wcerr;
extern wostream wclog;
static ios_base::Init __ioinit;
}
# 11 "rational.h" 2
# 12 "rational.h"
namespace myMath
{
class Rational
{
public:
Rational(int num, int denom);
Rational operator+(Rational rhs);
Rational operator-(Rational rhs);
Rational operator*(Rational rhs);
Rational operator/(Rational rhs);
private:
void normalize();
int numerator;
int denominator;
friend std::ostream& operator<<(std::ostream& os, Rational f);
};
}
# 2 "main.cpp" 2
int main()
{
myMath::Rational f1(2, 16);
myMath::Rational f2(7, 8);
std::cout << f1 << " + " << f2 << " == " << (f1+f2) << "\n";
std::cout << f1 << " - " << f2 << " == " << (f1-f2) << "\n";
std::cout << f1 << " * " << f2 << " == " << (f1*f2) << "\n";
std::cout << f1 << " / " << f2 << " == " << (f1/f2) << "\n";
return 0;
}
这是预处理后的结果,可以看到.h的内容已经include了,之后把这个送给编译器编译,编译器既看到了.h的内容又看到了.c的内容
g++ -E rational.cpp 让编译器只执行预处理,不进行后面的编译和链接,直接输出预处理后的结果
# 1 "rational.cpp"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "rational.cpp"
# 1 "rational.h" 1
# 10 "rational.h"
# 1 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream" 1 3
# 36 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream" 3
# 37 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream" 3
# 1 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/x86_64-pc-cygwin/bits/c++config.h" 1 3
# 229 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/x86_64-pc-cygwin/bits/c++config.h" 3
# 229 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/x86_64-pc-cygwin/bits/c++config.h" 3
namespace std
{
typedef long unsigned int size_t;
typedef long int ptrdiff_t;
.......
extern wostream wcerr;
extern wostream wclog;
static ios_base::Init __ioinit;
}
# 11 "rational.h" 2
# 12 "rational.h"
namespace myMath
{
class Rational
{
public:
Rational(int num, int denom);
Rational operator+(Rational rhs);
Rational operator-(Rational rhs);
Rational operator*(Rational rhs);
Rational operator/(Rational rhs);
private:
void normalize();
int numerator;
int denominator;
friend std::ostream& operator<<(std::ostream& os, Rational f);
};
}
# 2 "rational.cpp" 2
# 1 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/stdlib.h" 1 3
# 36 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/stdlib.h" 3
# 1 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/cstdlib" 1 3
# 39 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/cstdlib" 3
# 40 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/cstdlib" 3
# 37 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/stdlib.h" 2 3
# 38 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/stdlib.h" 3
using std::abort;
using std::atexit;
using std::exit;
using std::at_quick_exit;
using std::quick_exit;
using std::div_t;
using std::ldiv_t;
using std::abs;
using std::atof;
using std::atoi;
using std::atol;
using std::bsearch;
using std::calloc;
using std::div;
using std::free;
using std::getenv;
using std::labs;
using std::ldiv;
using std::malloc;
using std::mblen;
using std::mbstowcs;
using std::mbtowc;
using std::qsort;
using std::rand;
using std::realloc;
using std::srand;
using std::strtod;
using std::strtol;
using std::strtoul;
using std::system;
using std::wcstombs;
using std::wctomb;
# 4 "rational.cpp" 2
# 5 "rational.cpp"
namespace myMath
{
Rational::Rational(int num, int denom)
{
numerator = num;
denominator = denom;
normalize();
}
void Rational::normalize()
{
if( denominator < 0 )
{
numerator = -numerator;
denominator = -denominator;
}
int a = abs(numerator);
int b = abs(denominator);
while( b > 0 )
{
int t = a % b;
a = b;
b = t;
}
numerator /= a;
denominator /= a;
}
Rational Rational::operator+(Rational rhs)
{
int a = numerator;
int b = denominator;
int c = rhs.numerator;
int d = rhs.denominator;
int e = a*b + c*d;
int f = b*d;
return Rational(e, f);
}
Rational Rational::operator-(Rational rhs)
{
rhs.numerator = -rhs.numerator;
return operator+(rhs);
}
Rational Rational::operator*(Rational rhs)
{
int a = numerator;
int b = denominator;
int c = rhs.numerator;
int d = rhs.denominator;
int e = a*c;
int f = b*d;
return Rational(e, f);
}
Rational Rational::operator/(Rational rhs)
{
int t = rhs.numerator;
rhs.numerator = rhs.denominator;
rhs.denominator = t;
return operator*(rhs);
}
std::ostream& operator<<(std::ostream& os, Rational f)
{
os << f.numerator << "/" << f.denominator;
return os;
}
}
人造人 发表于 2018-12-26 12:29
ide帮你做了好多事
1.编译main.cpp
2.编译rational.cpp
能在说的简单一些么。。。小白有些不是很懂 , 在linux下,这里您是既编译了main 又编译了。
但我在windows下直接编译运行main就可以直接跑起来,没有显示的编译运行rational.cpp 表示不懂 千锦襄 发表于 2018-12-27 19:22
表示不懂
这样,如果楼主有兴趣可以了解一下编译原理。就本贴来说,rational.cpp肯定是要编译的。main.cpp编译也产生一个目标模块,最后由链接程序将rational.obj链接到main中,生成exe文件。就先简单这样理解吧,编译原理还是挺难得一门课。 小酒酒呢 发表于 2018-12-28 08:21
这样,如果楼主有兴趣可以了解一下编译原理。就本贴来说,rational.cpp肯定是要编译的。main.cpp编译也产 ...
谢谢
页:
[1]