INTRODUCTION TO C++
You may know that C++ is an extension to C Programming language. It was
developed at AT&T Bell Laboratories in the early 1980s by Bjarne Stroustrup. It is a
deviation from traditional procedural languages in the sense that it follows Object Oriented
Programming (OOP) approach which is quite suitable for managing large and complex
programs.
OBJECTIVES
After reading this lesson, you will be able to:
learn about C++ character set, tokens and basic data types;
explain the utility of different types of operators used in C++;
identify the difference between implicit and explicit conversions;
explain about Input/Output streams supported by C++;
Explain the structure of a C++ program;
write a simple program in C++.
C++ CHARACTER SET
Character set is a set of valid characters that a language can recognize. Acharacter
represents any letter, digit or any other special character. The C++programming language also
has some character set. Now let us learn C++language character set.
C++ character set
BASIC DATA TYPES
Now you will learn about basic data types used in C++ language. Every program specifies a
set of operations to be done on some data in a particular sequence. However, the data can be
of many types such as a number, a character, Boolean value etc. C++ supports a large number
,of data types. The built in or basic data types supported by C++ are integer, floating point and
character type. A brief discussion on these types is given below:
Data types with range
Integer type (int)
An integer is an integral whole number without a decimal point. These numbersare used for
counting. For example, 26, 373, –1729 are valid integers.Normally an integer can hold
numbers from –32768 to 32767. However, if theneed be, a long integer (long int) can also be
used to hold integers from–2, 147, 483, 648 to 2, 147, 483, 648.
Floating point type (float)
A floating-point number has a decimal point. Even if it has an integral value,it must include a
decimal point at the end. These numbers are used for measuringquantities.
Examples of valid floating-point numbers are: 27.4, -927., 40.03
A float type data can be used to hold numbers from 3.4*10–38 to 3.4*10 +38 withsix or
seven digits of precision. However, for more precision a double precisiontype (double) can be
used to hold numbers from 1.7*10–308 to 1.7*10+308 withabout 15 digits of precision.
Character Type (Char)
It is a non-numeric data type consisting of single alphanumeric character.
Examples of valid character types are: ‘A’, ‘9’, ‘P’, ‘8’, ‘&’.
It may be noted here that 9 and ‘9’ are of different data types. The former is
of type int and later of type char.
,TOKENS
A token is a group of characters that logically belong together. The programmercan write a
program by using tokens. C++ uses the following types of tokens.
Keywords
Identifiers
Literals
Punctuators
Operators
Keywords
There are some reserved words in C++ which have predefined meaning to
complier called keywords. Some commonly used keywords are given below:
List of Keywords
Identifiers
Symbolic names can be used in C++ for various data items used by a programmerin his/her
program. For example, if you want to store a value 50 in a memorylocation, you can choose
any symbolic name (say MARKS) and use it as givenbelow:
MARKS = 50
The symbol ‘=’ is an assignment operator. The significance of the abovestatement is that
‘MARKS’ is a symbolic name for a memory location wherethe value 50 is being stored.A
, symbolic name is generally known as an identifier. The identifier is a sequenceof characters
taken from C++ character set. The rules for the formation of anidentifier are:
(i) An identifier can consist of alphabets, digits and/or underscores.
(ii) It must not start with a digit.
(iii) C++ is case sensitive, i.e., upper case and lower-case letters are considereddifferent from
each other. It may be noted that TOTAL and total are twodifferent identifier names.
(iv) It should not be a reserved word (keywords).
Literals
Literals (often referred to as constants) are data items that never change theirvalue during the
execution of the program. The following types of literals areavailable in C++.
(i) integer-constants
(ii) character-constants
(iii) floating-constants
(iv) string-literals
Integer constants
Integer constants are whole numbers without any fractional part. It may containeither + or –
sign, but decimal point or commas does not appear in any integerconstant. C++ allows three
types of integer constants.
1. decimal (Base 10)
2. octal (Base 8)
3. hexadecimal (Base 16)
You may know that C++ is an extension to C Programming language. It was
developed at AT&T Bell Laboratories in the early 1980s by Bjarne Stroustrup. It is a
deviation from traditional procedural languages in the sense that it follows Object Oriented
Programming (OOP) approach which is quite suitable for managing large and complex
programs.
OBJECTIVES
After reading this lesson, you will be able to:
learn about C++ character set, tokens and basic data types;
explain the utility of different types of operators used in C++;
identify the difference between implicit and explicit conversions;
explain about Input/Output streams supported by C++;
Explain the structure of a C++ program;
write a simple program in C++.
C++ CHARACTER SET
Character set is a set of valid characters that a language can recognize. Acharacter
represents any letter, digit or any other special character. The C++programming language also
has some character set. Now let us learn C++language character set.
C++ character set
BASIC DATA TYPES
Now you will learn about basic data types used in C++ language. Every program specifies a
set of operations to be done on some data in a particular sequence. However, the data can be
of many types such as a number, a character, Boolean value etc. C++ supports a large number
,of data types. The built in or basic data types supported by C++ are integer, floating point and
character type. A brief discussion on these types is given below:
Data types with range
Integer type (int)
An integer is an integral whole number without a decimal point. These numbersare used for
counting. For example, 26, 373, –1729 are valid integers.Normally an integer can hold
numbers from –32768 to 32767. However, if theneed be, a long integer (long int) can also be
used to hold integers from–2, 147, 483, 648 to 2, 147, 483, 648.
Floating point type (float)
A floating-point number has a decimal point. Even if it has an integral value,it must include a
decimal point at the end. These numbers are used for measuringquantities.
Examples of valid floating-point numbers are: 27.4, -927., 40.03
A float type data can be used to hold numbers from 3.4*10–38 to 3.4*10 +38 withsix or
seven digits of precision. However, for more precision a double precisiontype (double) can be
used to hold numbers from 1.7*10–308 to 1.7*10+308 withabout 15 digits of precision.
Character Type (Char)
It is a non-numeric data type consisting of single alphanumeric character.
Examples of valid character types are: ‘A’, ‘9’, ‘P’, ‘8’, ‘&’.
It may be noted here that 9 and ‘9’ are of different data types. The former is
of type int and later of type char.
,TOKENS
A token is a group of characters that logically belong together. The programmercan write a
program by using tokens. C++ uses the following types of tokens.
Keywords
Identifiers
Literals
Punctuators
Operators
Keywords
There are some reserved words in C++ which have predefined meaning to
complier called keywords. Some commonly used keywords are given below:
List of Keywords
Identifiers
Symbolic names can be used in C++ for various data items used by a programmerin his/her
program. For example, if you want to store a value 50 in a memorylocation, you can choose
any symbolic name (say MARKS) and use it as givenbelow:
MARKS = 50
The symbol ‘=’ is an assignment operator. The significance of the abovestatement is that
‘MARKS’ is a symbolic name for a memory location wherethe value 50 is being stored.A
, symbolic name is generally known as an identifier. The identifier is a sequenceof characters
taken from C++ character set. The rules for the formation of anidentifier are:
(i) An identifier can consist of alphabets, digits and/or underscores.
(ii) It must not start with a digit.
(iii) C++ is case sensitive, i.e., upper case and lower-case letters are considereddifferent from
each other. It may be noted that TOTAL and total are twodifferent identifier names.
(iv) It should not be a reserved word (keywords).
Literals
Literals (often referred to as constants) are data items that never change theirvalue during the
execution of the program. The following types of literals areavailable in C++.
(i) integer-constants
(ii) character-constants
(iii) floating-constants
(iv) string-literals
Integer constants
Integer constants are whole numbers without any fractional part. It may containeither + or –
sign, but decimal point or commas does not appear in any integerconstant. C++ allows three
types of integer constants.
1. decimal (Base 10)
2. octal (Base 8)
3. hexadecimal (Base 16)