?
Solved

Conversion Constructor

Posted on 2004-11-29
4
Medium Priority
?
343 Views
Last Modified: 2006-11-17
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

=======================
0
Comment
Question by:KalluMama
  • 2
4 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 1600 total points
ID: 12702766
Conversion constructors are constructors that use argument of a different data type. So, basically you are right.
There are typical C datatypes for file manipulations like time_t (well known as Unix time) and struct tm. So you can prepare constructors for these types.
0
 
LVL 15

Assisted Solution

by:efn
efn earned 400 total points
ID: 12702776
I think you are on the right track.

Technically, the constructor with three parameters is not a conversion constructor.  A conversion constructor has only one parameter.

It would be a minor improvement to use initialization lists, e.g.:

classTime::classTime(int hour, int minute, int second)
: hr(hour), min(minute), sec(second)
{}

--efn
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12702805
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)
0
 
LVL 30

Expert Comment

by:Axter
ID: 12703339
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.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question