Variables in C++

Variables are reserved memory locations which are used to store various information which means that some space in memory will be reserved for that variable.

A variable of type char stores a single character, variables of type int store integers (numbers without decimal places), and variables of type float store numbers with decimal places. Each of these variable types – char, int, and float – is each a keyword that you use when you declare a variable.

Using the right variable type can be important for making your code readable and for efficiency–some variables require more memory than others. Moreover, because of the way the numbers are actually stored in memory, a float is “inexact”, and should not be used when you need to store an “exact” integer value.

Declaring Variables in C++

Here are some variable declaration examples:

int x;
char letter;
float the_float;

It is permissible to declare multiple variables of the same type on the same line; each one should be separated by a comma.

int a, b, c, d;

If you were watching closely, you might have seen that declaration of a variable is always followed by a semicolon.

Common Errors when Declaring Variables in C++

If you attempt to use a variable that you have not declared, your program will not be compiled or run, and you will receive an error message informing you that you have made a mistake. Usually, this is called an undeclared variable.

#include <iostream>

using namespace std;

int main()
{

int thisisanumber;

cout << “Please enter a number: “; cin >> thisisanumber;
cin.ignore();
cout << “You entered: ” << thisisanumber << “\n”;
cin.get();

}

C Variables & Constants

C Constants

The alphabets, numbers and special symbols when properly combined form constants, variables and keywords. A constant is an entity that does not change.

C Variables

Variables in c are names which refers to a location in memory which is used to holds some value with which we are working. A variable is an entity that may change its value. In any program we typically do lots of calculations. The results of these calculations are stored in computer memory locations. To make the retrieval and usage of these values we give names to the memory locations. These names are called variables.

C Keywords

A keyword is a word that is part of C Language itself. These words have predefined meanings and these words cannot be used as variable names.

char signed break for
auto const sizeof case
if extern double struct
continue goto register enum
typedef default return static
float union do switch
volatile int unsigned else
while long void short

Types of Variables

There are two main types of variables in C: numeric variables that hold only numbers or values, and string variables that hold text, from one to several characters long.

Basic fundamental data types in C language

Name
Description
Size
Range
char Character or small integer. 1byte signed: -128 to 127
unsigned: 0 to 255
short int Short Integer. 2bytes signed: -32768 to 32767
unsigned: 0 to 65535
long int (long) Long integer. 4bytes signed: -2147483648 to 2147483647
unsigned: 0 to 4294967295
bool Boolean value. It can take one of two values: true or false. 1byte true or false
float Floating point number. 4bytes +/- 3.4e +/- 38 (~7 digits)
double Double precision floating point number. 8bytes +/- 1.7e +/- 308 (~15 digits)
long double Long double precision floating point number. 8bytes +/- 1.7e +/- 308 (~15 digits)

Java Objects & Classes

Java is an object-oriented programming language and classes are fundamental part of any java program. Individual objects are created from these classes. Below is a java class example.

public class Employee{

int empAge;
String empName;

public void setEmpAge(int age){

}

public int getEmpAge(){

}

}

Java Objects

The java objects are similar to real world objects like human, cat, car etc. Each object has their own states and behaviors. The state of an object in java is stored in fields and behaviors are implemented through methods. The communication between objects are done through methods.

Local Variables

Variables which are defined inside method or constructor body are called local variables. The variable can be accessed inside that method and are destroyed when method completed.

Instance variables

Variables which are defined in class body but outside any method or constructor are instance variables. They are initialized when class is instantiated and can be accessed in method or constructor of that class.

Class Variables

Variables which are defined inside class body but outside any method or constructor with static keyword are called class variables.

Java Constructors

In java each class has a constructor method. If you do not write a constructor method in your class java compiler will automatically bind the default constructor with your class. The constructor method should have same name as of class name. When object of a class is created constructor will be invoked. There can be multiple constructors in same class.

Constructor Example

public class Employee{

public Employee(String name){

// constructor code goes here.

}

}

Creating Objects

Objects are created from classes. ‘new’ keyword is used to create object of a class. See the example below.

public class Employee{

public Employee(String empName){

System.out.println(“Employee Name is :” + empName );

}

public static void main(String []args){

// Creating an object newEmployee
Employee newEmployee = new Employee(“Jhon”);

}

}