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

C++, how do i write a c++ program that will input a number and output its f...

how do i write a c++ program that will input a number and output its factorial using the while loop

Integration, w.a.p to find outWrite a program to find the area under the cu...

w.a.p to find outWrite a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b.

What are all of the implicit member functions of the class? , What are all ...

What are all of the implicit member functions of the class? Or what are all of the functions that compiler implements for us if we don't describe one? A: 1.      copy ctor

Decode the code, Smugglers are becoming very smart day by day. Now they hav...

Smugglers are becoming very smart day by day. Now they have developed a new technique of sending their messages from one smuggler to another. In their new technology, they are send

Database, write a c++ program to update employee records

write a c++ program to update employee records

3n+1, Consider the following algorithm to generate a sequence of numbers. S...

Consider the following algorithm to generate a sequence of numbers. Start with an integer n. If n is even, divide by 2. If n is odd, multiply by 3 and add 1. Repeat this process wi

Polindrome, A palindrome is a string that reads the same from both the ends...

A palindrome is a string that reads the same from both the ends. Given a string S convert it to a palindrome by doing character replacement. Your task is to convert S to palindrome

Example of array, In this lab, please complete a given program to perform t...

In this lab, please complete a given program to perform the following tasks: 1. Allocate a 10 by 5 2D byte array dynamically. The way of allocation must be consistent with pag

C, Write a program to find the area under the curve y = f(x) between x = a ...

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve between two points can b

Define some features of external storage class in c program, Define some fe...

Define some features of external storage class in c program? The features of an external storage class variable are as follows: Storage - memory Default initial value -

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