已经撸完两篇了,继续撸~~~~~~~~~~~~~~~~~~~~
1.运算符重载:
在类中定义一个特殊的函数,以便通知编译器,遇到该重载运算符时调用该函数,并由该函数来完成该运算符应该完成的操作
<函数类型><类名>::operator<重载的运算符>(<形参表>)
{
}
注意:重载的运算符必须是一个已有的合法的C++运算符。
当重载一个运算符时,该运算符的操作数个数,优先级和结合性不能改变
例子:
/**************************************Count.h******************************************************/
#pragma once
class CCount
{
public:
CCount(int s);
~CCount(void);
private:
int sum;
public:
int operator++();
int operator++(int);
};
/**************************************************Count.cpp************************************/
#include "Count.h"
CCount::CCount(int s)
{
sum = s;
}
CCount::~CCount()
{
}
int CCount::operator++()//自增运算符是前缀运算符
{
return (++sum);
}
int CCount::operator++(int)//自增运算符是后缀运算符
{
return (sum++);
}
/*********************************************主函数******************************************/
#include "Count.h"
CCount::CCount(int s)
{
sum = s;
}
CCount::~CCount()
{
}
int CCount::operator++()//自增运算符是前缀运算符
{
return (++sum);
}
int CCount::operator++(int)//自增运算符是后缀运算符
{
return (sum++);
}
2.重载二元运算符
/**************************Math.h**************************************/
class CMath
{
public:
CMath(int score);
~CMath(void);
private:
int score;
public:
int operator+(CMath myScore);
int operator+(int a);
}
/***********************Math.cpp************************************/
#include"Math.h"
CMath::CMath(int score)
{
}
CMath::~CMath()
{
}
int CMath::operator+(CMath myScore) //重载运算符
{
return(this->score+myScore.score);
}
int CMath::operator+(int a) //重载运算符
{
return( this->score+ a);
}
/*******************operatorScore.cpp**********************************/
#include" Math.h"
#include <iostream>
using namespace std;
int main()
{
CMath m1(70);
CMath m2(80);
cout<<m1+m2<<endl; //原本两个对象是不能直接相加的,需要重载运算符
cout<<m1+50<<endl; //原本对象和数字是不能直接相加的,需要重载运算符
system("pause");
return 0;
}
3.函数模板
形式: template<class 类型名>{class 类型名2}.....>
函数模板的定义
template<class Type1,class Type2>
Type1 sum(Type1 m1,Type2 m2)
{
return (m1+m2);
}
隐式实例化例子:
#include <iostream>
using namespace std;
template <class T1,class T2>
T1 sum(T1 x, T2 y)
{
return (x+y);
}
int main()
{
cout << sum(2, 'a') << endl;
cout << sum('a', 2) << endl;
cout << sum(2.2, 2.5) << endl;
cout << sum(2.2, 5) << endl;
/****分别输出99,c,4.7,7.2**********************/
system("pause");
return 0;
}
显示实例化例子:
#include <iostream>
using namespace std;
template <class T1,class T2,int baseNum>
T1 sum(T1 x, T2 y)
{
return (x+y)*baseNum;
}
int main()
{
cout << sum<int,char,2>(2, 'a') << endl;
cout << sum<char, int, 2>('a', 2) << endl;
cout << sum<float,float, 2>(2.2, 2.5)<< endl;
cout << sum<float,int, 2>(2.2, 5) << endl;
system("pause");
return 0;
}
4.类模板
#include<iostream>
using namespace std;
template<typename T1, typename T2>
class CSum
{
public:
CSum()
{
result = 0;
}
~CSum()
{
}
private:
T1 result;
public:
void sum(T1 x, T1 y)
{
result = x + y;
}
void sum(T1 x, T2 y)
{
result = x + y;
}
void show(void)
{
cout <<"result= "<< result << endl;
}
};
/*****************************main.cpp*************************/
#include "Sum.h"
using namespace std;
int main()
{
CSum<char,double> one; //char 替代T1,double 替代 T2
one.sum(2, 'a');
one.show();
system("pause");
return 0;
}