LP1 - Unit 2 - Learning C++ Programming Language
LP1 - Unit 2 - Learning C++ Programming Language
UNIT 2
2.1 Introduction
The first thing that people notice about the C++ language is its unusual name. Is there
a C programming language, you might ask? Is there a C− or a C−− language? Are
there programming languages named A and B? The answer to most of these questions
is no. But the general thrust of the questions is on the mark. There is a B programming
language; it was not derived from a language called A, but from a language called
BCPL. The C language was derived from the B language, and C++ was derived from
the C language. Why are there two pluses in the name C++? As you will see in the
next chapter, ++ is an operation in the C and C++ languages, so using ++ produces a
nice pun. The languages BCPL and B will not concern us. They are earlier versions of
the C programming language. We will start our description of the C++ programming
language with a description of the C language.
UNIX fit together so well that soon not just systems programs, but almost all
commercial programs that ran under UNIX were written in the C language. C became
so popular that versions of the language were written for other popular operating
systems; its use is not limited to computers that use UNIX. However, despite its
popularity, C is not without its shortcomings.
On line 1, the file iostream.h is included in the file. The first character is the #
symbol, which is a signal to the preprocessor. Each time you start your
compiler, the preprocessor is run. The preprocessor reads through your source
code, looking for lines that begin with the pound symbol (#), and acts on those
lines before the compiler runs.
include is a preprocessor instruction that says, "What follows is a filename. Find
that file and read it in right here." The angle brackets around the filename tell
the preprocessor to look in all the usual places for this file. If your compiler is
1 | Computer Programming 1 17
set up correctly, the angle brackets will cause the preprocessor to look for the
file iostream.h in the directory that holds all the H files for your compiler. The
file iostream.h (Input-Output-Stream) is used by cout, which assists with
writing to the screen. The effect of line 1 is to include the file iostream.h into
this program as if you had typed it in yourself.
The preprocessor runs before your compiler each time the compiler is
invoked. The preprocessor translates any line that begins with a
pound symbol (#) into a special command, getting your code file ready
for the compiler.
Line 3 begins the actual program with a function named main(). Every C++
program has a main() function. In general, a function is a block of code that
performs one or more actions. Usually functions are invoked or called by
other functions, but main() is special. When your program starts, main() is
called automatically .main(), like all functions, must state what kind of value
it will return. The return value type for main() in HELLO.CPP is int, which
means that this function will return an integer value.
All functions begin with an opening brace ({) and end with a closing brace (}).
The braces for the main() function are on lines 4 and 7. Everything between
the opening and closing braces is considered a part of the function. The meat
and potatoes of this program is on line 5. The object cout is used to print a
message to the screen. cout is used in C++ to print strings and values to the
screen. A string is just a set of characters.
A variable name can consist of alphabets (both upper and lower case), numbers and
the underscore ‘_’ character. However, the name must not start with a number.
Example:
• The variable declaration refers to the part where a variable is first declared or
introduced before its first use.
• A variable definition is a part where the variable is assigned a memory
location and a value. Most of the time, variable declaration and definition are
done together.
Output:
Types of variables
• Local Variables
• Instance Variables
• Static Variables
• These variables are created when entered into the block or the function is
called and destroyed after exiting from the block or when the call returns from
the function.
• The scope of these variables exists only within the block in which the variable
is declared. i.e. we can access this variable only within that block.
• Initialization of Local Variable is Mandatory.
2. Instance Variables: Instance variables are non-static variables and are declared in a
class outside any method, constructor or block.
• As instance variables are declared in a class, these variables are created when
an object of the class is created and destroyed when the object is destroyed.
• Unlike local variables, we may use access specifiers for instance variables. If
we do not specify any access specifier then the default access specifier will be
used.
• Initialization of Instance Variable is not Mandatory.
• Instance Variable can be accessed only by creating objects.
• Static variables are created at the start of program execution and destroyed
automatically when execution ends.
• Initialization of Static Variable is not Mandatory. Its default value is 0
• If we access the static variable like the Instance variable (through an object),
the compiler will show the warning message and it won’t halt the program.
The compiler will replace the object name with the class name automatically.
• If we access the static variable without the class name, the Compiler will
automatically append the class name.
All variables use data-type during declaration to restrict the type of data to be
stored. Therefore, we can say that data types are used to tell the variables the type of
data it can store. Whenever a variable is defined in C++, the compiler allocates some
memory for that variable based on the data type with which it is declared. Every
data type requires a different amount of memory.
C++ supports a wide variety of data types and the programmer can select the data
type appropriate to the needs of the application. Data types specify the size and
types of value to be stored. However, storage representation and machine
instructions to manipulate each data type differ from machine to machine, although
C++ instructions are identical on all machines.
1. Primitive Data Types: These data types are built-in or predefined data types
and can be used directly by the user to declare variables. example: int, char,
float, bool, etc. Primitive data types available in C++ are:
Integer: The keyword used for integer data types is int. Integers typically require 4
bytes of memory space and range from -2147483648 to 2147483647.
Character: Character data type is used for storing characters. The keyword used for
the character data type is char. Characters typically require 1 byte of memory space
and range from -128 to 127 or 0 to 255.
Boolean: Boolean data type is used for storing Boolean or logical values. A Boolean
variable can store either true or false. The keyword used for the Boolean data type is
bool.
Floating Point: Floating Point data type is used for storing single-precision floating-
point values or decimal values. The keyword used for the floating-point data type is
float. Float variables typically require 4 bytes of memory space.
Double Floating Point: Double Floating-Point data type is used for storing double-
precision floating-point values or decimal values. The keyword used for the double
floating-point data type is double. Double variables typically require 8 bytes of
memory space.
void: Void means without any value. void data type represents a valueless entity. A
void data type is used for those function which does not return a value.
Wide Character: Wide character data type is also a character data type but this data
type has a size greater than the normal 8-bit datatype. Represented by wchar_t. It is
generally 2 or 4 bytes long.
2. Derived Data Types: The data types that are derived from the primitive or built-in
datatypes are referred to as Derived Data Types. These can be of four types namely:
• Function
• Array
• Pointer
• Reference
1 | Computer Programming 1 23
3. Abstract or User-Defined Data Types: These data types are defined by the user
itself. Like, as defining a class in C++ or a structure. C++ provides the following user-
defined datatypes:
• Class
• Structure
• Union
• Enumeration
• Typedef defined Datatype
1. Arithmetic Operators
2. Assignment Operators
3. Relational Operators
4. Logical Operators
5. Bitwise Operators
6. Other Operators
For example,
Here, the + operator is used to add two variables a and b. Similarly, there are
various other arithmetic operators in C++.
1 | Computer Programming 1 24
Output:
a + b = 9
a - b = 5
a * b = 14
a / b = 3
a % b = 1
For Example,
Output:
result_a = 11
result_b = 99
For example,
Output:
a = 2
b = 7
After a += b;
a = 9
For example,
Example:
Output:
3 == 5 is 0
3 != 5 is 1
3 > 5 is 0
3 < 5 is 1
3 >= 5 is 0
3 <= 5 is 1
Example:
Output:
(3 != 5) && (3 < 5) is 1
(3 == 5) && (3 < 5) is 0
(3 == 5) && (3 > 5) is 0
(3 != 5) || (3 < 5) is 1
(3 != 5) || (3 > 5) is 1
(3 == 5) || (3 > 5) is 0
!(5 == 2) is 1
!(5 == 5) is 0
2.3 References
Malini Devi J. (2014). C++ Programming Language for Beginner.
Chapter 1 pp. 1-4
2.4 Acknowledgment
The images, tables, figures, and information contained in this module were
taken from the references cited above.