0% found this document useful (0 votes)
33 views14 pages

C++ Notes English

C++ is a general-purpose, object-oriented programming language created by Bjarne Stroustrup in 1979, serving as a superset of C with additional features. It includes characteristics such as being statically typed, compiler-based, and case-sensitive, and supports both low-level and high-level programming. The document covers C++ program structure, tokens, data types, control flow statements, and examples of decision-making and looping statements.

Uploaded by

yogijilive01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views14 pages

C++ Notes English

C++ is a general-purpose, object-oriented programming language created by Bjarne Stroustrup in 1979, serving as a superset of C with additional features. It includes characteristics such as being statically typed, compiler-based, and case-sensitive, and supports both low-level and high-level programming. The document covers C++ program structure, tokens, data types, control flow statements, and examples of decision-making and looping statements.

Uploaded by

yogijilive01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Saurabh sir c++ notes english

What is C++
C++ language is a general purpose, object-oriented programming language created by Bjarne
Stroustrup in 1979.

C++ language is similar to C language. We can make all the programs made in C language very
easily in C ++ language without any changes or with minor changes.

We can say that C ++ language is a superset of C language, which has the features of C language
as well as some other additional features as well.

C++ is an efficient and powerful programming language which is mainly used in GUI platforms
and 3D graphics.

C++ language has much bigger function libraries than C language which makes it much more
simple and convenient than C language.

C ++ language is also a middle-level programming language like C language, in which we can


create both low level and high-level programs very easily.

History of C++
Bjarne Stroustrup started working on the C++ language in 1978 and released its first version in
1979. Initially, according to them, this was not a new programming language, they said that it
was an extended version of the C language.

Features of C++ Language


The C++ language has the following characteristics:

 Statically typed
 Compiler Based
 General-Purpose Language
 Case Sensitive
 Object-Oriented Programming

1. Statically typed
C++ language is a statically typed programming language in which the data type of the variable
is checked at compile-time instead of at run time.
Saurabh sir c++ notes english

2. Compiler Based
C ++ language is a compiler-based programming language in which programs cannot be
executed or run without compilation.

The program made in C ++ language is first converted into machine language with the help of
compiler, then we can execute that program.

3. General Purpose Language


C ++ language is a general purpose programming language, in which we can create almost all
types of software from application software to system software.

4. Case Sensitive
C++ is a case sensitive programming language in which words written in lower case letters and
upper case letters have different meanings.

5. Object-oriented Programming
C++ is an object-oriented programming language in which programs are made by treating things
as an object.

C++ Program Structure

Let us look at a simple code that would print the words Hello World.
Live Demo
#include <iostream.h.h>

// main() is where program execution begins.


main() {
cout << "Hello World"; // prints Hello World

}
Let us look at the various parts of the above program −
 The C++ language defines several headers, which contain information that is either
necessary or useful to your program. For this program, the header <iostream.h> is needed.
 The next line '// main() is where program execution begins.' is a single-line comment
available in C++. Single-line comments begin with // and stop at the end of the line.
 The next line cout << "Hello World"; causes the message "Hello World" to be displayed
on the screen.

Compile and Execute C++ Program


Saurabh sir c++ notes english

Let's look at how to save the file, compile and run the program. Please follow the steps given
below −
 Open a text editor and add the code as above.
 Save the file as: hello.cpp
 Open a command prompt and go to the directory where you saved the file.
 Type 'g++ hello.cpp' and press enter to compile your code. If there are no errors in your
code the command prompt will take you to the next line and would generate a.out
executable file.
 Now, type ctrl+f9 to run your program.

C++ Tokens
C++ Tokens are the tiniest individual units of any program. C++ is the superset of „C‟ and so most
constructs of „C‟ are permissible in the „C++‟ with their sense and usage unaffected. So tokens,
expressions, and the data types are equal to that of „C‟.C++ Tokens are the tiniest individual units of
any program. C++ is the superset of „C‟ and so most constructs of „C‟ are permissible in the „C++‟
with their sense and usage unaffected. So tokens, expressions, and the data types are equal to that of
„C‟.

Following, given below are the „C++‟ tokens: (most of the „c++‟ tokens are basically equal to the
„C‟ tokens, respectively)

 Keywords
 Identifiers
Saurabh sir c++ notes english

 Constants
 Variables
 Operators

Keywords

These are the reserved words that have a fixed and definite meaning, and their sense cannot be
changed or modified. Moreover, the meaning and the working process of these keywords are
already known to the compiler. „C++‟ has more numbers of keywords as compared to „C‟, and those
extra ones are having special working abilities.

There are a total of 32 of these, and these are as follows:

There are additional 30 reserved words that were not in ‘C’, these are therefore new to the
‘C++’, and these are as follows:

Identifiers
Identifiers are the names that are given to the different entries just like variables, structures, as well
as functions. Also, the names of the identifiers should be unique because these entities are
applicable to the implementation of the program.

Identifier naming conventions


Only the alphabetic characters, digits, and underscores are allowed.

The first letter should be an alphabet or an underscore (_).

The identifiers are case-sensitive.

Keywords that are reserved can‟t be used as the name of the identifier.

Constants
Constants are similar to a variable, except for one thing, that is their value which never changes
during the execution process once defined.

There are two more different ways of defining the constants in ‘C++’. These are as follows:

By the use of the „const‟ keyword


Saurabh sir c++ notes english

By the use of the „#define‟ pre-processor

Declaration of a constant:
const [data_type] [constant_name]=[value];

Operator
„C++‟ operator is a symbol that we use for performing mathematical or logical manipulations.

Arithmetic Operators

Increment and Decrement Operators

Relational Operators

Logical Operators

Bitwise Operators

Assignment Operators

Variable
A variable is a name that is meaningful. In addition, this name is basically of the data storage
location in a computer system‟s memory. Similarly, when we use a variable we refer to
the memory address of the computer system

.
Saurabh sir c++ notes english

Syntax to declare a variable


[data_type] [variable_name];

Example

COPY CODE

#include

int main() {

int a,b;// a and b are integer variable

cout<<” Enter first number :”;

cin>>a;

cout<<” Enter the second number:”;

cin>>b;

int sum;

sum=a+b;

cout<<” Sum is : “<

C++ Data Types


Data types in C++ are mainly divided into three types:
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
 Character
 Boolean
 Floating Point
 Double Floating Point
 Valueless or Void
 Wide Character
Saurabh sir c++ notes english


 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
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
Saurabh sir c++ notes english

 Enumeration
 Typedef defined Datatype

Control Flow Statement


The control statements or flow of control statements are used to control the flow of program.
These statements are very important in any programming languages to decide whether other
statement will be executed or not. The code statement generally runs in the sequential manner.
We may require executing or skipping some group of statements based on the given condition,
jumps to another statement, or repeat the execution of the statements.

In , control statement allows to smooth flow of the program. By using the control flow
statements, a program can be altered, redirected, or repeated based on the application logic.

Categories of Flow Statement

In , Control flow statement can be categorized mainly in three following ways.

o Decision-making statements
o Looping statements
o Jump statements
Saurabh sir c++ notes english

Decision-Making Statements

The Decision-making statements allow us to determine which statement to execute based on the
test expression at runtime. Decision-making statements are also known as the Selection
statements. In program, single or multiple test expression (or condition) can be existed, which
evaluates Boolean TRUE and FALSE. These results of the expression/condition helps to decide
which block of statement (s) will execute if the given condition is TRUE or FALSE.

provides following types of Decision-making statement.

o If Statement
o If-else Statements
o If else if Statement
o Switch Case Statement

1. if statement
This consists of a single expression followed by one or more statements. If the expression
evaluates to true, then the statement body is executed. Otherwise, the statement body is skipped.
The statement body is enclosed in curly braces.

An example is given below:

#include <iostream.h>
main () {
// local variable :
int x = 20;

// check if the expression is true


if( x < 100 ) {
// the statement body
cout << "x is less than 20 \n";
}
cout << "value of x is : " << x;

iostream.h
}
Output

a is less than 20;


value of a is : 10
Saurabh sir c++ notes english

2. if..else statement
This is an if statement followed by an else part. The else part is executed if the conditional
expression is false.

An example is given below:

main () {
int age;
cout << "How old are you? ";
cin >> age;

if(age > 16) {


cout << "You are an adult!";
}
else {
cout << "You are not yet an adult! ";
}
cout << "Because you are " << age << " years old";
iostream.h
}

3. nested if statements
In this case, we use on if..else statement inside another on. It will be best understood with an
example. The previous example is modified to include a nested if statement

main () {
int age;
cout << "How old are you? ";
cin >> age;

if(age > 16) {


cout << "You are an adult! /n";
// Nested if statement
if(age < 18){
cout << "But you cannot enlist!"
}
}
else {
cout << "You are not yet an adult! ";
}
cout << "Because you are " << age << " years";
}
Saurabh sir c++ notes english

4. switch statement
The switch statement enables you to test a particular variable against a list of values. When the
variable matches any of the items in the list, a particular statement or statements are executed.

The syntax for the switch statement is given below:

switch(expression) {
case expression_1 :
statements;
break;
case expression_2 :
statements;
break;
...
...
case expression_n :
statements;
break;
default : //Optional
statements;
}

Looping Statements

looping statements are used to execute the block of code multiple-times for the given number of
time until it matches the given condition. These statements are also called Iteration statement.

provides following types of the looping statements.

o for loop
o for….in loop
o while loop
o do while loop

For Loop
Loop is an entry controlled loop, meaning that the condition specified by us is verified before

entering the loop block. It is a repetition control structure. The loop written by us is run a

specified number of times.


Saurabh sir c++ notes english

To control the loop, we use a loop variable in For loop. This variable is first initialized to some

value, then we perform the check on this variable comparing it to the counter variable, and

finally, we update the loop variable.

Syntax:

for(initialization expression; test expression; update expression)

// statements to execute in the loop body


}

Initialization Expression:

Here we initialize the loop variable to a particular value. For example, int i=1;

Let us look at an example of For loop:

#include <iostream.h>

int main()

for (int i = 1; i <= 5; i++)

cout << " Good morning \n";

}
}
Saurabh sir c++ notes english

While Loop
While loop is also an entry controlled loop, where we verify the condition specified by us, before

running the loop. The difference is being that we use For loops when we know the number of

times the body of the loop needs to run, whereas we use while loops in circumstances

when beforehand we do not know the precise number of times the body of the loop needs to run.

The execution of the loop is terminated based on the test condition.

Syntax:

initialization expression;

while (test_expression)

// statements to execute in the loop body

update_expression;
}

Jump Statements

Jump statements are used to jump from another statement, or we can say that it transfers the
execution to another statement from the current statement.

provides following types of jump statements -

o Break Statement
o Continue Statement

The above jump statements behave differently.


Saurabh sir c++ notes english

Continue
It is used to execute other parts of the loop while skipping some parts declared inside the
condition, rather than terminating the loop, it continues to execute the next iteration of the same
loop.

Break
It is used to terminate the whole loop if the specified condition is met. Unlike the continue
statement once the condition is met, break breaks the loop and the remaining part of the loop is
not executed.

You might also like