⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 list1905.cpp

📁 teach yourself C++ in 21 days 第五版
💻 CPP
字号:
//Listing 19.5 Passing Template Objects to and from Functions
#include <iostream>
using namespace std;

const int DefaultSize = 10;

// A trivial class for adding to arrays
class Animal
{
  public:
    // constructors
    Animal(int);
    Animal();
    ~Animal();
 
    // accessors
    int GetWeight() const { return itsWeight; }
    void SetWeight(int theWeight) { itsWeight = theWeight; }
 
    // friend operators
    friend ostream& operator<< (ostream&, const Animal&);
 
  private:
    int itsWeight;
};

// extraction operator for printing animals
ostream& operator<<
(ostream& theStream, const Animal& theAnimal)
{
   theStream << theAnimal.GetWeight();
   return theStream;
}

Animal::Animal(int weight):
itsWeight(weight)
{
   // cout << "Animal(int)" << endl;
}

Animal::Animal():
itsWeight(0)
{
   // cout << "Animal()" << endl;
}

Animal::~Animal()
{
   // cout << "Destroyed an animal..." << endl;
}

template <class T>   // declare the template and the parameter
class Array          // the class being parameterized
{
  public:
    Array(int itsSize = DefaultSize);
    Array(const Array &rhs);
    ~Array() { delete [] pType; }
 
    Array& operator=(const Array&);
    T& operator[](int offSet) { return pType[offSet]; }
    const T& operator[](int offSet) const
       { return pType[offSet]; }
    int GetSize() const { return itsSize; }

    // friend function
    template <class T>
    friend ostream& operator<< (ostream&, const Array<T>&);

  private:
    T *pType;
    int  itsSize;
};

template <class T>
ostream& operator<< (ostream& output, const Array<T>& theArray)
{
   for (int i = 0; i < theArray.itsSize; i++)
      output << "[" << i << "] " << theArray[i] << endl;
   return output;
}

// implementations follow...

// implement the Constructor
template <class T>
Array<T>::Array(int size):
itsSize(size)
{
   pType = new T[size];
   // the constructors of the type you are creating
   // should set a default value
}

// copy constructor
template <class T>
Array<T>::Array(const Array &rhs)
{
   itsSize = rhs.GetSize();
   pType = new T[itsSize];
   for (int i = 0; i < itsSize; i++)
      pType[i] = rhs[i];
}

void IntFillFunction(Array<int>& theArray);
void AnimalFillFunction(Array<Animal>& theArray);

int main()
{
   Array<int> intArray;
   Array<Animal> animalArray;
   IntFillFunction(intArray);
   AnimalFillFunction(animalArray);
   cout << "intArray...\n" << intArray;
   cout << "\nanimalArray...\n" << animalArray << endl;
   return 0;
}

void IntFillFunction(Array<int>& theArray)
{
   bool Stop = false;
   int offset, value;
   while (Stop == false)
   {
      cout << "Enter an offset (0-9) ";
      cout << "and a value. (-1 to stop): " ;
      cin >> offset >> value;
      if (offset < 0)
         break;
      if (offset > 9)
      {
         cout << "***Please use values between 0 and 9.***\n";
         continue;
      }
      theArray[offset] = value;
   }
}


void AnimalFillFunction(Array<Animal>& theArray)
{
   Animal * pAnimal;
   for (int i = 0; i < theArray.GetSize(); i++)
   {
      pAnimal = new Animal;
      pAnimal->SetWeight(i*100);
      theArray[i] = *pAnimal;
      delete pAnimal;  // a copy was put in the array
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -