C++ Type compatibility

C++ Type compatibility

c++ is very strict with the type compatibility as compared to C language. for instance, c++ language defines int, short int, and long int as three different types, although each of these has size of one byte. In C++ language the types of values must be the same for complete compatibility, or a cast must be applied. These restrictions in c++ are necessary in order to support function overloading where two functions with the same name are distinguished using the type of function arguments

another major difference is the way char constants are stored. in C programming, they are stored as ints, and therefore

is equivalent to sizeof(int) in c language, in c++ language, however, char is not promoted to the size of int and therefore sizeof(‘y’) equals sizeof(char)

Type Compatibility

C++ is very strict with regard to type compatibility as compared to C programming. Type compatibility is very close to implicit or automatic type conversion. The type compatibility is being able to use two types being able to substitute one for the other without modification and together without modification.

The type compatibility is categorized into following three types by the compiler:
1. Assignment compatibility

In assignment compatibility, if the one type of variable assigned to another type variable is different It will results into loss of value of assigned variable if the size of the assigned variable is large than the size of variable to which it is assigned.

For example

float f1=12.5;
int i1=f1;
This assigning of variable f1 float value to i1 int type will result in loss of decimal value of f1. However, this type type compatibility will not show any type error but it might give a warning “possible loss of data”.

2. Expression compatibility

Consider following example
int num=5/2;
cout<< num;
Here in the above example the result will be 2 because The actual result of 5/2 is 2.5 but because of incompatibility there will be loss of decimal value

3. Parameter compatibility

Due to incompatibility when we pass the values from calling to called function in type of actual parameter and formal parameters loss of data occurs.

For example

void show(int num)
{
cout << ”num=” << num;
}
void main()
{
show(5.2);
}

Here output will be num=5 due to incompatibility in actual and formal parameter type.

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise