C + + operator overload (1) Time Add __c++

Source: Internet
Author: User

The purpose of overloaded operators:

The operations object for predefined operators in C + + is limited to basic built-in data types

Most of the time we need to do similar operations on our similar types, and this time we need to redefine the operators and give them new content

Give them new functionality to meet their own needs

The essence of operator overloading is function overloading or function polymorphism. Operator overloading is a form of C + + polymorphism. The goal is to allow people to perform different basic operations with a function of the same name.

The basic format is as follows:

< return type descriptor > operator < operator symbol > (< parameter table >)  
     < function body >  
One use case is as follows:

We construct a time class to realize the addition of time to time and to find the current time.

#include <iostream>
using namespace std;

Class Cmytime {

	int m_hours;
	int m_minutes;
	//constructor and destructor
	Cmytime ();
	Cmytime (int h, int m = 0);
	~cmytime ();

	void addmin (int m); The addition of minutes
	//void addhr (int h);  The hours are added
	//void Reset (int h = 0, int m = 0);  Reset
	cmytime* Sum (const cmytime *t) const;  Sum of Time: Add Method 1
	cmytime operator+ (const cmytime t) const;//Time Add: Add mode 2
	//Time Add way 3   pointer add:

	void Show () const;  Show Results

We can implement a function by calling a function with a time object and adding another time object.

cmytime* cmytime::sum (const cmytime *t) const {
	cmytime* Sum = new Cmytime ();  Property in the class
	sum->m_minutes = T->m_minutes + this->m_minutes;
	Sum->m_hours = t->m_hours + this->m_hours + sum->m_minutes/60;

	return sum;
The method is invoked as follows:

	The time used to add
	cmytime* planningtime = new Cmytime ();   Default time
	cmytime* coding = new Cmytime (2);    
	cmytime* fixing = new Cmytime (5);

	Planningtime->show ();
	Coding->show ();
	Fixing->show ();
	Time sum

	cmytime* total = new Cmytime ();  
	Total = coding->sum (fixing);     Add function call
	cout << "After the sum:  " << Endl;
	Total->show ();

In the above example, the time object coding by calling the custom add function sum to add the time object fixing to it

It would be a bit cumbersome to use.

We can add it by overloading the operator.

The following methods are used: first overload the operator with the following methods

Cmytime cmytime::operator+ (const cmytime t) const   
	cmytime sum;
	Sum.m_minutes = t.m_minutes + this->m_minutes;
	Sum.m_hours = t.m_hours + this->m_hours + sum.m_minutes/60;

	return sum;

Here's the point: we can add two class objects using a method like integer addition

Time sum: Add Way 2
	cmytime planning2;
	Cmytime coding2 (2,40);
	Cmytime fixing2 (5,55);

	Cmytime Sumtime;
	cout << Endl << Endl;
	cout << "After the sum of Method 2:  " << Endl;
	Sumtime = Coding2 + fixing2;  After overloading, the plus sign can add two Cmytime class objects directly
	sumtime.show ();

This is the simplest example and there will be more ways and examples to introduce

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.