Link to home
Start Free TrialLog in
Avatar of KalluMama
KalluMama

asked on

Conversion Constructor

I was trying to create this class...and I am required to create constructors using conversion constructor. Am I on the right path? I am getting the results, but ai m not sure if this is the reight way to do it? any suggestions...

Thanks

=======================

//classTime.h

//#include "stdafx.h"
#include <iostream>

#ifndef CLASSTIME_H
#define CLASSTIME_H

using namespace std;

class classTime
{
public:
      //Print variables
      void print() const;
      
      //Default constructor: set all the instance variables to zero
      classTime();

      //Conversion constructor: assign the arguments to the instance variables
      //Use construction list operator
      classTime(int, int, int);

      //Conversion constructor: extract the hours minutes and seconds from
      //timeSeconds and assign them to the respective instance variables
      classTime(int timeSeconds);      // timeSeconds_ < seconds24Hours

      //Conversion constructor: parse the hours minutes and seconds from
      //“hh:mm:ss” and assign them to the respective instance variables.
      classTime(const char *time);      // literal string constant "hh:mm:ss"

private:
      int hr;
      int min;
      int sec;      
};

#endif

=======================

//classTime.cpp

//#include "stdafx.h"
#include <iostream>
#include "classTime.h"

using namespace std;
//Print Variables
void classTime::print() const
{
      cout << "Hr = " << hr << endl;
      cout << "Min = " << min << endl;
      cout << "Sec = " << sec << endl;
}

//Default constructor: set all the instance variables to zero
classTime::classTime()
{
      hr   = 0;
      min  = 0;
      sec  = 0;
}

//Conversion constructor: assign the arguments to the instance variables
//Use construction list operator
classTime::classTime(int hour, int minute, int second)
{
      hr  = hour;
      min = minute;
      sec = second;
}

//Conversion constructor: extract the hours minutes and seconds from
//timeSeconds and assign them to the respective instance variables
classTime::classTime(int timeSeconds)
{
      hr  = ((timeSeconds / 60) / 60);
      min = (timeSeconds / 60) - ((timeSeconds/60)/60) * 60;
      sec = timeSeconds - ((hr * 60 * 60) + (min * 60));
}

classTime::classTime(char *time)
{

}

=======================

TestDriver.cpp

// INCLUDES
//#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>      
//#include "windows.h"

#include "classTime.h"

using namespace std;

// MACROS
#define skip1   cout << endl
#define skip2   cout << endl << endl
#define skip3   cout << endl << endl << endl

int main()
{
      cout << "\nBegin main()\n\n";
      
      //Testing the default constructor
      classTime one;
      one.print();
      skip2;

      //Testing the Conversion constructor
      classTime two(2,6,8);
      two.print();
      skip2;

      classTime three(559);
      three.print();
      skip2;

      cout << endl
         << "\nEnd - Normal Termination\n"
         << endl;
      return 0;
} // main

=======================
ASKER CERTIFIED SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
In case mentioned by efn, you must use a structure containing all the arguments, this case is ideal to use 'struct tm' datatype.
Here is an explanation about it:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_asctime.2c_._wasctime.asp

So you can implement:
    classTime::classTime(struct tm)
Avatar of Axter
FYI:
I recommend you avoid using macros as much as possible.
This is especially so, if you're just learning.  You don't want to pick bad habbits.