OOPPSSSS
OOPPSSSS
Procedural programming in C++ is a programming paradigm that focuses on procedures (also known as
functions) to operate on data. It's one of the fundamental styles of programming and is heavily
influenced by the C language, from which C++ evolved.
Encapsulation: Encapsulation is the process of combining data and functions into a single unit
called a class. It hides the internal details of an object and only exposes necessary parts using public
functions. This protects data from accidental modification.
Abstraction: Abstraction means hiding complex implementation details and showing only the
essential features of an object. It helps in reducing complexity and increasing readability.
Inheritance: Inheritance allows one class (derived class) to acquire the properties and behaviors of
another class (base class). It promotes code reusability and helps in building hierarchical relationships
between classes.
Polymorphism: Polymorphism means the ability to take many forms. In C++, polymorphism can
be compile-time (function overloading and operator overloading) or run-time (using virtual functions). It
allows the same function to behave differently for different objects.
1 Program is divided into small partscalled Program is divided into parts called
functions. objects.
3 It does not have any proper way for hiding OOP provides Data Hiding so provides
data so it is less secure. more security.
Benefits of OOPs
Reuse code: You can use old code to make new programs without starting from scratch.
Easy to fix: You can fix or change one part without breaking everything.
Data safety: OOP keeps important information safe and only shares what’s needed.
Grows easily: You can add new things to your program without much trouble.
Real-life examples: It helps you design programs by copying how things work in real life.
Difference Between C and C++
C C++
C was developed in 1972 by Dennis Ritchie C++ was developed by Bjarne Stroustrup at
at Bell Laboratories. Bell Laboratories in the early 1980s.
The file extension of a program in C The file extension of a C++ program is .cpp.
language is .c.
C uses <stdio.h> header file for C++ uses <iostream.h> header file for
input/output operations. input/output operations.
Tokens
In C++, a token is the smallest unit of a program that has meaning to the compiler. When the C++
compiler reads a program, it breaks the entire source code into meaningful elements called tokens.
There are six types of tokens in C++:
1. Keywords: Reserved words used by the language that have a special meaning. Example:
int, float, if, while, return
2. Identifiers: Names used for variables, functions, arrays, classes, etc. Example: age, sum,
displayResult
3. Constants (Literals): Fixed values that do not change during program execution.
Example: 10, 3.14, 'A', "Hello"
5. Punctuation / Special Symbols: Includes characters like ;, {, }, (), [], #, , which help
structure the code.
Identifiers
Identifiers are the names given to various program elements such as variables, functions, arrays, objects,
etc. They are created by the programmer to uniquely identify elements in the code.
Types of Variables:
Local Variables: Declared inside functions or blocks; accessible only within that scope.
Global Variables: Declared outside all functions; accessible throughout the program.
Static Variables: Retain their value between function calls.
Constants
A constant is a value that cannot be changed during program execution. It is used to define fixed values
that remain the same throughout the program.
Types of Constants:
Literal Constants: Direct values like 10, 3.14, 'A', "Hello"
Symbolic Constants: Declared using const keyword or #define directive.
Reference variables
Reference variable is an alternative name of already existing variable. It cannot be changed to refer
another variable and should be initialized at the time of declaration and cannot be NULL. The operator
‘&’ is used to declare reference variable.
1. Character Data Type (char): The character data type is used to store a single character.
The keyword used to define a character is char. Its size is 1 byte and it stores characters enclosed in
single quotes (‘ ‘). It can generally store upto 256 characters according to their ASCII codes.
2. Integer Data Type (int): Integer data type denotes that the given variable can store the
integer numbers. The keyword used to define integers is int. Its size is 4-bytes (for 64-bit) systems and
can store numbers for binary, octal, decimal and hexadecimal.
3. Boolean Data Type (bool): The boolean data type is used to store logical values: true(1)
or false(0). The keyword used to define a boolean variable is bool. Its size is 1 byte.
4. Floating Point Data Type (float): Floating-point data type is used to store numbers
with decimal points. The keyword used to define floating-point numbers is float. Its size is 4 bytes (on 64-
bit systems).
5. Double Data Type (double):The double data type is used to store decimal numbers
with higher precision. The keyword used to define Double Data Type numbers is double. Its size is 8
bytes (on 64- bit systems).
6. Void Data Type (void): The void data type represents the absence of value. We cannot
create a variable of void type. It is used for pointer and functions that do not return any value using the
keyword void.
Streams in C++
In C++, streams are used for input and output (I/O) operations. A stream is a flow of data — either from
input devices to the program (input stream) or from the program to output devices (output stream). C++
handles I/O through the use of streams, provided by the iostream library.
Error Stream (cerr and clog):cerr is used to output error messages (unbuffered). clog is
used for logging information (buffered).
C++ Operators
The C++ programming language provides a wide range of operators to perform mathematical, logical,
and other operations. An operator is a symbol used to perform mathematical and logical operations.
Arithmetic Operators
The arithmetic operators are the symbols that are used to perform basic mathematical operations like
addition, subtraction, multiplication, division and percentage modulo.
Relational Operators
The relational operators are the symbols that are used to compare two values. That means the relational
operators are used to check the relationship between two values.
Logical Operators
The logical operators are the symbols that are used to combine multiple conditions into one condition.
Bitwise Operators
The bitwise operators are used to perform bit-level operations in the c programming language. When we
use the bitwise operators, the operations are performed based on the binary values.
Conditional Operator
The conditional operator is also called a ternary operator because it requires three operands. This
operator is used for decision making.