Blue Theme Orange Theme Green Theme Red Theme
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
6 Months Free & No Setup Fees ASP.NET Hosting!
Search :       Advanced Search »
Home » C++ » Object Oriented Programming: Part 4

Object Oriented Programming: Part 4

In this article series, we will learn lots of basic things in OOP.

Author Rank :
Page Views : 1084
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


Variables in C++


In any programming we typically do lots of calculations. The result of these calculations is stored in computers memory. Like human memory, the computers memory also consists of cells. The calculated values are stored in these memory cells. To make the retrieval and usage of these values easy these memory cells are given names. Since the value stored in each location may change the names given to these locations are called variable names.

 

Data Types


When using variable we store the data in computers memory, we need to define its type that is format that what we want to store in. So, that computer can handle the data actions that we will perform after storing. There are following types of data:

 

(i)     Char (Character)

It is smallest data type in C++. It allocates 1 bytes (8 bits) memory in 32 bits computers to store the char value within the range

(a)  Signed char (-128 to 127)

(b)  Unsigned char (0 to 255)

 

(ii)   Short

It allocates 2 bytes (16 bits) memory in 32 bits computers to store the values within the range

(a)  Signed short (-32768 to 32767)

(b)  Unsigned short (0 to 65535)

 

(iii)  Long

It allocates 4 bytes (32 bits) memory in 32 bits computers to store the values within the range

(a)  Signed long (-2147483648 to 2147483647)

(b)  Unsigned long (0 to 4294967295)

 

(iv) Int (Integer)

It allocates 4 bytes (32 bits) memory in 32 bits computers to store the integer values within the range

(a)  Signed long (-2147483648 to 2147483647)

(b)  Unsigned long (0 to 4294967295)

Note: Only int changes the allocated space as per computer (32 bits or 64 bits computers)

 

(v)  Float (Floating Point)

It allocates 4 bytes (32 bits) of memory in 32 bits computers to store the floating numbers of 7 digits (excluding decimal).

 

(vi) Double

It allocates 8 bytes (64 bits) of memory in 32 bits computers to store the floating numbers of 15 digits (excluding decimal).

 

(vii)     Long Double

It allocates 10 bytes (80 bits) of memory in 32 bits computers to store the floating numbers of 19 digits (excluding decimal).

 

(viii)    Bool

It is short of Boolean. It can take values in true/false. It allocates 1 byte (8 bits) of memory. This data type is only compatible with ANSI C compilers.

 

(ix)  wchar_t

It is short of wide character. This data type is designed as to handle the international marked characters. It allocates 2 bytes (16 bits) of memory. This data type is also only compatible with ANSI C compilers.

 

Declaration of Variables


To use variables in C++ programming language we have to declare it first. To declare any variable first we use its data type name and then write the variable name. Variable name should be communicable and understandable that is variable name can describe its nature and properties. For example, to store student mark we should use std_mark and to store student address use std_add. Variable name cannot consist space and also cannot start with numeric values. Foe example

Right

Wrong

int x;

int mymobnum;

int mark2;

int 1;

int 2mymobnum;

int 2mark;

         

 

 

 

 

 

To declare three variables we can write

          int x, y, z;

instead of writing

          int x;

          int y;

          int z;

 

Initialisation of variables


It is the process to assign the value in the variable, which is declared we can initialise the value at the time of variable declaration also. For example,

          int x=10;

          OR

          int x;

          x=10;

One another way to declare the variable and initialise the value in it is to use the open and close parenthesis. For example,

          int x(10);

 

Scope of variables


To use any variable in C++ programming, variable should be declared first. Without declaration of variable we cannot use in program. There are two types of variable scopes.

 

(i)                 Local Variables

The variable which is declared for local use like in any function, loop or even only in main function is known as local variables.

 

(ii)               Global Variables

The variable which is declared for global use in program is known as global variables. Global variables can be used anywhere in program and no need to declare everywhere.

 

Example on scope of variables:

 

#include<iostream.h>

int sum;                  //global variable

int num1, num2;          //global variable

main()

{

int multi;                 //local variable

multi=num1*num2;

cout<<multi;

}

 

Constants


It is an expression that has fixed value. They are divided into many categories:

 

(i)           Integer Numbers

Numbers within the range -2147483648 to 2147483647 are constant integers. The numbers like octal and hexadecimal are also integer constants.

 

(ii)         Floating Point Numbers

The number which has exponents is known as floating point numbers. For example,

3.14159

6.02e23=6.02*1023

1.6e-19=1.6*10-19

 

(iii)   Character Strings

The numbers which has nun-numerical value is known as string. Strings are always covered with single quote or double quote. For example,

       'z'

       'p'

       "Hellow Guys"

       "Hello, how are you?"

 

(iv)   Escape Sequence

The character which is only used to work with strings are known as escape sequence. Escape sequences are always used with inverted slash (\). For example,

           

Escape

Sequences

Description

\n

New line

\r

Carriage return

\t

Tab

\v

Vertical tabulation

\b

Backspace

\f

Page feed

\a

Alarm

\'

Single quote

\"

Double quote

\?

Question mark

\\

Single slash

      

       Usages of escape sequences:

              printf("Hellow\nhow are you?");

              printf("Are you good\?");

 

(v)   Defined Constants

For to use any variable global we use the defined constants. Even in another linked file usages the defined constants. For example,

       #define PI 3.14

       #define newline '\n'

       #define height 30

 

(vi)   Declared Constants

One another way to create any variable constant permanently is to use const prefix before. For example,

       const int height=100;

       const int salary=12000;

 

 

Note: Continue in next part.

 

HAVE A HAPPY CODING!

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Abhimanyu Kumar Vatsa
http://www.itorian.com/about/
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Nevron Chart
Become a Sponsor
 Comments
6 Months Free & No Setup Fees ASP.NET Hosting!
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.