zoukankan      html  css  js  c++  java
  • 运算符重载(++,<<,>>Data类的重载)

    // Data.cpp : 定义控制台应用程序的入口点。
    //运算符重载 自加运算符和输出输入运算符的重载   自加运算符在日期增加时可以判断平年还是闰年
    
    #include "stdafx.h"
    #include<iostream>
    using namespace std;
    class MyData //定义一个Data类有三个数据成员
    {private :
    	int year;
    	int month;
    	int day;
    
    
    public :
    	MyData (){year=0;
    	month=0;
    	day=0;}
    	MyData(int y,int m,int d)
    	{
    	year=y;
    	month=m;
    	day=d;
    	}
    	friend ostream& operator <<(ostream &,MyData &);//重载输出流 声明为友原函数
    	friend istream& operator >>(istream &,MyData &);//重载输入流 声明为友原函数
    	MyData operator ++( );//前置自加运算符重载函数声明
    
    };
    
    ostream& operator<<(ostream & output,MyData & data)//定义重载输出(Data)类流函数
    {
    
    output <<"日期是:"<<data.year<<" 年"<<data.month<<"月"<<data.day<<"日"<<endl;
    return output;
    }
    
    istream& operator >>(istream & input ,MyData & data)//定义重载输入(Data)类流函数
    {
    cout <<"请输入年月日:"<<endl;
    cout <<"请输入年: "<<endl;
    input >>data.year;
    cout <<"请输入月: "<<endl;
    input>>data.month;
    cout <<"请输入日: "<<endl;
    input>>data.day;
    return input;
    }
    
    
    MyData MyData::operator ++( )//前置自加运算符重载函数定义
    {
         
    	if ((year%4)==0&&((year%100)!=0))
    	
    			{	if(month==2)
    				 {
    				  if ( ++day>29)
    					{	
    						day=day-29;
    						month++;
    					
    					}
    				 
    				 }
    				 else if (month==1||month==7||month==3||month==5||month==8||month==10||month==12)
    				 {
    				 if ( ++day>31)
    					{	
    						day=day-31;
    						month++;
    							  if (month>12)
    									{
    										++year;
    										month=month-12;
    									}
    					
    					}
    				 
    				 
    				 }
    				else	if ( ++day>30)
    					{	
    						day=day-30;
    						month++;
    						   
    					}
    	}
    		else 
    		{
    					if(month==2)
    					 {
    					  if ( ++day>28)
    						{	
    							day=day-28;
    							month++;
    						
    						}
    					 
    					 }
    					 else if (month==1||month==7||month==3||month==5||month==8||month==10||month==12)
    					 {
    					 if ( ++day>31)
    						{	
    							day=day-31;
    							month++;
    								  if (month>12)
    										{
    											++year;
    											month=month-12;
    										}
    						
    						}
    					 
    					 
    					 }
    					else	if ( ++day>30)
    						{	
    							day=day-30;
    							month++;
    							   
    						}
    		}
    	
    		return *this;
    
    }
    
    
    
    int main()//主函数
    {
    	
    MyData data1(2012,12,1);
    //cin >>data1;
    for (int i=0;i<2000;i++)
    {++data1;
    cout << data1<<endl;
    }
    return 0;
    }

  • 相关阅读:
    什么是Map 3D 2012/ AIMS2012中的industry model?
    Windows Azure云计算学习笔记1Windows Azure简介
    图解安装Autodesk Infrastructure Map Server(AIMS) 2012
    [转]Fedora8 Linux下安装mapguide
    鼠标手,轨迹球?
    MapGuide Open Source v2.2 快速安装学习指南
    Autodesk云计算云端CAD,AutoCAD WS Android教程
    Isaac的Civil博客.COLIBRA (Civil Objects Library)介绍
    php redis 发布订阅功能(publish/subscribe)
    (转)placement new
  • 原文地址:https://www.cnblogs.com/lixingle/p/3313045.html
Copyright © 2011-2022 走看看