zoukankan      html  css  js  c++  java
  • operator重载

    #include <iostream>
    using namespace std;
    class A
    {
    public:
    	A(double _data = 0.0) :
    	  data(_data)
    	  {
    	  }
    	  A& operator =(const A& rhs)
    	  {
    		  data = rhs.data;
    		  return *this;
    	  }
    	  friend A operator +(const A& lhs, const A& rhs);
    	  friend A operator -(const A& lhs, const A& rhs);
    	  friend A operator *(const A& lhs, const A& rhs);
    	  friend A operator +(const A& lhs, double rhs);
    	  friend A operator +(double lhs, const A& rhs);
    	  friend A operator *(const A& lhs, double rhs);
    	  friend A operator *(double lhs, const A& rhs);
    	  friend A operator -(const A& lhs, double rhs);
    	  friend A operator -(double lhs, const A& rhs);
    	  friend ostream& operator <<(ostream& fout, A& a);
    	  //  A& operator += (const A& rhs);
    	  //  A& operator -= (const A& rhs);
    	  //  A& operator *= (const A& rhs);
    private:
    	double data;
    };
    A operator +(const A& lhs, const A& rhs)
    {
    	A res(0);
    	res.data = lhs.data + rhs.data;
    	return res;
    }
    A operator -(const A& lhs, const A& rhs)
    {
    	A res(0);
    	res.data = lhs.data - rhs.data;
    	return res;
    }
    A operator *(const A& lhs, const A& rhs)
    {
    	A res(0);
    	res.data = lhs.data * rhs.data;
    	return res;
    }
    A operator +(const A& lhs, double rhs)
    {
    	A res(0);
    	res.data = lhs.data + rhs;
    	return res;
    }
    A operator +(double lhs, const A& rhs)
    {
    	A res(0);
    	res.data = lhs + rhs.data;
    	return res;
    }
    A operator *(const A& lhs, double rhs)
    {
    	A res(0);
    	res.data = lhs.data * rhs;
    	return res;
    }
    A operator *(double lhs, const A& rhs)
    {
    	A res(0);
    	res.data = lhs * rhs.data;
    	return res;
    }
    A operator -(const A& lhs, double rhs)
    {
    	A res(0);
    	res.data = lhs.data - rhs;
    	return res;
    }
    A operator -(double lhs, const A& rhs)
    {
    	A res(0);
    	res.data = lhs - rhs.data;
    	return res;
    }
    ostream& operator <<(ostream& fout, A& a)
    {
    	fout << a.data;
    	return fout;
    }
    int main(int argc, char* argv[])
    {
    	A a(2.3);
    	A b(1.2);
    	A d(3.4);
    	A c;
    	c = a + b + d;
    	c = a + b;
    	c = a + 1.0;
    	c = a - b;
    	c = a - 1.0;
    	c = a * b;
    	c = a * 1.0;
    	cout << c << endl;
    	c = 1.0 + 2.0 * a * a - 3.0 * a * b;
    	cout << c << endl;
    	system("pause");
    	return 0;
    }
    #include <string>
    #include <fstream>
    #include <iostream>
    using namespace std;
    class Person
    {
    public:
    	Person(string name, string sex, string age) :
    	  _name(name), _sex(sex), _age(age)
    	  {
    	  }
    	  string getName() const
    	  {
    		  return _name;
    	  }
    	  string getSex() const
    	  {
    		  return _sex;
    	  }
    	  string getAge() const
    	  {
    		  return _age;
    	  }
    private:
    	string _name;
    	string _sex;
    	string _age;
    };
    
    ostream& operator<<(ostream &c, const Person& p) //写成iostream会出错的
    {
    	return c << p.getName() << "\t" << p.getSex() << "\t" << p.getAge() << "\n";
    }
    
    int main()
    {
    	ofstream outfile("out.txt");
    	if (!outfile)
    	{
    		cout << "create or open file error" << endl;
    		exit(-1);
    	}
    	Person p(string("aa"), string("Man"), string("13"));
    	outfile << p;
    	system("pause");
    	return 0;
    }
  • 相关阅读:
    [Jenkins] 批量删除构建历史
    [Android Tips] 32. 解决 Android Device Monitor 在 Mac OS X 卡住
    [Gradle] 查看项目依赖
    [Gradle] 获取 gradle 命令行参数
    [Android Tips] 31.如何将第三库引入的 Permission 删除掉
    [Gradle] 发布构件到本地仓库
    [Android Tips] 30.如何在 Android Studio 中一次性格式化所有代码
    下载安卓应用的历史版本
    设置int、float型数据的输出格式
    头文件limits—各个类型的数据的范围
  • 原文地址:https://www.cnblogs.com/byfei/p/14104713.html
Copyright © 2011-2022 走看看