Home
Username:
Password:
C++ Fundamentals Tutorials

Constructors / User Defined Conversions




Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.


Learn More

Subtitles of the Movie

Hi this is Tim, welcome back to the C Plus Plus Fundamentals VTC video training course. We're going to talk about user defined conversion. Now you already know about automatic conversion of types like integers, to floats, floats to doubles, characters to integers, things like that happen implicitly and are performed by the compiler so that the correct work can be performed if you multiply a character by an integer, we're going to promote the character, cast it up to an int and then perform the work. Well the user defined conversion is when the compiler has no idea how to convert something to something else, most specifically how to convert, a type to an object and that's what we're going to do. The user defined conversion let's you specify how an object converts either with constructors or with a conversion function. The user defined conversions are implicitly used, they're not called like a cast would be. They are just used when they are using code and of course we'll look at some code here in just a second, back this up and show you what I'm talking about. Their implicitly used in addition to standard conversions to convert initializes, function arguments, function return values, expressions controlling iteration like in a looping environment, selections and explicit type conversions. Now like I said there are two types of user defined conversions. There's conversion by constructor and then there's the use of a special conversion function and we are going to look at both of those. Now here's a converting constructor and you can see this constructor is right over here that we have a class why and here we have a converting constructor that has no code in it. Neither one of these do. So if a integer I is passed in we would return Y and that would perform that conversion for us and here this one takes a character pointer in and an integer and returns Y back then of course there is just another little method down here called add to do whatever its going to do. That's just part of our class definitions. So here, over here in main we're going to be using this class and just consider that this went on in one long list, I just can't show it to you on the screen in one long list. So here, Y of object one equal to two is the same thing as making object one equal to why initialized with two and that since these is an integer, coming in here is going to use this conversion constructor and take in the number two and then convert that to an object of type Y and return that. Here we pass in a string, Y of some string and that's going to use our second converting constructor. Our second method of construction. Again here's the actual call, and this is what it's really going to look like it performs. Here again we pass a ten into a Y, that's an integer so that uses this one or object one equal to ten performs the same kind of functionality so that the number gets passed in and then the Y object is returned object one and then the ad of Y of 5 is a that's what this is equalivant to the adds of 5 and that's our little add method over here and we pass in 5 into Y and since we have a Y object and we're creating a new one here then our constructor runs and we're, since we're passing in an integer we'll use this particular converted constructor. So a constructor is the way its built here is always converting constructor, anyway a constructor with a single input variable. Now let's take a look at a conversion function. So here we have the actual conversion that is put into the class which is created to perform specific conversion, this one is going to convert from a integer to our object. So here we have the class Y and here is the definition of operator, that's the keyword there, that we have operator and that we're creating an int, operator, so that's our conversion type and that's the return of this will be the conversion type and it will be returning back our integer B, these conversion functions take no parameters, there is nothing that could come in because of the way they work and because of the work that they do they are implicitly defined to return this operator type. So we know what comes out and we know that nothing goes in. here the F function down here is going to take in a Y object and we're going to convert it to an integer and that is the whole function of this conversion function, that's why it exists. So here an integer I is equal to int of the object, we know the object is why, when this int gets used here the compiler understands to come to Y and look for an operator definition and there it is, int so B will be returned, whatever the value of that member B is so that will come back into I. J, here we have a cast an int cast of the object, casting the object to int, does the same job, find the int, returns to B and here we do an implicit conversion of I which is an integer being added to an object, the object has to be cast or converted to an integer and to do that we come and look for int, find B, add them together and put it into K, so that's our conversion function. So now we know how we can provide our own user defined conversions and we have to provide them when the compiler would not otherwise be able to know how to perform the conversions.

Tutorial Information

Course: C++ Fundamentals
Author: Tim Heagarty
SKU: 33797
ISBN: 1-934743-09-7
Release Date: 2007-09-14
Duration: 4 hrs / 55 lessons
Captions: For Online University members only
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 98,729 Video Tutorials (23,265 free)
  • Video Available as Flash or QuickTime
  • Over 1026 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available