C++ Programming

C++ Programming

Type Conversions


Introduction

Earlier we have learnt about data types and it’s functions. When constants and variables of different types are mixed in an expression, they are converted to the same type. Therefore, the process of converting one pre-defined data type into another is known as Type Conversion. C++ facilitates type conversions in two ways:

Implicit Type Conversion

The conversion done by the compiler on its own without any external trigger from the user is known as Implicit type conversion. This type of conversion is also known as Automatic type conversion. It is generally applied whenever different data types are intermixed in an expression, so as to not lose any information. Let’s consider the following code fragment:

int X=65;
char C=X;            //Implicit type conversion
cout<<C;

The output of the following code is ‘A’ since the ASCII value ‘A’ is 65. Here, an integer is converted into   a character. This conversion happens when the program is compiled.

Explicit Type Conversion

The conversion done by the user manually from a given type into another data type is referred to as Explicit type conversion. This type of conversion is also known as Type Casting. Let’s consider the following code fragment:

int A=1, B=2;
float C= (float) A/B;         //Type Casting
cout<<C;

The output of the following code is ‘0.5’ cause of the float in the brackets. If the float was not there, then the output would be ‘0’ since “A/B” will return the integer value (  zero ). Here, an integer is converted into a decimal. This conversion   is done manually by the user. 

Now let’s briefly discuss about derived data types and it’s features. These topics will be covered thoroughly in the subsequent chapters. 





Arrays

An array is a collection of similar items stored in contiguous memory locations. In programming, sometimes a simple variable is not enough to hold all the data. This means that when we use character data type, it enables us to store only one character. So if we want to store our name in a variable, we have to use that character data types as an array. Since we are storing characters ( String literal ) in an array, we use double quotes to do so. Array can also be used for other data types. Syntax to declare an array:

<data type> <variable>[size of array];

Ex: int A[4];

This means that the variable “A” can hold upto four integer values. Alright, here’s is a contrast between  a character data type and a character data type as an array:

char A='L';

char A[5]="Learn";

Functions

A function is block of code which is used to perform a particular task, for example let’s say you are writing a large C++ program and in that program you want to do a particular task several number of times, like displaying value from 1 to 10, in order to do that you have to create function ( to display values from one to ten ) and call that function every time you want to display values. This would make you code simple, readable and reusable. Syntax:

<return type> <function name>( )
{
      //Body of the function
}

Pointers

Pointer is a variable in that holds the memory address of another variable. They have data type just like variables, for example an integer type pointer can hold the address of an integer variable and an character type pointer can hold the address of char variable.





References

A reference is an alternative name for an object. It provides alias for a previously defined variable. So this reference can access the value of the variable from memory location.

Constants

In C++, constants are created using const keyword. It is an access in C++ that assigns a constant value to a variable. Any attempt in modifying the value assigned to such variable is reported as an error by the compiler.

const float PI=3.14;                                // PI is declared as a constant





The Weekly Program

Now let’s create a program to print employee details.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr( );
char ename[20],epos[20],ecom[20];
int eno;
float esal;
cout<<"Enter Employee Name: ";
cin>>ename;
cout<<"Enter Company's Name: ";
cin>>ecom;
cout<<"Enter Employee Position: ";
cin>>epos;
cout<<"Enter Enumber: ";
cin>>eno;
cout<<"Enter Salary: ";
cin>>esal;
cout<<"\n\n\n";
cout<<"Employee Details\n";
cout<<"Company: "<<ecom<<"\n";
cout<<"Position: "<<epos<<"\n";
cout<<"Enumber: "<<eno<<"\n";
cout<<"Salary: "<<esal<<"\n";
getch();
}

Execution of the Program

The program is quite self-explanatory. Now Compile the program. If the compiler shows errors then check my program and resolve it. Then Run the program. Here’s the output:

Now it’s time to do it yourself and feel free to mess around with the program to understand in-depth what each statement does. Don’t forget to save your program.

That’s it for today, see you next week!!!