• c++运算符重载


    C++不允许把下标运算符函数作为外部函数来定义,它只能是非静态的成员函数。

    注意操作符重载作为友元函数和成员函数的区别


    // person.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include <iostream>
    #include <string>
    #include <sstream>
    using namespace std;
    
    class MyShape {
    protected:
    	int R_, G_, B_;
    
    	string colorToString() {
    		stringstream ss;
    		ss << R_ << " " << G_ << " " << B_;
    		return ss.str();
    	}
    public:
    	void setColor(int R, int G, int B) {
    		R_ = R; G_ = G, B_ = B;
    	}
    	int getR() {
    		return R_;
    	}
    	int getG() {
    		return G_;
    	}
    	int getB() {
    		return B_;
    	}
    	virtual void Draw() = 0;
    	MyShape() {
    		R_ = 255; G_ = 255, B_ = 255;
    	}
    };
    
    class MyCircle : public MyShape{
    private:
    	int x_, y_, radius_, _size;
    	MyCircle*mMyCircle;
    public:
    	MyCircle(int x, int y, int radius) {
    		x_ = x;
    		y_ = y;
    		radius_ = radius;
    		mMyCircle = NULL;
    	}
    	MyCircle(int size) {
    		_size = size;
    		mMyCircle = new MyCircle[size];
    	}
    	MyCircle() {
    		x_ = y_ = 200;
    		radius_ = 100;
    		mMyCircle = NULL;
    	}
    
    	MyCircle(MyCircle& aCircle) {
    		x_ = aCircle.x_;
    		y_ = aCircle.y_;
    		radius_ = aCircle.radius_;
    		R_ = aCircle.getR();
    		G_ = aCircle.getG();
    		B_ = aCircle.getB();
    		mMyCircle = NULL;
    	}
    	~MyCircle()
    	{
    		if (mMyCircle != NULL)
    		delete[]mMyCircle;
    	}
    	void setCenter(int x, int y) {
    		x_ = x;
    		y_ = y;
    	}
    
    	void setRadius(int radius) {
    		radius_ = radius;
    	}
    	int getRadius() {
    		return radius_;
    	}
    
    	void Draw() {
    	}
    
    	//----在此处添加关系运算符  >、<、>=、<=、==、!=  的重载原型声明
    	friend bool operator>(const MyCircle &c1, const MyCircle &c2)//声明为友元函数
    	{
    		return c1.radius_ > c2.radius_;
    	}
    
    
    
    	bool operator<(const MyCircle &c1)
    	{
    		return this->radius_ < c1.radius_;
    	}
    	bool operator>=(const MyCircle &c1)
    	{
    		return this->radius_ >= c1.radius_;
    	}
    	bool operator<=(const MyCircle &c1)
    	{
    		return this->radius_ <= c1.radius_;
    	}
    	bool operator==(const MyCircle &c1)
    	{
    		return this->radius_ == c1.radius_;
    	}
    	bool operator!=(const MyCircle &c1)
    	{
    		return this->radius_ != c1.radius_;
    	}
    
    	MyCircle operator++()
    	{
    		radius_++;
    		return *this;
    	}
    	MyCircle operator++(int dummy)
    	{
    		MyCircle temp(*this);
    		radius_++;
    		return temp;
    	}
    	MyCircle& operator[](const int &index)//不允许声明为友元函数
    	{
    		return mMyCircle[index];
    	}
    
    };
    
    //----在此处添加关系运算符的重载定义
    
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	int r1, r2, r3 = 0;
    	cin >> r1 >> r2 >> r3;
    	MyCircle c1, c2, c3;
    	c1.setRadius(r1);
    	c2.setRadius(r2);
    	c3.setRadius(r3);
    	if (c1 > c2)
    		cout << 1 << endl;
    	if (c2 < c1)
    		cout << 5 << endl;
    
    	MyCircle aa(4);
    	for (int i = 0; i < 4; i++)
    	{
    		aa[i] = MyCircle(1, 2, i + 1);
    		cout << aa[i].getRadius();
    	}
    	system("pause");
    	return 0;
    }
    


    版权声明:

  • 相关阅读:
    数据结构——自学笔记一
    Xmind软件——xmind 8 pro下载激活推荐!!
    Java学习笔记之面向对象、static关键字
    解决“var/log/sysstat/sa21: 没有那个文件或目录 请检查是否允许数据收集”
    c++.net学习笔记
    Android Studio学习-连接真机测试教学
    Servlet总结二(文件路径)
    Servlet总结一
    多线程的使用
    Lock
  • 原文地址:https://www.cnblogs.com/walccott/p/4956880.html
Copyright © 2020-2023  润新知