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

Constructor, how to use and what is implicit and explicit constructor

how to use and what is implicit and explicit constructor

Write a c program to input five numbers, Write a C program to input five nu...

Write a C program to input five numbers and print them out on a new line e.g 5, 10, 15, 20, 25 displayed as   5   10   15   20   25 #include stdio.h   void main()   {

Networking program development, Networking program development. 1.ARP pr...

Networking program development. 1.ARP protocol. 2.Switching HUB. 3.wireshark. 4.winpcap library. 5.C++ & MFC. 6.LAN evironment through switch and HUB(static ARP t

Describe processing an array, Describe Processing an array? The Single ...

Describe Processing an array? The Single operations which involve complete arrays are not permitted in C language. therefore if a and b are similar arrays (for example same dim

Pseudocode , Record separation problem Let us assume that a particular data...

Record separation problem Let us assume that a particular database program manages a simple mailing list which consists of one record for each person on the list, and a number of f

C program to demonstrate pointer to array, C program to demonstrate Pointer...

C program to demonstrate Pointer to array: void p2a(int *); void main() {                 int x=10, *a,**b;                 int arr[5];                 a=&x;//po

Area of curve, Write a program to find the area under the curve y = f(x) be...

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

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