C++M-I(4,5,6)
C++M-I(4,5,6)
IIIT, Bhubaneswar
Dept. Comp. Sc. & Engg.
C++ and Object Oriented Programming
Lecture: 4, 5, 6
Lecture: 5
VII. C++ Tokens:
• The smallest individual units in a program or the smallest meaningful symbols in the
language are known as tokens.
• Each statement in a program consists of different components which independently
identified as tokens by the compiler.
• Tokens can be classified into the following types:
Words withspecial
Keywords int, double, for, auto
meaningto the compiler
Mathematical or logical
Operators +, -, &&, %, <<
operations
Example:
Int x = 10 ;
Keywords:
• Keywords are reserved set of words with fixed meanings.
• These keywords cannot be used as variables names or other user defined program
elements.
• There are 48 keywords in C++ (including the 32 keyword of C).
• In addition the existing 32 keywords of C, there are keywords specific to C++ and 15 other
keywords added by ANSI Committee.
o C & C++ common keywords
Identifiers:
• Identifiers are name given to various program elements, such as variables, function,
arrays, structure, pointer, etc.
• They are user defined names, consisting of a sequence of alphabetic character, digits and
underscore( _ ).
• The first letter must be start with a character or underscore.
• Uppercase and lowercase letters are distinct.
• ANSI C++ has no bound on the length of identifier.
• Keywords can’t be used as identifier name.
CSE/C++/Module-I/Trilochan Rout/Lecture: 4, 5, 6 6
Example:
int a, b;
- The above example represent that int is the keyword and a, b are two integer
identifiers.
Variable Declaration and Initialization:
• A variable is a name given to memory location.
• It can also be stated as an identifiers that holds varying values.
• The syntax for variable declaration remains same as that of C programming:
1. <data type><variable name>;
• The major difference in C++ is that, we can declare the variables anywhere in the program
unlike C where declaration is done at the beginning.
• C++ syntax for initialization is:
1. <data type><variable name> = value;
• Initialization can done by using “cin” statement
1. cin>> x;
• Again initialization can be done by dynamic initialization (that is declaration &
initialization in one statement)
1. int num = 20;
2. float avg = num / 4 ;
CSE/C++/Module-I/Trilochan Rout/Lecture: 4, 5, 6 7
Lecture: 6
IX. Data Types in C++:
• Data are the raw documents or files which are used in computers.
• C++ supports all the C datatypes.
o Generally data can be four types:
1. Integer
2. Character
3. String
4. Floating point types
• Data type represents the characteristics of data which type the data is?
• C++ data types can be classified into the following types
o Derived data type
o Basic or Built-in or Fundamental data type
o User-defined data type
char *ch ;
float *f ;
- In the first statement ‘x’ is an integer pointer and which holds the address of any
integer variable only. Similarly in the second statement ‘ch’ is a character pointer
that can stores the address of any character variable and ‘f’ is float pointer that can
store the address of any float variable only.
Example:
#include<iostream.h>
#include<conio.h>
void main( )
{
int x = 2;
int *p;
cout<<”\n Address of X = “<<&x;
p = &x; //p contains the address of integer variable of x
cout<<”\n Value of X = “<< *p;
getch( );
}
Output:
Explanation:
- In this program x is an integer variable and *p is an integer pointer.
- The first coutstatement displays the address of variable.
CSE/C++/Module-I/Trilochan Rout/Lecture: 4, 5, 6 9
P=&x
Pointer variable p
➢ Function:
- A function is a self contained block or a sub-program of one or more statements that
perform a special task when called.
- We can use a single function for multiple times or with multiple definitions called as
function overloading.
- It means reusability of program code.
Example:
main ( ) fun A ( )
{
..... {
.....
fun A( ); .....
.....
return;
..... funB ( )
fun B( ); }
..... {
..... .....
}
return;
➢ Arrays:
}
- It is a collection of similar data types which are stored in separate contiguous
memory locations.
Example:
intarr[5]; →the statement declares an array arr[ ] which can hold 5 integer values,
which are stored in contiguous memory location.
- The array numbering starts from 0 to n-1. Where ‘n’ is the size of array.
CSE/C++/Module-I/Trilochan Rout/Lecture: 4, 5, 6 10
➢ References:
- Reference data types are declared with the help of ‘&’ operator.
- It is also called as address of operator.
- C++ reference type declares alias(alternative name) for object variables and allow
the programmers to use the variable by reference.
- It represents that both the variables points to the same memory space.
- So any operation performed on one, will reflect on the other.
- By declaring “&variable-name” we are assigning the address of variable which was
already allocated for the variable.
Example:
#include <iostream.h>
#include <conio.h>
int main()
{
int x=7;
clrscr();
int&y=x; /*y is the reference variable */
cout<<"\n Address of X: "<<&x<<endl<<" Address of Y: "<<&y;
cout<<"\n X = "<<x<<"\tY = "<<y;
x=111;
cout<<"\n New value of X: "<<x <<"\tand"<<"\t Y: "<<y;
getch();
return 0;
}
Output:
Explanation: Here‘y’ is the reference variable of ‘x’. Hence address of both x and
y is same. So any operation performed on x and y will reflect on the other.
- In C++, we do not need the keywords structwhile defining the variables b1, b2, b3.
The main aim behind this is to make the syntax and the operation of the user-
defined datatypes similar to that of built-in datatypes. We can access the variables
of the structure using the dot(.) operator.
Note: By default the structure member are public.
2. Unions:
union data
{
charch[2];
int i;
};
data d;
- Structures and Unions both are used to group a number of different variables
together. But while a structure enables us to treat number of different variables at
different places in memory, a union enables us to treat the same space in memory
as a no. of different variables.
Anonymous unions:
- Unions can also be declared with no type name or object name. In this case, they
become anonymous unions, and its members are directly accessible by their
member names. For example, look at the differences between these two structure
declarations:
structure with regular union structure with anonymous union
struct { struct {
char title[50]; char title[50];
char author[50]; char author[50];
union { union {
float dollars; float dollars;
int yen; int yen;
} price; };
} book; } book;
- The only difference between the two pieces of code is that in the first one, the
union has a name (price), while in the second it has not. This affects the way to
access membersdollars and yen of an object of this type. For an object of the first
type (a regular union), it would be:
▪ book.price.dollars
▪ book.price.yen
whereas for an object of the second type (an anonymous union), it would be
• book.dollars
• book.yen
- Again, remember that because it is a union (not a structure), the membersdollars and yen
actually share the same memory location, so they cannot be used to store two different
values simultaneously. The price can be set in dollars or in yens, but not in both
simultaneously.
-
CSE/C++/Module-I/Trilochan Rout/Lecture: 4, 5, 6 12
3. Class:
- It works same as the structure.
- A class is an abstract data type formed by grouping of object having identical
properties, common behavior and shared relationship.
Syntax:
class<class_name>
{
<member variable list>;
<member function list>;
};
Example:
class student
{
introllno; //data member
intregd_no; //data member
charsname[10];
int input(); //member function
void output(); //member function
};
Note: By default, the class data members and member function are private.
Class structure
Classmembers are by Structure members are by
defaultprivate. default public.
It is a logical concept. It is a physical concept.
4. Enumerated:
- In enumerated data-types, the values for the variables are already declared and so
there is a choice for selecting the value of the variable only from the declaration.
- It provides a way to attach names to numbers.
- It is useful for creating symbolic constants.
- The programmer can declare new datatype and define variables of these data
types using the keyword enum.
Syntax:
enum<datatype / tag name> { variable lists };
Example-1:
enum logical {false, true};
Explanation: Here the two variables false and true are automatically assigned
constant unsigned integer values starting from 0.
That is here false = 0, true = 1.
Example-2:
enum day{Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday};
day d;
CSE/C++/Module-I/Trilochan Rout/Lecture: 4, 5, 6 13
5. TypedefDatatype:
- Typedef allow the programmer to create an alias for a data type, and use the
aliased name instead of the actual type name. To declare a typedef, simply use the
typedef keyword, followed by the type to alias, followed by the alias name:
- A typedef does not define new type, but is just another name for an existing type.
- A typedef can be used anywhere a regular type can be used.
X. TYPECASTING:
- Converting a variable from one data type to another is called type casting.
- It is basically two types:
▪ Implicit typecasting
▪ Explicit typecasting
➢ Implicit typecasting:
➢ When the compilers carries out the conversion itself by using inbuilt
datatypes in routines then it is called implicit type conversion.
➢ The variable of lower datatypes when converted to higher datatype is
known as demotion.
➢ Explicit typecasting:
➢ When the datatype of a variable is converted forcefully by writing codes, it
is called explicit typecasting.
Syntax:
datatype(expression);
Or
(datatype) variable ;
Example: