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 program to demonstrate call by reference, C program to demonstrate call b...

C program to demonstrate call by reference: void disp(int *,int*,int*,int*); void main() {                 int a=2,b=3, sum,mul;                 printf("Enter 1st n

Calculation of mortgage interest rates, 1. When developing this project in ...

1. When developing this project in a Win32 Console Applications that includes the precompiled headers, enter in the Name: box, PRJ2[Your Full Last Name][Your First Initial] with no

Program for read a formal expression from standard input, Your program will...

Your program will read two kinds of data from two files: names and predicates. It will read a formal expression from standard input and check whether that expression is syntactical

Area under curve, progarm in c for area under curve   #include ...

progarm in c for area under curve   #include float start_point, /* GLOBAL VARIABLES */ end_point, total_area; int numtraps;

Queue, program to enter 5 values in queue and remove value one by one from ...

program to enter 5 values in queue and remove value one by one from queue.

String, palindrome is a string that reads the same from both the ends. Give...

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 palindromes

Luminous jewels polishing game, Luminous Jewels - The Polishing Game Bytel...

Luminous Jewels - The Polishing Game Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various lum

Text Editor, Add a function for saving the text stored in an array to a fil...

Add a function for saving the text stored in an array to a file. Your program must check whether or not the output file already exists, and if it does, your program must ask the us

Stone game, A game is played with three piles of stones and two players. At...

A game is played with three piles of stones and two players. At her turn, a player removes one or more stones from the piles. However, if she takes stones from more than one pile,

What is a union how does it differ from a structure, Question : (a) (i)...

Question : (a) (i) What is a structure member in C? What is the relationship between a structure member and the structure? (ii) How can structure variables be declared in C?

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