User defined data types in C Plus Plus

There are some user defined data types
  1. Structure
  2. Union
  3. Class
  4. Enumerated data type
 

Structure

Structure is a collection of values having different data types.
 
struct Person{
float weight;
int age;
}
 
Note:
The size of structure defined by the sum of size of individual elements containing in the structure.
 
for the above example Person, the size of structure will be 6 byte.
 

Union

Union is also collection of values having different types but only different is that elements of union share common memory and the size of union will be size of largest element of union.
 
 
union Person{
float weight;
int age;
}
 
Note:
The size of union defined by the size of largest elements containing in the union.
 
for the above example Person, the size of union will be 4 byte.
 

Class

It is another user defined data type, it is also called collection of object and skelton for an object.
 
class Person{
}
 

Enumerated data types

This user defined data types provide the way to provide name to the numbers.
 
enum colors{
RED, GREEN, BLUE
};
 
Note
By default enum keyword provide 0,1,2 to the list of words.
 
cout<<RED<<endl; // 0
cout<<GREEN<<endl; // 1
cout<<BLUE<<endl; // 2
© 2016 - 2022, All Rights are Reserved.