书籍:C++ Primer Plus (第六版)(中文版)
工具:Dev-C++ 5.11
电脑信息:Intel® Xeon® CPU E5-2603 v3 @ 1.60GHz
系统类型:64位操作系统,基于X64的处理器 windows10 专业版
第11章 使用类
11.4 重载运算符:作为成员函数还是非成员函数
11.5 再谈重载:一个矢量类
实例11.5
vector.h
cpp
#ifndef VECTOR_H_
#define VECTOR_H_
#include <iostream>
namespace VECTOR
{
class vector
{
public:
enum Mode {RECT,POL};
private:
double x;
double y;
double mag;
double ang;
Mode mode;
void set_mag();
void set_ang();
void set_x();
void set_y();
public:
vector();
vector(double n1,double n2,Mode form = RECT);
void reset(double n1,double n2,Mode form = RECT);
~vector();
double xval() const {return x;}
double yval() const {return y;}
double magval() const {return mag;}
double angval() const {return ang;}
void polar_mode();
void rect_mode();
vector operator+(const vector & b) const;
vector operator-(const vector & b) const;
vector operator-() const;
vector operator*(double n) const;
friend vector operator*(double n,const vector & a);
friend std::ostream & operator<<(std::ostream & os,const vector & v);
};
}
#endif
vector.cpp
cpp
#include <cmath>
#include "vector.h"
using std::sqrt;
using std::sin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout;
namespace VECTOR
{
const double Rad_to_deg=45.0/atan(1.0);
void vector::set_mag()
{
mag=sqrt(x*x+y*y);
}
void vector::set_ang()
{
if(x==0.0 && y==0.0)
ang=0.0;
else
ang=atan2(y,x);
}
void vector::set_x()
{
x=mag*cos(ang);
}
void vector::set_y()
{
y=mag*sin(ang);
}
vector::vector()
{
x=y=mag=ang=0.0;
mode=RECT;
}
vector::vector(double n1,double n2,Mode form)
{
mode = form;
if(form==RECT)
{
x=n1;
y=n2;
set_mag();
set_ang();
}
else if(form==POL)
{
mag=n1;
ang=n2/Rad_to_deg;
set_x();
set_y();
}
else
{
cout<<"Incorrect 3rd argument to vector() --";
cout<<"vector set to 0\n";
x=y=mag=ang=0.0;
mode=RECT;
}
}
void vector::reset(double n1,double n2,Mode form)
{
mode = form;
if(form == RECT)
{
x=n1;
y=n2;
set_mag();
set_ang();
}
else if(form==POL)
{
mag=n1;
ang=n2/Rad_to_deg;
set_x();
set_y();
}
else
{
cout<<"Incorrect 3rd argument to vector() --";
cout<<"vector set to 0\n";
x=y=mag=ang=0.0;
mode=RECT;
}
}
vector::~vector()
{
}
void vector::polar_mode()
{
mode=POL;
}
void vector::rect_mode()
{
mode=RECT;
}
vector vector::operator+(const vector & b) const
{
return vector(x+b.x,y+b.y);
}
vector vector::operator-(const vector & b) const
{
return vector(x-b.x,y-b.y);
}
vector vector::operator*(double n) const
{
return vector(n*x,n*y);
}
vector operator*(double n,const vector & a)
{
return a*n;
}
std::ostream & operator<<(std::ostream & os,const vector & v)
{
if(v.mode==vector::RECT)
{
os<<"(x,y) = ("<<v.x<<", "<<v.y<<")";
}
else if(v.mode == vector::POL)
{
os<<"(m,a) = ("<<v.mag<<", "<<v.ang*Rad_to_deg << ")";
}
else
{
os<<"vector object mode is invalid.";
}
return os;
}
}
randwalk.cpp
cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "vector.h"
int main()
{
using namespace std;
using VECTOR::vector;
srand(time(0));
double direction;
vector step;
vector result(0.0,0.0);
unsigned long steps=0;
double target;
double dstep;
cout<<"Enter target distance (q to quit): ";
while(cin>>target)
{
cout<<"Enter step length: ";
if(!(cin>>dstep))
break;
while(result.magval() < target)
{
direction = rand()%360;
step.reset(dstep,direction,vector::POL);
result = result+step;
steps++;
}
cout<<"After "<<steps<<" steps,the subject "
"has the following location:\n";
cout<<result<<endl;
result.polar_mode();
cout<<" or\n"<<result<<endl;
cout<<"Average outward distance per step = "
<<result.magval()/steps<<endl;
steps=0;
result.reset(0.0,0.0);
cout<<"Enter target distance (q to quit): ";
}
cout<<"Bye!\n";
cin.clear();
while(cin.get()!='\n')
continue;
return 0;
}
编译运算结果:
cpp
Enter target distance (q to quit): 50
Enter step length: 2
After 256 steps,the subject has the following location:
(x,y) = (49.2231, -11.0343)
or
(m,a) = (50.4447, -12.635)
Average outward distance per step = 0.19705
Enter target distance (q to quit): 50
Enter step length: 2
After 634 steps,the subject has the following location:
(x,y) = (-43.8145, -25.971)
or
(m,a) = (50.9333, -149.343)
Average outward distance per step = 0.0803365
Enter target distance (q to quit): 50
Enter step length: 1
After 399 steps,the subject has the following location:
(x,y) = (-50.3994, -1.21405)
or
(m,a) = (50.4141, -178.62)
Average outward distance per step = 0.126351
Enter target distance (q to quit): q
Bye!
--------------------------------
Process exited after 56.52 seconds with return value 0
请按任意键继续. . .
11.6 类的自动转换和强制性类型转换
实例11.6
stonewt.h
cpp
#ifndef STONEWT_H_
#define STONEWT_H_
class Stonewt
{
private:
enum {Lbs_per_stn = 14};
int stone;
double pds_left;
double pounds;
public:
Stonewt(double lbs);
Stonewt(int stn,double lbs);
Stonewt();
~Stonewt();
void show_lbs() const;
void show_stn() const;
};
#endif
stonewt.cpp
cpp
#include <iostream>
#include "stonewt.h"
using std::cout;
Stonewt::Stonewt(double lbs)
{
stone = int(lbs)/Lbs_per_stn;
pds_left=int(lbs)%Lbs_per_stn+lbs-int(lbs);
pounds=lbs;
}
Stonewt::Stonewt(int stn,double lbs)
{
stone =stn;
pds_left=lbs;
pounds = stn*Lbs_per_stn+lbs;
}
Stonewt::Stonewt()
{
stone = pounds = pds_left=0;
}
Stonewt::~Stonewt()
{
}
void Stonewt::show_stn() const
{
cout<<stone<<" stone, "<<pds_left<<" pounds.\n";
}
void Stonewt::show_lbs() const
{
cout<<pounds<<" pounds.\n";
}
stone.cpp
cpp
#include <iostream>
#include "stonewt.h"
using std::cout;
void display(const Stonewt &st,int n) ;
int main()
{
Stonewt incognito=275;
Stonewt wolfe(285.7);
Stonewt taft(21,8);
cout<<"The celebrity weighed ";
incognito.show_stn();
cout<<"The detective weighed ";
wolfe.show_stn();
cout<<"The President weighed ";
taft.show_lbs();
incognito=276.8;
taft=325;
cout<<"After dinner,the President weighed ";
taft.show_lbs();
display(taft,2);
cout<<"The wrester weighed even more.\n";
display(422,2);
cout<<"No stone left unearned\n";
return 0;
}
void display(const Stonewt &st,int n)
{
for(int i=0;i<n;i++)
{
cout<<"Wow! ";
st.show_stn();
}
}
编译运输结果:
cpp
The celebrity weighed 19 stone, 9 pounds.
The detective weighed 20 stone, 5.7 pounds.
The President weighed 302 pounds.
After dinner,the President weighed 325 pounds.
Wow! 23 stone, 3 pounds.
Wow! 23 stone, 3 pounds.
The wrester weighed even more.
Wow! 30 stone, 2 pounds.
Wow! 30 stone, 2 pounds.
No stone left unearned
--------------------------------
Process exited after 0.1608 seconds with return value 0
请按任意键继续. . .
实例11.7
stonewt1.h
cpp
#ifndef STONEWT1_H_
#define STONEWT1_H_
class Stonewt
{
private:
enum {Lbs_per_stn = 14};
int stone;
double pds_left;
double pounds;
public:
Stonewt(double lbs);
Stonewt(int stn,double lbs);
Stonewt();
~Stonewt();
void show_lbs() const;
void show_stn() const;
operator int() const;
operator double() const;
};
#endif
stonewt1.cpp
cpp
#include <iostream>
#include "stonewt1.h"
using std::cout;
Stonewt::Stonewt(double lbs)
{
stone = int(lbs)/Lbs_per_stn;
pds_left=int(lbs)%Lbs_per_stn+lbs-int(lbs);
pounds=lbs;
}
Stonewt::Stonewt(int stn,double lbs)
{
stone =stn;
pds_left=lbs;
pounds = stn*Lbs_per_stn+lbs;
}
Stonewt::Stonewt()
{
stone = pounds = pds_left=0;
}
Stonewt::~Stonewt()
{
}
void Stonewt::show_stn() const
{
cout<<stone<<" stone, "<<pds_left<<" pounds.\n";
}
void Stonewt::show_lbs() const
{
cout<<pounds<<" pounds.\n";
}
Stonewt::operator int() const
{
return int (pounds + 0.5);
}
Stonewt::operator double() const
{
return pounds;
}
stone1.cpp
cpp
#include <iostream>
#include "stonewt1.h"
int main()
{
using std::cout;
Stonewt poppins(9,2.8);
double p_wt=poppins;
cout<<"Convert to double => ";
cout<<"poppins: "<<p_wt<<" pounds.\n";
cout<<"Convert to int=> ";
cout<<"poppins: "<<int (poppins)<<" pounds.\n";
return 0;
}
编译运输结果:
cpp
Convert to double => poppins: 128.8 pounds.
Convert to int=> poppins: 129 pounds.
--------------------------------
Process exited after 0.02376 seconds with return value 0
请按任意键继续. . .