C++ Primer Plus读书笔记

//函数模板

/************************************************************************/
/* 函数模板是通用的函数描述,它们可以使用通用的类型定义函数                                                                     */
/************************************************************************/
template <class T> //class也可以用typename代替
void Swap(T &a,T &b);


template <typename T>
void Show(T a[]);


//重载模板
template <typename T>
void Swap(T *a, T *b ,int n);


#include <iostream>
#include <string.h>
using namespace std ;


const int Lim = 8 ;


//显式具体化


 struct job
{
    char name[40];
    double salary;
    int floor;
};
void Show1(job &j);
template <> void Swap<job>(job &j1, job &j2);
//显式具体化 先找 Swap<job>(job &j1, job &j2)再找 Swap(T &a,T &b);
int main()
{
int i = 5 ;
int j = 6 ;
Swap(i,j);
cout<<"i:"<<i<<" j:"<<j<<endl;


double x = 4.4;
double y = 5.5;
Swap(x,y);
cout<<"x:"<<x<<" y:"<<y<<endl;




//重载模板
int d1[Lim] = {0,7,0,4,1,7,7,6};
    int d2[Lim] = {0,6,2,0,1,9,6,9};
    cout << "Original arrays:\n";
    Show(d1); 
    Show(d2);
    Swap(d1,d2,Lim);        // matches new template
    cout << "Swapped arrays:\n";
    Show(d1);
    Show(d2);




//显式具体化


job sue = {"Susan Yaffee", 73000.60, 7};
    job sidney = {"Sidney Taffee", 78060.72, 9};
    cout << "Before job swapping:\n";
    Show1(sue);
    Show1(sidney);
    Swap(sue, sidney); // uses void Swap(job &, job &)
    cout << "After job swapping:\n";
    Show1(sue);
    Show1(sidney);

return 0 ; 
}


template <typename T>
void Swap(T &a , T & b)
{
T tmp ;
tmp = a ;
a = b ;
b = tmp  ;
}


template <> void Swap<job>(job &j1, job &j2)  // specialization
{
    double t1;
    int t2;
    t1 = j1.salary;
    j1.salary = j2.salary;
    j2.salary = t1;
    t2 = j1.floor;
    j1.floor = j2.floor;
    j2.floor = t2;
}






template <typename T>
void Show(T a[])
{
cout << a[0] << a[1] << "/";
    cout << a[2] << a[3] << "/";
    for (int i = 4; i < Lim; i++)
        cout << a[i];
    cout << endl;
}
//重载模板
template <typename T>
void Swap(T a[], T b[] ,int n)
{
T tmp ;
for ( int i = 0 ; i < n ; i++ )
{
tmp = a[i] ;
a[i] = b[i];
b[i] = tmp;
}


}


void Show1(job &j)
{
    using namespace std;
    cout << j.name << ": $" << j.salary
         << " on floor " << j.floor << endl;

}



//重载及友元

//mytime3.h

// mytime3.h -- Time class with friends
#ifndef MYTIME3_H_
#define MYTIME3_H_
#include <iostream>


class Time
{
private:
    int hours;
    int minutes;
public:
    Time();
    Time(int h, int m = 0);
    void AddMin(int m);
    void AddHr(int h);
    void Reset(int h = 0, int m = 0);
    Time operator+(const Time & t) const;
    Time operator-(const Time & t) const;
    Time operator*(double n) const;

//重载操作符的左边必须是对象:2.3*Time不能调用,因此可以用友元函数
    friend Time operator*(double m, const Time & t)
        { return t * m; }   // inline definition
    friend std::ostream & operator<<(std::ostream & os, const Time & t);


};
#endif



//mytime3.cpp

// mytime3.cpp  -- implementing Time methods
#include "mytime3.h"


Time::Time()
{
    hours = minutes = 0;
}


Time::Time(int h, int m )
{
    hours = h;
    minutes = m;
}


void Time::AddMin(int m)
{
    minutes += m;
    hours += minutes / 60;
    minutes %= 60;
}
void Time::AddHr(int h)
{
    hours += h;
}


void Time::Reset(int h, int m)
{
    hours = h;
    minutes = m;
}


Time Time::operator+(const Time & t) const
{
    Time sum;
    sum.minutes = minutes + t.minutes;
    sum.hours = hours + t.hours + sum.minutes / 60;
    sum.minutes %= 60;
    return sum;
}


Time Time::operator-(const Time & t) const
{
    Time diff;
    int tot1, tot2;
    tot1 = t.minutes + 60 * t.hours;
    tot2 = minutes + 60 * hours;
    diff.minutes = (tot2 - tot1) % 60;
    diff.hours = (tot2 - tot1) / 60;
    return diff;
}


Time Time::operator*(double mult) const
{
    Time result;
    long totalminutes = hours * mult * 60 + minutes * mult;
    result.hours = totalminutes / 60;
    result.minutes = totalminutes % 60;
    return result;
}

//友元函数不是成员函数,所以不需要Time::限定符
std::ostream & operator<<(std::ostream & os, const Time & t)
{
    os << t.hours << " hours, " << t.minutes << " minutes";
    return os; 
}


//usetime3.cpp -- using the fourth draft of the Time class
// compile usetime3.cpp and mytime3.cpp together
#include <iostream>
#include "mytime3.h"


int main()
{
    using std::cout;
    using std::endl;
    Time aida(3, 35);
    Time tosca(2, 48);
    Time temp;


    cout << "Aida and Tosca:\n";
    cout << aida<<"; " << tosca << endl;
    temp = aida + tosca;     // operator+()
    cout << "Aida + Tosca: " << temp << endl;
    temp = aida* 1.17;  // member operator*()
    cout << "Aida * 1.17: " << temp << endl;
    cout << "10 * Tosca: " << 10 * tosca << endl;


    return 0; 
}


//复数操作的例子

//complex.h

#ifndef  COMPLEX_H_
#define COMPLEX_H_


#include <iostream>


class complex//定义复数类
{
private : 
double r ;//实数
double i ;//复数
public:
complex();
complex(double real);
complex(double real,double imag);
complex operator+(const complex& z)const;
complex operator-(const complex &z)const;
complex operator~()const ;//共轭
friend complex operator*(const complex &x, const complex & y);//复数乘以复数


friend std::ostream & operator<<(std::ostream &os, const complex & x);
friend std::istream & operator>>(std::istream &is, complex & x);


};

#endif




//complex.cpp



#include <iostream>
#include <cmath>
#include "complex.h"


complex::complex()
{

r=i= 0.0 ;
}
complex::complex(double real)//为了能复用complex operator*(const complex &x, const complex & y)来处理实数乘以复数的情况
{
r = real;
i = 0 ;
}
complex:: complex(double real,double imag)
{
this->r = real ;
this->i = imag ;
}


complex complex::operator+(const complex& z)const
{
complex sum ;
sum.r = r+z.r ;
sum.i = i+z.i ;
return sum ;
}


complex complex::operator-(const complex &z)const
{
complex sub ;
sub.r = r-z.r ;
sub.i = i-z.i ;
return sub ;

}


complex complex:: operator~()const //共轭
{
complex conjugate ;
conjugate.r = r ;
conjugate.i = -i ;
return conjugate ;
}




//友元函数
complex operator*(const complex &x, const complex & y)//复数乘以复数
{
complex muli;
muli.r = x.r* y.r - x.i * y.i ;
muli.i =x.r * y.i + x.i* y.r ;
return muli ;
}




std::ostream & operator<<(std::ostream &os, const complex & x)
{
os<<"("<<x.r<<","<<x.i<<")";
  return os ;
  
}


std::istream & operator>>(std::istream & is, complex & x)
{
    std::cout << "real: ";
    if (is >> x.r)
    {
        std::cout << "imaginary: ";
        is >> x.i;
    }
    return is;
}




//main.cpp


#include <iostream>
#include "complex.h"  // to avoid confusion with complex.h
int main()
{
    using std::cout;
    using std::endl;
    using std::cin;


    complex a(3.0, 4.0);   // initialize to (3,4i)
    complex c;
    cout << "Enter a complex number (q to quit):\n";
    while (cin >> c)
    {
        cout << "c is " << c << endl;
        cout << "complex conjugate is " << ~c << endl;
        cout << "a is " << a << endl;
        cout << "a + c is " << a + c << endl;
        cout << "a - c is " << a - c << endl;
        cout << "a * c is " << a * c << endl;
        cout << "2 * c is " << 2 * c << endl;
        cout << "Enter a complex number (q to quit):\n";
    }
    cout << "Done!\n";
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值