Data types:
- Data types are just like initials.
- Data types are the kind of data that the variable use in programming languages.
- Generally the data types are classified as
ii)float
iii)char
iv)double.
- The data type "int" holds 2 byte values, whereas "float" holds 4 bytes, and "char" data type holds 1 byte and "double" data type holds 8 bytes.
- A particular variable can point to a single data type throughout the program.
- The concept of data type differs when we use a templates
- When we use templates, a variable may point to a unknown data type. (i.e) a generic pointer.
- Variables are the memory location that the variable use in programming languages.
- A variable can be name of a person, or anything else.A user defined name can be defined to a variable.
- Rules for declaring a variables:
- The variable must starts with an alphabet or an underscore i.e( _ ) followed by alphabets or numbers.
- No special characters except underscore should be used.
- The special words or reserved words i.e the keywords should not be used.
data_type variable_name;
Example:
int num;
float avg;
double area;
char name;
Declaring variable while using templates
template <class a>
class sample
{
private:
a ex;
public:
void fun()
{
cout<<"using templates";
}
void getdata()
{
cout<<"enter a integer value";
cin>>ex;
cout<<"enter a float value";
cin>>ex;
cout<<"enter a char value";
cin>>ex;
}
The above program is implemented using templates.Here the variable "ex" is of generic type.(i.e) unknown data type.
Hence it can take values for integer, or float, or character.
Assigning values:
Values to a variable can be assigned in two ways:
i) Direct assignment
ii) Run time assignment
Direct assignment:
Direct assignment to a variable is nothing but initialization of variables.
eg: int num=10;
Here the variable num takes the value 10 initially.Since it is a variable the value may be changed.
Run time assignment:
The values for the variables can be read from the user during the runtime.
(i.e) char name;
cout<<"enter your name";
cin>>name;