心胸决定格局,眼界决定境界...

操作符重载 友元函数 模板 拷贝重载 赋值重载 比较重载 STL 具体对象

complex.h
#pragma once
#include <iostream>
#define FRIEND_FUNC 1
#define INLINE_FUNC 1
class  ostream;
class  istream;

class Complex {
public:
    Complex();
    Complex(int real, int imag);

    //copy construction and assignment  function
    Complex(const Complex& a);    
    Complex&  operator = (const Complex & a);

    //manipulation
    void  set_real(int real);
    void  set_imag(int imag);
    int   get_real() const;
    int   get_imag() const;

    //unary operator over
    friend Complex operator + (const Complex& a, const Complex& b);
    friend Complex operator - (const Complex& a, const Complex& b);
    friend Complex operator * (const Complex& a, const Complex& b);
    friend bool operator == (const Complex& a, const Complex& b);

    //binary operator
    Complex operator -();
    Complex operator +();
    //print  information
    static void print(Complex &a);//C style
    friend std::ostream & operator << (std::ostream &os, const Complex &a);
    friend std::istream & operator >> (std::istream &os, Complex &a);

private:
    int real_;
    int imag_;
};

#ifdef INLINE_FUNC
inline Complex  operator + (const Complex& a, const Complex& b)
{
    Complex c;
#ifdef FRIEND_FUNC
    c.real_ = a.real_ + b.real_;
    c.imag_ = a.imag_ + b.imag_;
#else
    c.set_real(a.get_real() + b.get_real());
    c.set_imag(a.get_imag() + b.get_imag());
#endif
    return c;
}
inline Complex operator - (const Complex& a, const Complex& b)
{
    Complex c;
#ifdef FRIEND_FUNC
    c.real_ = a.real_ - b.real_;
    c.imag_ = a.imag_ - b.imag_;
#else
    c.set_real(a.get_real() - b.get_real());
    c.set_imag(a.get_imag() - b.get_imag());
#endif
    return c;
}

inline Complex operator * (const Complex& a, const Complex& b)
{
    Complex c;
#ifdef FRIEND_FUNC
    c.real_ = a.real_ * b.real_ - a.imag_ * b.imag_;
    c.imag_ = a.real_ * b.imag_ + a.imag_ * b.real_;
#else
    int real = a.get_real() * b.get_real() - a.get_imag() * b.get_imag();
    int imag = a.get_real() * b.get_imag() + a.get_imag() * b.get_real();
    c.set_real(real);
    c.set_imag(imag);
#endif
    return c;
}

inline bool operator == (const Complex& a, const Complex& b)
{
    bool is_equal = false;
    if ((a.real_ == b.real_) && (a.imag_ == b.imag_))
    {
        is_equal = true;
    }
    return is_equal;
}
#endif

//友元函数
//inline 函数
//添加迭代器,遍历整个集合
//重载运算符
//overload 重载  同一个类的多个方法名相同,参数列表不同
//override 重写  子类重写基类方法

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

Complex::Complex():real_(0), imag_(0) 
{

}

Complex::Complex(int real, int imag)
{
    real_ = real;
    imag_ = imag;
}

Complex::Complex(const Complex& a)
{
    real_ = a.real_;
    imag_ = a.imag_;
}
Complex&  Complex::operator = (const Complex & a)
{
    real_ = a.real_;
    imag_ = a.imag_;
    return *this;
}

void  Complex::set_real(int real)
{
    real_ = real;
}
void  Complex::set_imag(int imag)
{
    imag_ = imag;
}
int  Complex::get_real() const
{
    return real_;
}
int  Complex::get_imag() const
{
    return imag_;
}

#ifndef INLINE_FUNC
Complex  operator + (const Complex& a, const Complex& b)
{
    Complex c;
#ifdef FRIEND_FUNC
    c.real_ = a.real_ + b.real_;
    c.imag_ = a.imag_ + b.imag_;
#else
    c.set_real(a.get_real() + b.get_real());
    c.set_imag(a.get_imag() + b.get_imag());
#endif
    return c;
}

Complex operator - (const Complex& a, const Complex& b)
{
    Complex c;
#ifdef FRIEND_FUNC
    c.real_ = a.real_ - b.real_;
    c.imag_ = a.imag_ - b.imag_;
#else
    c.set_real(a.get_real() - b.get_real());
    c.set_imag(a.get_imag() - b.get_imag());
#endif
    return c;
}
#endif

Complex Complex::operator -()
{
    this->real_ = -this->real_;
    this->imag_ = -this->imag_;
    return *this;
}

#ifndef INLINE_FUNC
Complex operator * (const Complex& a, const Complex& b)
{
    Complex c;
#ifdef FRIEND_FUNC
    c.real_ = a.real_ * b.real_ - a.imag_ * b.imag_;
    c.imag_ = a.real_ * b.imag_ + a.imag_ * b.real_;
#else
    int real = a.get_real() * b.get_real() - a.get_imag() * b.get_imag();
    int imag = a.get_real() * b.get_imag() + a.get_imag() * b.get_real();
    c.set_real(real);
    c.set_imag(imag);
#endif
    return c;
}

bool operator == (const Complex& a, const Complex& b)
{
    bool is_equal = false;
    if ((a.real_ == b.real_) && (a.imag_ == b.imag_)) 
    {
        is_equal = true;
    }
    return is_equal;
}
#endif

Complex Complex::operator +()
{
    return *this;
}

void Complex::print(Complex &a) {
    std::cout << "real = " << a.get_real() << ", imag = "<< a.get_imag() <<std::endl;
}

std::ostream & operator <<(std::ostream &os, const Complex &a)
{
#ifdef FRIEND_FUNC
    os << "real = " << a.real_ << ", imag = " << a.imag_ << std::endl;
#else
    os << "real = " << a.get_real() << ", imag = " << a.get_imag() << std::endl;
#endif
    return os;
}

std::istream& operator >> (std::istream &is, Complex &a)
{
    is >> a.real_ >> a.imag_;
    return is;
}

complex_template.h
#pragma once

template<typename T>
class ComplexT {
public:
    ComplexT();
    ComplexT(T real, T imag);

    //copy construction and assignment  function
    ComplexT(const ComplexT& a);
    ComplexT&  operator = (const ComplexT & a);

    //manipulation
    void  set_real(T real);
    void  set_imag(T imag);
    T   get_real() const;
    T   get_imag() const;

private:
    T real_;
    T imag_;
};

//make sure below class define  in .h file
//if define in .cpp file, will report error
template<typename T>
ComplexT<T>::ComplexT() :real_(0), imag_(0)
{

}

template<typename T>
ComplexT<T>::ComplexT(T real, T imag)
{
    real_ = real;
    imag_ = imag;
}

template<typename T>
ComplexT<T>::ComplexT(const ComplexT<T>& a)
{
    real_ = a.real_;
    imag_ = a.imag_;
}

template<typename T>
void  ComplexT<T>::set_real(T real)
{
    real_ = real;
}

template<typename T>
void  ComplexT<T>::set_imag(T imag)
{
    imag_ = imag;
}

template<typename T>
T  ComplexT<T>::get_real() const
{
    return real_;
}

template<typename T>
T  ComplexT<T>::get_imag() const
{
    return imag_;
}
//完成通用魔板

main.cpp
// complex_iterator_operator_test.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

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

//define USE_PRINT 1
int main()
{
    Complex a(5, 3);
    Complex b(1, 2);
    Complex c;

    std::cout << "print a:";
#ifdef USE_PRINT
    Complex::print(a);
#else
    std::cout << a;
#endif

    std::cout << "print b:";
#ifdef USE_PRINT
    Complex::print(b);
#else
    std::cout << b;
#endif

    std::cout << "print c:";
#ifdef USE_PRINT
    Complex::print(c);
#else
    std::cout << c;
#endif

    std::cout << "print c = a + b:";
    c = a + b;
#ifdef USE_PRINT
    Complex::print(c);
#else
    std::cout << c;
#endif

    std::cout << "print c = a - b:";
    c = a - b;
#ifdef USE_PRINT
    Complex::print(c);
#else
    std::cout << c;
#endif

    std::cout << "print c = a * b:";
    c = a * b;
#ifdef USE_PRINT
    Complex::print(c);
#else
    std::cout << c;
#endif

//copy construction and assignment  function
    Complex d(c);
    Complex e = c;
    if (d == e) 
    {
        std::cout << "d = e" << std::endl;
    }
// test template
    ComplexT<float>  test;
}

 

posted @ 2021-02-28 15:56  WELEN  阅读(135)  评论(0编辑  收藏  举报