Chapter 1
Chapter 1
Basics of Programming:
History of C++:
The C++ is an object-oriented programming language design and developed by – Bjarne
Stroustrup at AT & T Bell Laboratories in Murray Hill, New Jersey located in USA in 1979.
As he wanted a flexible & a dynamic language which was similar to C with all its features,
It was developed for adding a feature of OOP (Object Oriented Programming) in C without
significantly changing the C component.
Since the class was major addition to the original language, Stroustrup called the new
language “C with Classes”, however later in 1983, the name was changed to C++. The idea
of C++ comes from the increment operator ++, thereby suggesting that C++ is an
incremented version of C.
Let's see the programming languages that were developed before C++ language.
Language Year Developed By
Algol 1960 International Group
BCPL 1967 Martin Richard
B 1970 Ken Thompson
Traditional C 1972 Dennis Ritchie
C++ 1979 Bjarne Stroustrup
Advantages of C++:
1. OOPS (Object Oriented Programming System)
One of the main advantages of the programming language is the Concept of OOPS, as many
times the developer have to solve real-world interfacing problem. Hence, they uses
programming languages like C wastes a lot of time doing so, but, when the developer
shifted to the CPP language, he solved the problem easily because of the Object-oriented
programming system. That helps programmers to enhance and improve their code. The
OOPS includes many the features like classes, inheritance, polymorphism, data,
abstraction, and encapsulation. In the OOPS standard, the programmer treats data as an
object and easily solves a real-world problem. This feature in the C++ language helps
many programmers to get numerous jobs.
2. Portability
The CPP language is a language that is highly coded portable; CPP language is highly
portable as you can write code in one system and use the code in another system. The
portability of the language allows programmers to run the same program on different
operating systems. For example, the programmer writes code in the windows operating
system on his laptop, and for some bad reason, he has to shift to his friend's laptop for the
code in the LINUX Operating system. He can easily display and modify that file.
3. Low level manipulation Language
Letters: A to Z, a to z.
Uppercase Characters:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
LowerCase Characters:
abcdefghijklmnopqrstuvwxyz
Digits: 0 to 9
0123456789
Special Symbols:
Space + - ∗ ⁄ ^ \ ( ) [ ] { } = != < > . ′ ″ $ , ; : % ! & _ # <= >= @
White Spaces:
Blank space, Horizontal tab (→), Carriage return (↵), Newline, Form feed
Other Characters:
C++ can process any of the 256 ASCII characters as data or as literals.
Documentation Section
Header Files / Pre-processors Statements
Global Declaration
Class Declaration
main() function
{
Documentation Section:
Documentation section is generally meant include set of comments, that is used to
provide the information about the program written like name of program, utility of
program, date of creation, date of last modification, author name, licensing or copyrights
information and any other information that programmer with to put for the references.
Pre-processor Statements:
This is the section where we write all of the pre-processor directive statements, pre-
processor statements usually with begins with a (#) symbol. Pre-processor statements
such #include tells the compiler to include header files and #define directive is used to
define constants prior to compilation of the program.
Global Declarations:
This is the section where all the global declaration comes. All of the variables, structures,
classes and function defined or declared outside the main function are treated as global.
Class definition:
The classes are used to map real world entities into programming. The classes are the key
building block of any C++ program. A C++ program may include several class definitions.
This is the section where we define all of our classes.
Main function definition:
This is the most vital part of each and every C++ program, it is mandatory for C++
program to have a main() function definition. There can be only one main() function in
C++ program. The execution of a C++ program starts with the main() function. The C++
program can not be executed without the main() function
User defined functions: -
This is the section of where we put all the user defined functions created to
perform a specific task. A user defined function must be defined before use it. User
defined function can written before or immediately after the main ( ) function and
called inside the main ( ) function.
Identifiers:
In C++, an identifier is a name given to program elements such as variables, array, class
and functions etc. An identifier is a sequence of letters, digits, and underscores.
Following are the rules to define identifier.
• They must begin with a letter or underscore(_).
• They must consist of only letters, digits, or underscore. No other special character is
allowed.
• It should not be a keyword.
• It must not contain white space.
Datatypes in C++
Built In:
int
Integers are used to store whole numbers. C++ supports several integer types, varying
internal sizes for storing signed and unsigned integers. Integers can be declared using int
keyword.
Type Storage size Value range
int 2 Bytes -32,768 to 32,767
unsigned int 2 Bytes 0 to 65,535
long 4 bytes -2,147,483,648 to 2,147,483,647
unsigned long 4 bytes 0 to 4,294,967,295
float
Float data types are used to store single-precision data values i.e. decimal values. It
holds 4 bytes of memory space.
double
Double data types are used to store double-precision floating-point data values. It holds 8
bytes of memory space.
Type Storage size Value range
float 4 bytes 3.4e-38 to 3.4e+38
double 8 bytes 1.7e-308 to 1.7e+308
Variables
Variables is name used to refer memory location in computer memory that holds a value
for that variable, this value can be changed during the execution of the program. When
you create a variable in C++, this means you are allocating some space in the memory for
that variable. The size of memory block allocated and type of the value it holds is
completely dependent upon the type of variable.
Rules for naming a variable in C++
• The name of variable always starts with alphabet or underscore
• Blank space not allowed between variable name.
• Special symbols not allowed between variable name except underscore ( _ )
• Kewords / reserve words are not allowed as a variable name.
• Variable names are case sensitive.
Declaration of variables:
Syntax:
<datatypes> <variable name>;
Or <datatypes> <variable name>=<value>;
Constant:
Constant are same like as a variable in c++, but the value of constant cannot be changed
or modified during the execution of program. Constant must be initialized when declared
as value cannot be assigned it to later.
Declaration of constant:
Syntax:
const <datatype> <constantname>=valueofconstant;
Eg. const float PI=3.14;
Types of Constants:
• Integer Constant
• Float constant
• Char constant
• String constant
Integer Constant: An integer constant can only hold integer quantity which is a
sequence of whole numbers or digits.
Float constant: floating point constants contains a decimal point or an exponent.
Character constant: a character constant is a single character, enclosed in single
quotation marks. It can be a single alphabet, a digit or a symbol.
String Constant: A string constant is sequence of characters enclosed in double quotes.
It may contain letters, digits, special characters and blank space.
Operators:
An operator is a special symbol that is used to carry out some Arithmetical or Logical
operation on its operand.
In C++, we have rich set of built in operators to carry out different type of operations.
Type of operators
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Increment and Decrement Operators
• Conditional Operators
• Assignments operators
Relational Operators
Relational Operators are used to evaluate a comparison between two operands. The
result of a comparison operation is a Boolean value that can only be true or false.
Let variable a holds 20 and variable b holds 10, then –
Operator Description Example
< Less than a<b returns FALSE
> Greater than a>b returns TRUE
<= Less than or equal to a<=b returns FALSE
>= Greater than or equal to a>=b returns TRUE
== Equal to a==b returns FALSE
!= Not equal to a!=b returns TRUE
Logical Operators
Logical operators are used to combine expressions with conditional statements using
logical (AND, OR, NOT) operators, which results in true or false.
Let variable a holds true or 1 and variable holds false or 0 then-
Operator Name Description Example
&& Logical AND Return true if all expressions are true (a && b) returns false
Return true if any one expression is
|| Logical OR ( a || b) returns true
true
Assignment Operators:
Assignment operators are used to assign value to a variable, you can assign a variable
value or the result of an arithmetical expression.
Operator Description Example
= Assignment Operator A=b – assign a value of b to a
+= Add and assign A+=b is equivalent to a=a+b;
-= Subtract and assign A-=b is equivalent to a=a-b;
*= Multiply and assign A*=b is equivalent to a=a*b;
/= Divide and assign A/=b is equivalent to a=a/b;
%= Mod and assign A%=b is equivalent to a=a%b;
******
List of Exercise:
• Write a program to print “hello world”.
• Write a program to print your name and address on separate line.
• Write a program to demonstrate different types of variables (int, float, char).
• Write a program to read a two numbers and print addition of those numbers.
• Write a program to read a number and find out square of that number.
• Write a program to read a three numbers and print addition of those numbers.
• Write a program to read a number and find out cube of that number.
• Write a program to read two numbers and interchange (swap) their values.
• Write a program to read two numbers and interchange (swap) their values.
(without using 3rd variable)
• Write a program to find out maximum between two numbers using conditional
operator.