运算符重载(++,<<,>>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;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的矩阵Matrix模板实现: ```cpp #include <iostream> #include <vector> template <typename T> class Matrix { private: std::vector<std::vector<T>> data; size_t row, col; public: Matrix(size_t row, size_t col) : row(row), col(col) { data.resize(row); for (size_t i = 0; i < row; ++i) { data[i].resize(col); } } Matrix(const Matrix<T>& other) { row = other.row; col = other.col; data = other.data; } Matrix<T>& operator=(const Matrix<T>& other) { if (&other != this) { row = other.row; col = other.col; data = other.data; } return *this; } Matrix<T> operator+(const Matrix<T>& other) const { if (row != other.row || col != other.col) { throw std::invalid_argument("Matrix with different size."); } Matrix<T> res(row, col); for (size_t i = 0; i < row; ++i) { for (size_t j = 0; j < col; ++j) { res.data[i][j] = data[i][j] + other.data[i][j]; } } return res; } Matrix<T> operator-(const Matrix<T>& other) const { if (row != other.row || col != other.col) { throw std::invalid_argument("Matrix with different size."); } Matrix<T> res(row, col); for (size_t i = 0; i < row; ++i) { for (size_t j = 0; j < col; ++j) { res.data[i][j] = data[i][j] - other.data[i][j]; } } return res; } Matrix<T> operator*(const Matrix<T>& other) const { if (col != other.row) { throw std::invalid_argument("Matrix with incompatible size."); } Matrix<T> res(row, other.col); for (size_t i = 0; i < row; ++i) { for (size_t j = 0; j < other.col; ++j) { T& t = res.data[i][j]; t = 0; for (size_t k = 0; k < col; ++k) { t += data[i][k] * other.data[k][j]; } } } return res; } friend std::ostream& operator<<(std::ostream& os, const Matrix<T>& mat) { for (size_t i = 0; i < mat.row; ++i) { for (size_t j = 0; j < mat.col; ++j) { os << mat.data[i][j] << ' '; } os << '\n'; } return os; } friend std::istream& operator>>(std::istream& is, Matrix<T>& mat) { for (size_t i = 0; i < mat.row; ++i) { for (size_t j = 0; j < mat.col; ++j) { is >> mat.data[i][j]; } } return is; } }; ``` 使用示例: ```cpp int main() { Matrix<int> a(2, 3); Matrix<int> b(3, 2); std::cin >> a >> b; std::cout << a + b << '\n'; std::cout << a - b << '\n'; std::cout << a * b << '\n'; return 0; } ``` 其中,Matrix<int>代表一个int型的矩阵,可以根据需要替换为其他数据型。在main函数中使用了输入输出流重载运算符<<,>>。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值