Object
is an identifiable entity with some characteristics and behavior.
Declaration
To create an object you must first declare it, specifying its type and its name, this is
known as declaration .it is the act of telling the compiler about the type and
the name for the memory address allocated for the variable.
Syntax:
Datatype
object name;
For
example: int sam;
Where
datatype can be any fundamental datatype and object
name as any valid name in C++.
A valid name in C++
must start contain only alphabets i.e. a-z and A-Z and numbers i.e. 0-9.the
name must strat from an alphabet and not any number.
It is to be noted that we can use the underline character also for writing a
valid name in C++.
Following names are
valid in C++
Sameer
Sam
Sam420
S1a2m3
Sam_420s
A
Following names are invalid in C++
4sam
Sam eer
222
4-a
Sa,sa
There are some names which
are allowed but which you shouldn't use either. All names containing a sequence
of two underline characters at any position (__) are reserved for the implementation, and so are any names
starting by an underline character followed by an upper-case letter. You can use them, but you should not.
Fundamental
datatypes in C++
The fundamental datatypes
in C++ are int , double , character and float.
Well there is one more which is the void datatype.
int
datatype is used for those variable which can hold integer
values and not any other value apart from integer. It can hold both positive
and negative integers.
For example:1,234,5,56
etc.
double datatype
is used for those variable which can hold floating-point or decimal numbers
For example:1.22,.34-43.2
etc
Double datatype is
used for those variable which can hold large decimal numbers.
It can also store large integer values.
Fr example:1.22222222222222222222222222222222222222222,93849234798374.2131233
etc.
Char datatype is
used for variable holding character values
For example : a,b,sam etc.
Initialization
Assigning a value to a declared variable the first
time is known as initialization of the variable
Syntax:
datatype variable name = value;
For example : int a=4;
Assigning and initialization are not to be mixed
together initialization is the act of giving the first value to a variable
whereas assigning a value to a variable thereafter is known as assignment.
Following statement will make the concept clear
int sam=420;
This is initialization
sam=2;
sam=2+2+2+2+2+2+2;
These statements are examples of assignment
Using input
and output functions
Using cout function:
First try to
understand the following program;
#include <iostream.h>
void main()
{
int a=4;
cout<<”can print
this”;
cout<<”a=”<<a;
char b=’a’;
cout<<b;
}
So using cout we can print integer numbers, sentences or string
(group of characters),
We can add multiple cout statements into one by simply adding the << make
at the end of the first cout statements like in the
third line. Here you need to know that to print the text in the next line we
use the command \n which tells the compiler to print the text coming after it
in the next line;
Using cin function
#include <iostream.h>
void main()
{
int age;
cout<<”enter the age:”;
cin>>age;
}
This program reads the age of a person using
the cin function .this function is defined in the iostream.h header file so therefore it is required to
include this header file in all program using cout
and cin.It is to be noted that while dealing with
string and reading using cin it reads only till the first blankspace
is encountered but mostly we use gets() for imputing string as it has added
advantage of reading blankspaces.