C++语言基础 例程 重载流插入运算符和流提取运算符

贺老师的教学链接  本课讲解

重载流插入运算符“<<”

#include <iostream>
using namespace std;
class Complex
{
public:
    Complex( )
    {
        real=0;
        imag=0;
    }
    Complex(double r,double i)
    {
        real=r;
        imag=i;
    }
    Complex operator + (Complex &c2);                    //运算符“+”重载为成员函数
    friend ostream& operator << (ostream&, const Complex&);     //运算符“<<”重载为友元函数
private:
    double real;
    double imag;
};

Complex Complex::operator + (Complex &c2)               //定义运算符“+”重载函数
{
    return Complex(real+c2.real,imag+c2.imag);
}

ostream& operator << (ostream& output,const Complex& c)       //定义运算符“<<”重载函数
{
    output<<"("<<c.real<<"+"<<c.imag<<"i)";
    return output;
}

int main( )
{
    Complex c1(2,4),c2(6,10),c3;
    c3=c1+c2;
    cout<<c1<<'+'<<c2<<'='<<c3<<endl;
    return 0;
}
/* 去掉operator << 中的const后,将不能支持cout<<c1+c2<<endl;式的输出  */
 

重载流提取运算符“>>”

#include <iostream>
using namespace std;
class Complex
{
public:
    friend ostream& operator << (ostream&,const Complex&);    //声明重载运算符“<<”
    friend istream& operator >> (istream&,Complex&);    //声明重载运算符“>>”
private:
    double real;
    double imag;
};
ostream& operator << (ostream& output,const Complex& c)       //定义重载运算符“<<”
{
    output<<"("<<c.real<<"+"<<c.imag<<"i)";
    return output;
}
istream& operator >> (istream& input,Complex& c)     //定义重载运算符“>>”
{
    cout<<"input real part and imaginary part: ";
    input>>c.real>>c.imag;
    return input;
}
int main( )
{
    Complex c1,c2;
    cin>>c1>>c2;
    cout<<"c1="<<c1<<endl;
    cout<<"c2="<<c2<<endl;
    return 0;
}

对复数输入输出的完善

#include <iostream>
using namespace std;
class Complex
{
public:
    Complex( )
    {
        real=0;
        imag=0;
    }
    Complex(double r,double i)
    {
        real=r;
        imag=i;
    }
    Complex operator + (Complex &c2);                   //运算符“+”重载为成员函数
    friend ostream& operator << (ostream&,const Complex&);    //声明重载运算符“<<”
    friend istream& operator >> (istream&,Complex&);    //声明重载运算符“>>”
private:
    double real;
    double imag;
};

Complex Complex::operator + (Complex &c2)               //定义运算符“+”重载函数
{
    return Complex(real+c2.real,imag+c2.imag);
}

ostream& operator << (ostream& output, const Complex& c)       //定义重载运算符“<<”
{
    output<<"("<<c.real;
    if(c.imag>=0) output<<"+";                  //虚部为正数时,在虚部前加“+”号
    output<<c.imag<<"i)";                 //虚部为负数时,在虚部前不加“+”号
    return output;
}
istream& operator >> (istream& input,Complex& c)     //定义重载运算符“>>”
{
    int a,b;
    char sign,i;
    do
    {
        cout<<"input a complex number(a+bi或a-bi):";
        input>>a>>sign>>b>>i;
    }
    while(!((sign=='+'||sign=='-')&&i=='i'))
    c.real=a;
    c.imag=(sign=='+')?b:-b;
    return input;
}

int main( )
{
    Complex c1,c2(6,10),c3;
    cin>>c1
    cout<<"c1="<<c1<<endl;
    cout<<"c2="<<c2<<endl
    c3=c1+c2;
    cout<<"c1+c2="<<c3<<endl;
    return 0;
}
时间: 2024-09-16 22:10:15

C++语言基础 例程 重载流插入运算符和流提取运算符的相关文章

C++语言基础 例程 重载单目运算符

贺老师的教学链接  本课讲解 示例1:分数类对象的相反数 class CFraction { private: int nume; // 分子 int deno; // 分母 public: CFraction(int nu=0,int de=1):nume(nu),deno(de) {} CFraction operator-(const CFraction &c); //两个分数相减,结果要化简 CFraction operator-(); //取反一目运算 }; // 分数相减 CFrac

C++语言基础 例程 重载双目运算符

贺老师的教学链接  本课讲解 String类运算符重载函数 #include<iostream> #include<cstring> using namespace std; class String { public: String( ){p=NULL;} String(char *str); friend bool operator>(String &s1, String &s2); friend bool operator<(String &

C++语言基础 例程 字符串流

贺老师的教学链接  本课讲解 例:"写"字符数组 #include<iostream> #include <strstream> using namespace std; struct student { int num; char name[20]; float score; }; int main( ) { student stud[3]= {1001,"Li",78,1002,"Wang",89.5,1004,&qu

C++语言基础 例程 函数重载

贺老师的教学链接 重载函数:同名同体,但接口不同 #include <iostream> using namespace std; int max(int a,int b,int c); //函数声明 double max(double a,double b,double c); long max(long a,long b,long c); int main( ) { int i1,i2,i3,i; cin>>i1>>i2>>i3; //输入3个整数 i=

C++语言基础 例程 C++的输入输出与流对象

贺老师的教学链接  本课讲解 体会缓冲区 #include <iostream> using namespace std; int main() { int n[5]; for(int i=0; i<5; i++) { cin>>n[i]; cout<<n[i]<<endl; } return 0; }

C++语言基础 例程 标准输入流

贺老师的教学链接  本课讲解 例: 输入个数不确定的成绩 #include <iostream> using namespace std; int main( ) { float grade; cout<<"enter grade:"; while(cin>>grade)//能从cin流读取数据 { if(grade>=85) cout<<grade<<" GOOD!"<<endl; if

C++语言基础 例程 文件的随机读写

贺老师的教学链接  本课讲解 示例:写到尾再从头读 #include <iostream> #include <fstream> #include <cstdlib> using namespace std; int main( ) { int a[10], b[10]; fstream iofile("f1.dat",ios::in|ios::out); if(!iofile) { cerr<<"open error!&quo

C++语言基础 例程 不同类型数据间的转换

贺老师的教学链接  本课讲解 //类型转换函数应用 #include <iostream> using namespace std; class Complex { public: Complex( ) { real=0; imag=0; } Complex(double r,double i) { real=r; imag=i; } operator double( ); friend Complex operator + (Complex c1,Complex c2); private:

C++语言基础 例程 案例:Time类的设计

贺老师的教学链接  本课讲解 #include <iostream> using namespace std; class CTime { private: unsigned short int hour; // 时 unsigned short int minute; // 分 unsigned short int second; // 秒 public: CTime(int h=0,int m=0,int s=0); void setTime(int h,int m,int s); //输