`
mylove2060
  • 浏览: 331362 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

operator-运算符重载"="

阅读更多
//Programmer.h
#pragma once
#include <iostream>  
#include <string>
class Programmer
{
public:
	Programmer() {};
	~Programmer() {};
	void SetName(std::string &in_name) { m_name = in_name; }
	void SetLanguage(std::string &in_language) { m_language = in_language; }
	void SetLevel(int in_level) { m_level = in_level; }
	std::string GetName() { return m_name; }
	std::string GetLanguage() { return m_language; }
	int GetLevel() { return m_level; }
	Programmer& operator= (const Programmer& in_programmer)
	{
		if ( this != &in_programmer )
		{
			m_name = in_programmer.m_name;
			m_language = in_programmer.m_language;
			m_level = in_programmer.m_level;
		}
		return *this;
	}
private:
	std::string m_name;
	std::string m_language;
	int m_level;
};





#include "stdafx.h"   
#include "programmer.h"
int _tmain(int argc, _TCHAR* argv[])   
{   
	Programmer a;
	std::string name = "Kevin Chen";
	std::string language = "C++";
	int level = 1;
	a.SetName(name);
	a.SetLanguage(language);
	a.SetLevel(level);
	Programmer b = a; // copy the values from a
	std::cout<<a.GetName()<<std::endl;
	std::cout<<b.GetName()<<std::endl;
	std::cout<<a.GetLanguage()<<std::endl;
	std::cout<<b.GetLanguage()<<std::endl;
	std::cout<<a.GetLevel()<<std::endl;
	std::cout<<b.GetLevel()<<std::endl;
	
	std::string name2 = "Jack Chen";
	std::string language2 = "C++";
	int level2 = 2;
	b.SetName(name2);
	b.SetLanguage(language2);
	b.SetLevel(level2);
	std::cout<<a.GetName()<<std::endl;
	std::cout<<b.GetName()<<std::endl;
	std::cout<<a.GetLanguage()<<std::endl;
	std::cout<<b.GetLanguage()<<std::endl;
	std::cout<<a.GetLevel()<<std::endl;
	std::cout<<b.GetLevel()<<std::endl;

	return 0;   
}  




输出结果:
Kevin Chen
Kevin Chen
C++
C++
1
1
Kevin Chen
Jack Chen
C++
C++
1
2

分享到:
评论

相关推荐

    C++实验报告实验七-运算符重载

    实验七 运算符重载 一、实验目的 (1).编写程序熟悉运算符重载函数的定义和使用; (2).编写程序熟悉重载单目和双目运算符的方法; (3).编写程序熟悉流插入和提取运算符的重载; 二、实验要求 1、创建一个二维...

    operator-overloading-js, JS简单运算符重载库.zip

    operator-overloading-js, JS简单运算符重载库 目录Operator-Overloading-JS安装工具node MODULE ( NPM )浏览器( Bower )Sneak示例重载运算符设计考虑/非常重要/必须阅读。Definig使用运算符重载的上下文

    复数的运算 运算符重载.cpp

    # 运算符重载 [TOC] ## 1.定义 运算符重载的方法是定义一个重载运算符的函数,在需要执行被重载的运算符时,系统就自动调用该函数,以实现相应的运算。也就是说,运算符重载是通过定义函数实现的。运算符重载实质...

    C++运算符重载 详细介绍PPT

    详细介绍 C++运算符重载 的PPT 文中部分内容: 下标运算符“[ ]”重载 C++把下标运算符[ ]看成一个双目运算符,其操作数为&lt;基本表达式&gt;和&lt;表达式&gt;,对应的运算符为operator[ ],必须重载为一个成员函数。对于下标...

    运算符重载的部分代码

    visual basic 2005 技术内部中第六章第七节运算符重载代码。 operator部分: Module Module1 Sub Main() End Sub End Module Public Structure Fraction 'Read-Only fields Private num As Long Private ...

    babel-operator-overload-plugin:Babel插件,用于运算符重载

    Babel插件,用于操作员重载。 有一个简单的模板项目。 这是基于( 的 例子 以下代码将两个整数相加,然后相加两个点。 需要使用开头的指令来启用转换。 'babel-operator-overload-plugin enabled' class Point ...

    实现一个complex class.(数学里的复数)练习运算符的重载。

    实现一个complex class.(数学里的复数)练习运算符的重载。 要求具有以下操作:  构造函数  析构函数  拷贝构造函数  operator=  operator*  operator+  operator-  operator/  operator==  ...

    复数运算和运算符的重载

    实现一个complex class.(数学里的复数)练习运算符的重载。 要求具有以下操作:  构造函数  析构函数  拷贝构造函数  operator=  operator*  operator+  operator-  operator/  operator==  ...

    C#重载运算符详解

    二、所有的运算符重载都必须声明为public和static 三、不同于扩展方法,所重载的方法必须是在被重载的类型内部,且用关键字operator C#中的两个字符串相加,实际上是连接两个字符串,假如有两个EmployeeDetail类型...

    c++运算符重载实现复数的四则运算

    #include using std::cout; using std::cin; using std::endl; class complex { public : friend complex operator -(complex &a, complex &b) { complex c; c.s=a.s-b.s; c.x=a.x-b.x; return c; }

    运算符重载总结C++

    函数类型 operator 运算符名称(形参列表) {对运算符重载的处理}

    实验五、运算符重载 复数+-*/ 大数+-*/

    运算符重载,可作为模板使用 完整的程序,易读的代码。 BigInteger operator +(const BigInteger& B); BigInteger operator -(const BigInteger& B); BigInteger operator *(const BigInteger& B); BigInteger ...

    运算符重载

    运算符重载.txt operator 1234567890

    C++实践Time类中的运算符重载参考方法

    【项目-Time类中的运算符重载】 实现Time类中的运算符重载。 class CTime { private: unsigned short int hour; // 时 unsigned short int minute; // 分 unsigned short int second; // 秒 public: CTime(int ...

    重载函数的运行

    对Point类重载“++”、“--”运算符,实现坐标值的改变。 #include using namespace std; class point { public: point(){}; point(int a,int b) { x=a; y=b; } point operator++(); point operator++(int)...

    C++ 自增、自减运算符的重载和性能分析小结

    01 ++、–运算符重载函数的格式 自增运算符和自减运算符是有前置和后置之分的,如: a++ // 后置自增运算符 ++a // 前置自增运算符 b-- // 后置自减运算符 --b // 前置自减运算符 为了区分所重载的是前置运算符...

    C++运算符重载的方法详细解析

    运算符重载实质上是函数的重载 重载运算符的函数一般格式如下: 函数类型 operator 运算符名称 (形参表列) {对运算符的重载处理} 例如,想将“+”用于Complex(复数)的加法运算,函数的原型可以是这样的: ...

    C++上机实验报告实验六.doc

    掌握运算符重载的方法 2.学习使用虚函数实现动态多态性 实验要求 1.定义Point类,有坐标_x,_y两个成员变量;对Point类重载"++"(自增)、"――"(自减 )运算符,实现对坐标值的改变。 2.定义一个车(vehiele)基类,有...

    字符串、运算符重载

    String operator =(String &s); String operator +(String &s); int operator &gt;(String &s); int operator &lt;(String &s); int operator &lt;=(String &s); int operator &gt;=(String &s); String operator++(); ...

    visuall C++ 运算符重载详细总结

    对运算符进行了详细的解析和总结,特别对“”"&gt;&gt;"运算符进行了详细的总结和分析了其工作原理。对运算符重载用不同的方法进行了总结。

Global site tag (gtag.js) - Google Analytics