64,654
社区成员
发帖
与我相关
我的任务
分享
#ifndef CBOX_H
#define CBOX_H
#include <iostream>
using std::cout;
using std::endl;
class CBox
{
static int objectCount;
public:
// constructor
CBox(double lv = 1.0, double wv = 1.0, double hv = 1.0):m_Height(hv)
{
objectCount++;
m_Length = lv > wv ? lv : wv;
m_Width = wv < lv ? wv : lv;
cout << "CBox Constructor called." << ":\t " << objectCount << endl;
}
// copy constructor
CBox(const CBox& cpy):
m_Length(cpy.m_Length), m_Width(cpy.m_Width), m_Height(cpy.m_Height)
{
++objectCount;
cout << "CBox copy constructor called."
<< ":\t " << objectCount << endl;
}
// assignment operator function
CBox& operator=(const CBox& rhs)
{
cout << "CBox assignment operator function called." << endl;
if(this == &rhs)
return *this;
m_Length = rhs.m_Length;
m_Width = rhs.m_Width;
m_Height = rhs.m_Height;
return *this;
}
// destructor
~CBox()
{
objectCount--;
cout << "CBox Destructor called." << " :\t" << objectCount << endl;
}
public:
double Volume() const
{ return m_Length * m_Width * m_Height; }
public:
bool operator>(const CBox&) const;
bool operator>(const double&) const;
CBox operator+(const CBox& rhs) const
{
return CBox(m_Length > rhs.m_Length ? m_Length : rhs.m_Length,
m_Width > rhs.m_Width ? m_Width : rhs.m_Width,
m_Height + rhs.m_Height);
}
private:
double m_Length;
double m_Width;
double m_Height;
};
int CBox::objectCount = 0;
inline bool CBox::operator>(const CBox& rhs) const
{ return Volume() > rhs.Volume(); }
inline bool CBox::operator>(const double& value) const
{ return Volume() > value; }
inline bool operator>(const double& value, const CBox& rhs)
{ return value > rhs.Volume(); }
#endif
#ifndef CSAMPLES_H
#define CSAMPLES_H
template<class T>
class CSamples
{
public:
// constructor
CSamples(const T values[], int count);
CSamples(const T& value);
CSamples(): m_Free(0)
{
//m_Free = 0;
cout << "CSamples default constructor called." << endl;
}
// destructor
~CSamples() { cout << "CSamples destructor called." << endl; }
public:
bool Add(const T& value);
T Max() const;
private:
T m_Values[5];
int m_Free;
};
template<class T> CSamples<T>::CSamples(const T values[], int count)
{
cout << "CSamples(2) constructor called." << endl;
m_Free = count < 5 ? count : 5;
for(int i = 0; i < m_Free; ++i)
m_Values[i] = values[i];
}
template<class T>
CSamples<T>::CSamples(const T& value)
{
cout << "CSamples(1) constructor called." << endl;
m_Values[0] = value;
m_Free = 1;
}
template<class T> bool CSamples<T>::Add(const T& value)
{
bool OK = m_Free < 5;
if(OK)
m_Values[m_Free++] = value;
return OK;
}
template<class T> T CSamples<T>::Max() const
{
// error C2446: “:”: 没有从“int”到“const CBox”的转换
// 如果CBox构造函数定义为explicit时,70行会出现上面的编译错误,
// int代表是什么?const CBox代表那个?
T theMax = m_Free ? m_Values[0] : 0;
for(int i = 1; i < m_Free; ++i)
if(m_Values[i] > theMax)
theMax = m_Values[i];
return theMax;
}
#endif
#include "CBox.h"
#include "CSamples.h"
int main(void)
{
CBox boxes[] = { CBox(4.0, 5.0, 2.0), CBox(8.0, 5.0, 3.0), CBox(2.0, 3.0, 4.0) };
cout << endl;
CSamples<CBox> myboxes(boxes, sizeof boxes / sizeof CBox);
cout << endl;
CBox maxbox = myboxes.Max();
system("pause");
return 0;
}