Program of conversion from basic to user-defined variable, C/C++ Programming

Assignment Help:

Conversion from Basic to User-Defined variable

Consider the following example.

class Distance

                 {

                  public  : 

                                Distance(void)    // Constructor with no

                                 {                                               // argument

                                                   feet = 0;

                                                   inches = 0.0;

                                                 };

 

                                                Distance(float metres)

                                                 {

                                                   float f;                                     // Constructor with

                                                   f = 3.28 * metres;      // one argument

                                                   feet = int(f);         // also used for                          

                                                   inches = 12 * ( f - feet);// conversion                                             };

                                                                                void display(void)

                                                 {

                                                   cout << " Feet = " << feet <<",";

                                                   cout << " Inches = " << inches << endl;

                                                 };

  private :            

int feet;

float inches;

 };

void main (void)

                {

                   Distance d1 = 1.25;  // Uses 2nd constructor

                   Distance d2;                      // Uses 1st constructor 

                   float m;

        d2 = 2.0 ;             // Uses 2nd constructor

                   cout << " 1.25 metres is : " << d1.showdist() ;

                   cout << " 2.0  metres is :" << d2.showdist();

     }

Output :

                                1.25 metres is :FEET = 4 , INCHES = 1.199999        

                                2.0  metres is :FEET = 6 , INCHES = 6.719999         

The above program changes distance in metres ( basic data type) into feet and inches ( members of an object of class Distance ).

The declaration of first object d1 uses the second constructor and conversion takes place. Though, when the statement encountered is

d2 = 2.0;

The compiler first checks for an operator function for the assignment operator. If the assignment operator is not overloaded, then it uses the constructor to do the conversion.

 

 


Related Discussions:- Program of conversion from basic to user-defined variable

I need csgo wallhack and human aimbot, I need CSGO Wallhack and Human Aimbo...

I need CSGO Wallhack and Human Aimbot Project Description: Need a CSGO hack which can ESP Wallhack and Aimbot realistically. Must be VAC Proof, and ESL proof, as wel

C program for pattern star, 1 PETTERN1 (Pettern1.c)   main() { ...

1 PETTERN1 (Pettern1.c)   main() {           int i,j,k=1,a;           clrscr();           for(i=1;i           {                    if(i>=3)

Thermodynamics, the program that solve the efficiency of otto cycle

the program that solve the efficiency of otto cycle

Array, array itself is a pinter.explain

array itself is a pinter.explain

multiplication of matrices with compatibility check, Normal 0 ...

Normal 0 false false false EN-US X-NONE X-NONE MicrosoftInternetExplorer4

Explain syntax rules for writing a destructor function, Syntax rules for wr...

Syntax rules for writing a destructor function Its name is the similar as that of the class to which it belongs, except that the first character of the name is the symbol t

Explain default arguments, Default Arguments A default argument is a va...

Default Arguments A default argument is a value that is automatically assigned to a formal variable, if the actual argument from the function call is omitted. e.g. void

Member dereferencing operators, M em b e r d e r e f e r e n c ...

M em b e r d e r e f e r e n c i ng o p e r a t o r s: T h e s e op e r a t o r s w i l l b e d i s c u s s e d l a t

Padovan string, padovan string program in java // aakash , suraj , p...

padovan string program in java // aakash , suraj , prem sasi kumar kamaraj college program 1 : package test.padovanstring; public class PadovanString {     publ

Console atm machine coding, construct a console programme for a bank ATM m...

construct a console programme for a bank ATM machine.

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd