Introduction to OOP
OOP (Object Oriented Programming) is a programming paradigm that was developed to overcome the drawbacks and limitations of POP (Procedure Oriented Programming).
OOP allows the decomposition of a problem into a number of entities called objects, and then data and functions are built around these objects.
Characteristics of OOP
- Emphasis on data rather than procedure.
- Programs are divided into objects.
- Data is hidden and cannot be accessed by external functions.
- Objects communicate with each other through functions.
- New data and functions can be added easily.
- Supports a bottom-up approach.
Features of OOP
Class
A class is a collection of similar objects and acts as a blueprint for defining data and methods for all objects of that class.
It is a user-defined data type because the user defines and creates the class.
Object
An object is a real-world entity having data and methods. It is an instance of a class.
Inheritance
Inheritance is the process of creating a class through an existing class in which the properties of one class are inherited by another class.
The class whose properties are being inherited is called the base class or parent class.
The class that inherits the properties of the parent class is called the child class or derived class.
Polymorphism
Polymorphism is the property of an object to take different forms depending upon the condition.
Abstraction
Abstraction is the process of hiding background details and showing only useful information.
It focuses on the outside view of an object while separating its essential behavior from its implementation.
Encapsulation
Encapsulation is the process of organizing data and methods into a single unit.
Its best application is making data private and accessing it using a public modifier.
Advantages of OOP
- We can control redundancy of code through inheritance.
- Easy to manage complex and large-sized programs.
- It takes less time for software development and maintenance.
- Reusability of code.
- Enhanced security and protection.
Applications of OOP
- Image processing and pattern recognition.
- Computer-Aided Design.
- Object-Oriented DBMS.
- Internet and web-based applications.
- Mobile computing.
- Digital electronics.
- Data warehouse and data mining.
Difference Between Class and Object
The provided notes contain this heading, but the comparison content is not included in the source.
Difference Between OOP and POP
The provided notes contain this heading, but the comparison content is not included in the source.
Tokens in C++
Tokens are the smallest units of a C++ program that are meaningful to the compiler.
Some common C++ tokens are:
Constant
Constants are values that do not change during the execution of a program.
In C++, constants can be integer constants, floating-point constants, character constants, or string literals.
Variable
Variables are named memory locations used to store data that can change during program execution.
String
Strings are sequences of characters enclosed within double quotation marks.
In C++, strings can be represented using the string class from the Standard Template Library (STL).
Keyword
Keywords are reserved words that have special meanings and purposes in the C++ language.
They cannot be used as identifiers such as variable names.
Identifier
Identifiers are names given to program elements such as variables, functions, classes, and labels.
They are used to uniquely identify these elements within a program.
Rules for Naming Identifiers
- The first character must be an underscore or character.
- It must consist only of alphabets, digits, or underscores.
- It must not be a keyword.
- It must not contain white space.
Operators in C++
Operators are special symbols that are used to perform operations on one or more operands.
Types of Operators
1. Unary Operator
A unary operator needs only one operand.
There are two common unary operators:
Increment Operator: It increases the value of an operand by 1.
Examples:
++b — Pre-increment
b++ — Post-increment
Decrement Operator: It decreases the value of an operand by 1.
Examples:
–b — Pre-decrement
b– — Post-decrement
2. Binary Operator
A binary operator requires two operands.
Common types are:
Arithmetic Operator: Used to perform arithmetic operations such as addition, subtraction, multiplication, and division.
Logical Operator: Used to evaluate whether a condition is true or false.
Examples:
AND — &&
OR — ||
NOT — !
Assignment Operator: Used to assign a value to a variable.
Example:
=
Relational Operator: Used to check whether a given condition is true or false.
Bitwise Operator: Used to manipulate data at the bit level.
3. Ternary Operator
A ternary operator requires three operands.
The conditional operator is a type of ternary operator.
General form:
condition ? true statement : false statement
Character Set
The term character set refers to a collection of characters and symbols that a computer system can understand and manipulate.
Types of Characters
1. Alphabets
These include uppercase and lowercase letters.
Examples:
A, B, C, …, Z
a, b, c, …, z
2. Numeric Characters
These include:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
3. Whitespace Characters
These include spaces, tabs, newlines, carriage returns, and other spacing characters used for formatting.
4. Special Characters
These include different symbols such as:
+, -, *, /, =, $, €, ¥, ©, ™
Data Types
Data types classify the types of data that a variable or expression can hold or produce in a program.
They define how data is stored in memory and what operations can be performed on it.
Data types are broadly categorized into:
- Primary Data Types
- Derived Data Types
- User-defined Data Types
1. Primary Data Types
Primary data types are also known as fundamental data types.
They are directly supported by the programming language.
Examples include:
Integer Types:
int, short, long, unsigned int
Floating-Point Types:
float, double, long double
Character Types:
char, unsigned char, wchar_t, char16_t, char32_t
Boolean Type:
bool
2. Derived Data Types
Derived data types are formed by combining primary data types or other derived types.
Examples include:
Arrays: Collection of elements of the same data type.
Pointers: Variables that store memory addresses of other variables.
References: Aliases to other variables or objects.
Structures: User-defined composite data types that group related data items together.
Classes: User-defined data types that encapsulate data and methods.
3. User-Defined Data Types
User-defined data types are created by programmers based on their requirements.
Examples include:
Structures and Classes: Used to create custom data types with specific attributes and behaviors.
Enumerations (Enums): Used to create a new integer type with a set of named constants.
Type-defs: Used to create aliases for existing data types.
Format Specifiers
Some common format specifiers mentioned in the notes are:
- %d — Integer
- %f — Floating-point number
- %c — Character
- %s — String
- %p — Pointer or memory address
- %X — Hexadecimal integer
- %o — Octal integer
- %u — Unsigned integer
Basic Input/Output Functions
In C++, input and output operations are commonly performed using the iostream library.
cout
Used to display output on the standard output device, usually the screen.
cin
Used to receive input from the standard input device, usually the keyboard.
endl
Used to insert a new line and flush the output stream.
cerr
Used to display error messages.
clog
Used to display logging messages.
Input/Output Header Files
iostream
Provides basic input and output services in C++.
It includes:
cin
cout
cerr
clog
iomanip
Used to control input and output formatting.
Examples include:
setw
setprecision
fixed
fstream
Used for file input and output operations.
It includes:
ifstream
ofstream
fstream
Procedure Oriented Programming (POP)
POP is a conventional programming paradigm in which large programs are divided into small sub-programs and modules.
It uses a top-down approach.
It is also called structure-oriented programming.
Characteristics of POP
- Uses a top-down approach.
- Data can flow freely among functions.
- Emphasis is given to algorithms or functions.
- Large programs are divided into functions.
- Functions share global data.
- Functions transfer data into multiple forms.
Advantages of POP
- Excellent for general-purpose programs.
- Simplicity of code.
- Source code is portable.
- Reusable.
- Needs less memory.
- Stateless programs.
- Easily testable code.
Disadvantages of POP
- Data cannot be altered.
- Handling a large amount of data is not efficient.
- Not recommendable for connection to servers and databases.
- Lack of data security.
- Difficult to maintain code for large programs.
- Lack of modern OOP features.
- Lack of code reusability.
- Less durable.
Control Flow Statement
Control flow statements are used to control the execution flow of a program based on certain conditions.
There are three main types:
- Conditional Statement
- Repetitive Statement
- Jumping Statement
1. Conditional Statement
A conditional statement allows a program to execute different blocks of instructions based on a given condition.
There are four types:
If Statement
The if statement executes a block of instructions if the given condition is true.
Example:
#include <iostream>
using namespace std;
int main() {
int number;
cout << “Enter an integer: “;
cin >> number;
if (number > 0) {
cout << “The number is positive.” << endl;
}
return 0;
}
If-Else Statement
If the condition is true, one block is executed. If the condition is false, another block is executed.
Example:
#include <iostream>
using namespace std;
int main() {
int number;
cout << “Enter an integer: “;
cin >> number;
if (number % 2 == 0) {
cout << “The number is even.” << endl;
}
else {
cout << “The number is odd.” << endl;
}
return 0;
}
If-Else-If Ladder
It is a multi-conditional statement where multiple conditions are checked.
Example:
#include <iostream>
using namespace std;
int main() {
int score;
cout << “Enter your score: “;
cin >> score;
if (score >= 90) {
cout << “Grade A” << endl;
}
else if (score >= 80) {
cout << “Grade B” << endl;
}
else if (score >= 70) {
cout << “Grade C” << endl;
}
else if (score >= 60) {
cout << “Grade D” << endl;
}
else {
cout << “Grade F” << endl;
}
return 0;
}
Switch Case
A switch statement allows a variable to be tested against a list of values.
Each value is known as a case.
Example:
#include <iostream>
using namespace std;
int main() {
int day;
cout << “Enter a number (1-7) representing the day of the week: “;
cin >> day;
switch (day) {
case 1:
cout << “Sunday” << endl;
break;
case 2:
cout << “Monday” << endl;
break;
case 3:
cout << “Tuesday” << endl;
break;
case 4:
cout << “Wednesday” << endl;
break;
case 5:
cout << “Thursday” << endl;
break;
case 6:
cout << “Friday” << endl;
break;
case 7:
cout << “Saturday” << endl;
break;
default:
cout << “Invalid input! Please enter a number between 1 and 7.” << endl;
}
return 0;
}
2. Repetitive Statement
A repetitive statement is also called a loop or iteration statement.
There are three types of loops:
For Loop
The for loop repeats a block of instructions a specified number of times.
It contains three parts:
- Initialization
- Condition
- Update
Example:
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; ++i) {
cout << i << ” “;
}
return 0;
}
While Loop
A while loop repeats a block of instructions as long as a given condition remains true.
Example:
#include <iostream>
using namespace std;
int main() {
int i = 1;
while (i <= 5) {
cout << i << ” “;
++i;
}
return 0;
}
Do-While Loop
A do-while loop is similar to a while loop, but the block of instructions is executed at least once before the condition is checked.
Example:
#include <iostream>
using namespace std;
int main() {
int i = 1;
do {
cout << i << ” “;
++i;
} while (i <= 5);
return 0;
}
3. Jumping Statement
A jumping statement changes the normal flow of execution in a program.
Break Statement
The break statement is used to terminate a loop or switch statement.
Example:
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 10; ++i) {
cout << i;
if (i > 5) {
break;
}
}
cout << endl;
return 0;
}
Continue Statement
The continue statement skips the current iteration and moves to the next iteration.
Example:
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 10; ++i) {
if (i % 2 == 0) {
continue;
}
cout << i << ” “;
}
cout << endl;
return 0;
}
Return Statement
The return statement terminates the execution of a function and returns control to the calling function.
It can also return a value to the caller.
C++ Program to Check Whether a Number is Palindrome or Not
#include <iostream>
using namespace std;
int main() {
int num, originalNum, remainder, reversedNum = 0;
cout << “Enter an integer: “;
cin >> num;
originalNum = num;
while (num != 0) {
remainder = num % 10;
reversedNum = reversedNum * 10 + remainder;
num /= 10;
}
if (originalNum == reversedNum)
cout << originalNum << ” is a palindrome.” << endl;
else
cout << originalNum << ” is not a palindrome.” << endl;
return 0;
}
C++ Program to Check Whether a Number is Armstrong or Not
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int num, originalNum, remainder, result = 0, n = 0;
cout << “Enter an integer: “;
cin >> num;
originalNum = num;
while (originalNum != 0) {
originalNum /= 10;
n++;
}
originalNum = num;
while (originalNum != 0) {
remainder = originalNum % 10;
result += pow(remainder, n);
originalNum /= 10;
}
if (result == num)
cout << num << ” is an Armstrong number.” << endl;
else
cout << num << ” is not an Armstrong number.” << endl;
return 0;
}
Discussion
Share a helpful question, idea, or explanation with other students.