Class 10 | Concept of OOP using C++ | C++ Notes

Introduction to OOP
OOP (Object Oriented Programming) is a programming paradigm that was developed to overcome drawbacks and limitations of POP (Procedure Oriented Programming). The OOP was developed by
Alan Klay . OOP allows decomposition of problem into number of entities called object and then build data and functions around these objects.

Characteristics of OOP:
i) Emphasis on data rather than procedure
ii) Program are divided into object
iii) Data is hidden and can’t be accessed by external function
iv) Objects communicate with each other through functions
v) New data and functions can be added easily
vi) Supports bottom up approach

Features of OOP:-
Class: It is the collection of similar objects which is defined as the blueprint to define data and method for all the object of class. It is user-defined data type because user  define and create class.
Object: It is a real world entity having data and method. It is the instance of class.
Inheritance: It is the process of creating class through existing class in which property of a class is inherited by another class. The class whose properties are being inherited is called base or parent class. The class who is inheriting the property of parent class is called child class or derived class.
Polymorphism: It is defined as the property of an object to take own different form depending upon condition.
Abstraction: It is the hiding of background and showing of useful information only. It focuses the outside view of an object separating its essential behavior from its implementation.
Encapsulation: It is the process of organizing data and method into a single unit. It’s  best application is making data private and access it using public modifier.

Advantages of OOP:-
i) We can control redundancy of code through inheritance
ii) Easy for managing complex and large sized programs
iii) It takes very less time for development and maintenance of the software
iv) Reusability of code
v) Enhanced security and protection

Application of OOP :-
i) Image processing and pattern recognition
ii) Computer Aided Design
iii) object oriented DBMS
iv) Internet and web based application
v) mobile computing
vi) Digital Electronic
vii) Data warehouse and data mining

Difference between class and object:

Difference between OOP and POP:

Tokens in C++
Tokens are the smallest unit of C++  program which are meaningful to the compiler.
Some tokens of C++ are:-
Constant: Constants are values that do not change during the execution of a program. In C++, constants can be of various types, such as 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 quotes (). 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).There are 32 keywords and all are written in lowercase.
Identifiers:  Identifiers are names given to various program elements, such as variables, functions, classes, and labels. Identifiers are used to uniquely identify these elements within a program.
Rules for naming identifier:-
1) first character must be in underscore or character
2) must consist of only alphabet, digit or underscore
3) must not be a keyword
4) must not contain white space

Operator: Operators are special symbols that are used to perform operations on one or more operand.
Types of operator:-
i) Unary Operator
This operator needs only one operand to act upon. There are two unary operators:  Increment and Decrement.
Increment operator is used to increment value of operand by 1. It can be either pre increment (++b) or
post increment (b++).
Decrement operator is used to decrement value of operand by 1. It can be either pre decrement (–b) or
post decrement (b- -).

ii) Binary operator
This operator needs two operands to act upon.
Types of binary operator are:
Arithmetic operator
is used to perform arithmetic operations like addition, subtraction, multiplication, division, etc.
Logical Operator is used to evaluate either the operation Is true or false. These operators are used in decision making. AND (&&), OR (||), NOT (!), etc.
Assignment operator is used to assign value for variable. The most common assignment operator is equivalent
symbol (=).
Relational operator is used to check if the given condition is true or false. If the condition is true it returns 1 else returns 0.
Bitwise operator is used to manipulate data on the bit level. Bitwise AND(&), Bitwise OR(/),etc.

iii) Ternary Operator
This operator needs three operands to act upon. Conditional operator is a type of ternary operator.
Conditional operators are type of operator that make use of condition inside the program.
Syntax of conditional operator:
if(condition)? True statement: False statement;

Character set: The term “character set”  refers to a specific collection of characters and symbols that a computer system can understand and manipulate. It’s essentially a mapping between numbers (codes) and characters.
There are several characters in character set:-
 i) Alphabets:  These include letters from the alphabet, both uppercase and lowercase.
For example, ‘A’, ‘B’, ‘C’, …, ‘Z’ and ‘a’, ‘b’, ‘c’, …, ‘z’.
ii) Numeric Characters: These are the digits used to represent numbers. They include ‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, and ‘9’.
iii) Whitespace Characters: These characters represent space, tab, newline, carriage return, and other types of spacing characters used for formatting and layout.
iv) Special Characters: Special characters encompass a wide range of symbols, such as mathematical symbols (‘+’, ‘-‘, ‘*’, ‘/’, ‘=’, etc.), currency symbols (‘$’,’€’,’¥’, etc.), copyright symbol (‘©’), trademark symbol (‘™’), and others.

Data Types: Data types are a classification of the various types of data that a variable or expression can hold or produce in a program. These types define the operations that can be performed on the data and the way it is stored in memory.
Data types can be broadly categorized into Primary, Derived, and User-defined types.
i) Primary Data Types: Primary data types, also known as fundamental data types, are the basic building blocks of data manipulation. They are directly supported by the programming language. These data types represent basic data entities like integers, floating-point numbers, characters, and Boolean values.
Examples of primary data types in C++ include:
Integer types: int, short, long, long , unsigned int, etc.
Floating-point types: float, double, long double
Character types: char, unsigned char, wchar_t, char16_t, char32_t
Boolean type: bool

ii) Derived Data Types: Derived data types are formed by combining primary data types or other derived types to create complex data structures.
Examples of derived data types in C++ 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 to operate on that data, forming the basis of object-oriented programming in C++.

iii) User-defined Data Types: User-defined data types are created by the programmer based on their specific requirements. They are defined using primary and derived data types. User-defined data types allow programmers to model real-world entities more effectively and encapsulate complex functionality into reusable components. They play a crucial role in making code modular, maintainable, and easier to understand.
Examples of User-defined data types in C++ include:
Structures and Classes: As mentioned above, structures and classes allow programmers to create custom data types with specific attributes and behaviors.
Enumerations (enums): A way to create a new integer type with a set of named constants.
Type-defs: A way to create aliases for existing data types, improving code readability and portability.

Format specifiers : Format specifiers are primarily used with the formatted input/output operations provided by the <iostream> library, such as cout for output and cin for input. The format specifiers are similar to those used in C, but with a slightly different syntax due to the object-oriented nature of C++.
Some common format specifiers used in C++ with cout and cin:
%d
: Used to format integers (decimal).
%f: Used to format floating-point numbers.
%c: Used to format characters.
%s: Used to format strings (null-terminated character arrays).
%p: Used to format pointers (memory addresses).
 %X: Used to format integers in hexadecimal format.
%o: Used to format integers in octal format.
%u: Used to format unsigned integers.

Basic Input/ Output functions
In C++, input and output operations are typically performed using the <iostream> library, which provides classes like cin and cout for input and output, respectively.
Some basic input/output functions commonly used in C++:
cout: This is the standard output stream object, used to output data to the standard output device (usually the console).
cin: This is the standard input stream object, used to read data from the standard input device (usually the keyboard).
endl: This inserts a newline character into the output stream and flushes the stream.
cerr: This is the standard error stream, used for writing error messages to the standard error device (typically the console). Unlike cout, output to cerr is usually unbuffered, which means it’s immediately display.
clog: This is the standard logging stream, used for writing log messages to the standard logging device (typically the console). It behaves similarly to cerr but is often used for less critical messages.

Input / Output Header File
In C++, there are several header files related to input/output operations.
Some of commonly used header-file along with their main purposes:
<iostream>: This header provides basic input/output services for C++. It includes declarations for the standard stream objects (cin, cout, cerr, clog) and other necessary functionality for input and output.
<iomanip>: This header provides facilities to manipulate the format of input and output. It includes manipulators for controlling the formatting of output, such as setw, setprecision, fixed, etc.
<fstream>: This header provides facilities for file input and output operations in C++. It includes classes like ifstream (for reading from files), ofstream (for writing to files), and fstream (for both reading and writing).

Procedure oriented Programming
POP is a conventional programming paradigm in which large programs are divided into small sub-programs and modules. This uses top down approach. It is also called structure oriented programming.
Characteristics of POP:-
i) Use top down approach
ii) Data can flow freely among functions
iii) Emphasis is given on algorithm or function
iv) Large programs are divided into function
v) Functions share global data
vi) Function transfer data into multiple forms

Advantages of POP:-
i) Excellent for general purpose programs
ii) simplicity of code
iii) Source code is portable
iv) Reusable
v) Needs less memory
vi) Stateless Programs
vii) Easily testable code

Dis-advantages of POP:-
i) Data can’t be altered
ii) Gaining large amount of data is not possible efficiently
iii) Not recommendable for connection to server and database
iv)lack of data security
v) Difficult to maintain code for large programs
vi) Lack of modern OOP features
vii) Lack of code Reusability
viii) Less durable

Control Flow Statement
  Control flow statements are used to control the execution flow of a program based on certain conditions. There are 3 types of control flow statement:

1) Conditional Statement: This statement allows program to execute different block of codes based on given condition. There are 4 conditional statement:

i) if statement: In this statement if the condition evaluates true then it executes the program and program is skipped if the condition is false.
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;
}

ii)if else statement: In this statement if the condition evaluates true then it executes the program and if the condition evaluate false it execute another block of code.
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;
}

iii) if else if ladder statement:  It is a multi-conditional statement in which multiple values or variable are compared together to find true one.
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;
}

iv) Switch Case: This allows variable to be tested against list of values. Each value is called 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: This statement is also called as loop or iteration statement. There are 3 types of looping statement:

i) for loop: The for loop is a control flow statement that repeats a block of code a specified number of times. It consists of three parts: initialization, condition, and update .The initialization is executed once at the beginning of the loop. The condition is checked before each iteration, and if it evaluates to true, the loop body is executed. After each iteration, the update statement is executed .If the condition evaluates to false, the loop terminates.
Example:
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; ++i) {
cout << i << ” “;
}
return 0;
}

ii) while loop: The while loop repeats a block of code as long as a specified condition is true .It only consists of a condition. The condition is checked before each iteration, and if it evaluates to true, the loop body is executed. If the condition evaluates to false initially, the loop body is never executed.
Example:
#include <iostream>
using namespace std;
int main() {
int i = 1;
while (i <= 5) {
cout << i << ” “;
++i;
}
return 0;
}

iii) Do while loop: The do-while loop is similar to the while loop, but it ensures that the loop body is executed at least once before the condition is checked. It consists of a condition and a block of code. The loop body is executed once, and then the condition is checked. If the condition evaluates to true, the loop body is executed again, and the process continues. If the condition is false initially, the loop terminates.
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 is a control flow statement in programming that alters the normal flow of execution within a program. It’s used to transfer control from one part of the code to another.
There are several types of jumping statements:
i) Break Statement: It is used to terminate the loop or switch statement and transfers control to the statement immediately following the terminated loop or switch. For example, in a loop, if a certain condition is met, you may want to exit the loop early.
Example:
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 10; ++i) {
cout << i
if (i > 5) {
break;
}
}
cout << std::endl;
return 0;
}

ii) Continue Statement: It skips the current iteration of a loop and proceeds to the next iteration. It’s often used when you want to skip certain elements or conditions within a loop.
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;
}

iii) Return Statement: It terminates the execution of a function and returns control to the calling function. It’s used to return a value from a function back to the caller.
Example:
#include <iostream>
using namespace std;
return a + b;
}
int main() {
int result = add(5, 3); // Calling the add function
cout << “The result of addition is: ” << result << endl;
return 0;
}

Write a program in C++ to check if the 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;
}

Write a program in C++ to check if the given 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;
}

 

 

 

 

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *