类模板初尝
来源:互联网 发布:网络文化建设申论 编辑:程序博客网 时间:2024/05/17 06:17
#pragma once
//Caculate.h
template<typename T> class Caculate
{
public:
Caculate() = default;
Caculate(const T& a, const T& b) :first(a), second(b){}
Caculate(const Caculate& c);
Caculate(Caculate &&)noexcept;
Caculate& operator =(const Caculate&);
Caculate& operator =(Caculate&&)noexcept;
~Caculate() = default;
public:
T sum();
T minus();
T multiplies();
T divides();
private:
T first;
T second;
};
template<typename T> Caculate<T>::Caculate(const Caculate& c):first(c.first),second(c.second)
{
}
template<typename T> Caculate<T>::Caculate(Caculate && c)noexcept : first(std::move(c.first)), second(std::move(c.second))
{
}
template<typename T> Caculate<T>& Caculate<T>::operator =(const Caculate& c)
{
first = c.first;
second = c.second;
return *this;
}
template<typename T> Caculate<T>& Caculate<T>::operator =(Caculate && c)noexcept
{
first = std::move(c.first);
second = std::move(c.second);
return *this;
}
template<typename T> T Caculate<T>::sum()
{
return (first + second);
}
template<typename T> T Caculate<T>::minus()
{
return (first - second);
}
template<typename T> T Caculate<T>::multiplies()
{
return (first * second);
}
template<typename T> T Caculate<T>::divides()
{
if (second == 0)
exit(EXIT_FAILURE);
else
return (first / second);
//Caculate.h
template<typename T> class Caculate
{
public:
Caculate() = default;
Caculate(const T& a, const T& b) :first(a), second(b){}
Caculate(const Caculate& c);
Caculate(Caculate &&)noexcept;
Caculate& operator =(const Caculate&);
Caculate& operator =(Caculate&&)noexcept;
~Caculate() = default;
public:
T sum();
T minus();
T multiplies();
T divides();
private:
T first;
T second;
};
template<typename T> Caculate<T>::Caculate(const Caculate& c):first(c.first),second(c.second)
{
}
template<typename T> Caculate<T>::Caculate(Caculate && c)noexcept : first(std::move(c.first)), second(std::move(c.second))
{
}
template<typename T> Caculate<T>& Caculate<T>::operator =(const Caculate& c)
{
first = c.first;
second = c.second;
return *this;
}
template<typename T> Caculate<T>& Caculate<T>::operator =(Caculate && c)noexcept
{
first = std::move(c.first);
second = std::move(c.second);
return *this;
}
template<typename T> T Caculate<T>::sum()
{
return (first + second);
}
template<typename T> T Caculate<T>::minus()
{
return (first - second);
}
template<typename T> T Caculate<T>::multiplies()
{
return (first * second);
}
template<typename T> T Caculate<T>::divides()
{
if (second == 0)
exit(EXIT_FAILURE);
else
return (first / second);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
#include<iostream>
#include"Caculate.h"
using namespace std;
int main()
{
Caculate<double> a(1.23, 34.444);
cout << a.divides() << endl;
cout << a.minus() << endl;
cout << a.sum() << endl;
cout << a.multiplies() << endl;
system("pause");
return 0;
}
0 0
- 类模板初尝
- 函数模板、类模板
- 模板 - 类模板
- 模板函数,模板类
- 模板的模板类
- 类模板(函数模板)
- 模板之---类模板
- C++模板:模板类
- 函数模板 类模板
- 函数模板、类模板
- C++模板学习(函数模板+类模板)
- 模板——函数模板、类模板
- 模板 Templates 模板函数 模板类
- C++模板(类模板、函数模板)
- 模板之类模板(数组类模板)
- 类模板
- 类模板
- 类模板
- 各排序算法分类以及复杂度分析
- hdu 2009
- Call back function
- [扫描线 线段树] BZOJ 4422 [Cerc2015]Cow Confinement
- 结合SQLCipher的LKDBHelper数据库加密
- 类模板初尝
- (转)ScrollView自适应ListView的四种方案
- 将腾讯视频Qlv格式转为MP4格式(亲测可用)
- bzoj2194 快速傅立叶之二
- 定位用系统自带的
- 什么是C++虚函数、虚函数的作用和使用方法
- PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to fin
- poj-2533-Longest Ordered Subsequence
- Vue基础学习