OOPortal OOPortal


Constructor Functions  «Prev 

C++ Program Example using mod_int

Using the mod_int type, we can write code to convert seconds into minutes and seconds:
/***** Filename:  mod_int.cpp *****/
#include <iostream>
//using namespace std;
//Modulo numbers and constructor initialization

class mod_int {
public:
   mod_int(int i);               //constructor declaration
   void  assign(int i) { v = i % modulus; }
   void  print() const { cout << v << '\t'; }
   const static int modulus;
private:
   int  v;
};

//Default constructor for mod_int
inline  mod_int::mod_int(int i = 0)
      { v = i % modulus; }

//mod_int::mod_int(int i = 0) : v(i % modulus){}
const int mod_int::modulus = 60;

int main()
{
   int      seconds = 400;
   mod_int  z(seconds);

   cout << seconds << " seconds equals "
        << seconds / 60 << " minutes ";
   z.print();
   cout << " seconds" << endl;
   int look; cin >> look;
}