0% found this document useful (0 votes)
10 views11 pages

Oops

The document provides an overview of Object-Oriented Programming (OOP) using C++, covering key concepts such as classes, objects, encapsulation, inheritance, polymorphism, and abstraction. It highlights the benefits of OOP, including modularity, reusability, maintainability, and data protection, and discusses popular object-oriented languages. Additionally, it outlines various applications of OOP in fields like game development, software development, embedded systems, and telecommunications.

Uploaded by

sayyedanam9653
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)
10 views11 pages

Oops

The document provides an overview of Object-Oriented Programming (OOP) using C++, covering key concepts such as classes, objects, encapsulation, inheritance, polymorphism, and abstraction. It highlights the benefits of OOP, including modularity, reusability, maintainability, and data protection, and discusses popular object-oriented languages. Additionally, it outlines various applications of OOP in fields like game development, software development, embedded systems, and telecommunications.

Uploaded by

sayyedanam9653
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/ 11

FY Bsc IT

Name of the Course: Introduction to OOP using C++


MODULE:-1

❖ Introduction to Programming Concepts: Object oriented programming paradigm


OOPs (Object-Oriented Programming) is a programming paradigm or style that is based on the concept of objects. It
organizes code by grouping related data and functions together into "objects," which are instances of a "class." OOPs is
widely used because it helps in organizing and managing complex code, making it more modular, reusable, and easier to
maintain.
1. Objects
An object is like a real-world object that has characteristics (called attributes) and behaviors (called methods). For
example, if we think of a "dog" as an object:
• Attributes: Breed, color, age.
• Methods: Bark, run, eat.
In programming, an object would store data (like the dog's breed) and have functions to perform tasks (like the dog
barking).
2. Classes
A class is a blueprint or template for creating objects. It defines the attributes and behaviors that the objects of that class
will have. For example, a "Car" class might define attributes like "color" and "model," and methods like "drive" or
"stop." When you create a specific car, like a red "Toyota," it becomes an object of the "Car" class.
3. Encapsulation
Encapsulation is the idea of bundling the data (attributes) and the methods that work on the data into a single unit,
called an object. It also involves controlling access to the object's data, so you can protect it from being changed in
unintended ways. You might hide some details and provide access only through well-defined methods. This is like
putting a lock on the car’s engine to keep it safe, only allowing someone to drive the car through the ignition method.
4. Inheritance
Inheritance is a way to create new classes based on existing ones. A new class can inherit attributes and methods from a
parent class, making it easier to reuse code. For example, you could have a "Vehicle" class, and then create a "Car" class
that inherits all the properties of a vehicle, but adds some of its own, like "number of doors."
5. Polymorphism
Polymorphism means that objects of different classes can be treated as objects of a common superclass. It also refers to
the ability to use a method in different ways. For example, you could have a method called "makeSound" that works
differently for different animals: the "Dog" class makes a barking sound, while the "Cat" class makes a meowing sound,
even though they both use the same method name.
6. Abstraction
Abstraction hides the complex details and shows only the essential features of an object. It's like using a TV remote:
you don’t need to understand how the remote works internally, you just press buttons to change the channel, volume,
etc. In programming, this helps make complex systems easier to work with by focusing only on what’s necessary for the
task at hand.
Why Use OOP?
• Modularity: You can break your code into smaller, manageable pieces (objects).
• Reusability: You can reuse classes in different programs through inheritance.
• Maintainability: Changes can be made easily by updating objects or methods without affecting the entire pro-
gram.
• Real-world modeling: OOP mirrors real-world scenarios, making it easier to design and understand the code.
❖ basic concepts of object oriented programming

Summary of Basic OOP Concepts:

1. Class: A template for creating objects, defining attributes and methods.


2. Object: An instance of a class, representing real-world entities.
3. Encapsulation: Bundling data and methods into a single unit, hiding internal details.
4. Inheritance: A way for a class to inherit attributes and methods from another class.
5. Polymorphism: The ability to treat objects of different classes as objects of a common su-
perclass, often with method overriding.
6. Abstraction: Hiding the complex details and showing only the essential features of an ob-
ject.
ASST. PROF ASMAA SHAIKH 1
❖ benefits of object oriented programming
Object-Oriented Programming (OOP) in C++ provides numerous benefits that contribute to cleaner, more manageable,
and reusable code. Below are some of the key advantages of using OOP in C++:
1. Modularity
• Definition: OOP allows you to break down a program into smaller, independent modules (objects), which
makes the code easier to manage and develop.
• Benefit: Modularity helps in organizing complex systems, where each module (object) can be developed and
tested independently, improving development efficiency and collaboration.
2. Reusability
• Definition: Through inheritance and composition, C++ allows you to reuse existing code. New classes can
inherit attributes and methods from existing classes, reducing the need to duplicate code.
• Benefit: Reusability leads to reduced development time and effort, as existing classes or objects can be used as
building blocks for new software without having to rewrite functionality from scratch.
3. Scalability
• Definition: OOP in C++ makes it easier to scale software by organizing it into well-defined objects and classes
that can be extended and modified as needed.
• Benefit: As the software grows or evolves, you can add new functionality to existing classes without changing
the core design, ensuring that the software can evolve alongside user requirements.
4. Maintainability
• Definition: Since OOP divides software into smaller, manageable objects, changes or updates can be made to
specific parts of the code without affecting the entire system.
• Benefit: This makes maintenance easier because updates or fixes can be applied to a class or object, minimiz-
ing the risk of bugs or problems in other parts of the software.
5. Encapsulation
• Definition: Encapsulation refers to the concept of bundling data (attributes) and methods (functions) that oper-
ate on the data into a single unit (object). It also involves hiding the internal details of an object and exposing
only essential parts through public interfaces (methods).
• Benefit: Encapsulation protects an object's internal state from unintended interference and ensures that only
valid operations are performed. It improves security and data integrity.
6. Abstraction
• Definition: Abstraction is the concept of hiding the complex implementation details and showing only the es-
sential features of an object.
• Benefit: By abstracting unnecessary details, the software becomes easier to use and understand. This also sim-
plifies interactions with objects by providing only the necessary interface and hiding the complexity behind the
scenes.
7. Polymorphism
• Definition: Polymorphism allows objects of different classes to be treated as objects of a common superclass.
It also allows a method to behave differently based on the object calling it (method overriding).
• Benefit: Polymorphism increases flexibility and extensibility. You can write code that works with different
types of objects without needing to know the exact type of the object, improving code reusability and adapta-
bility.
8. Data Protection and Security
• Definition: Through encapsulation and access control (public, private, protected modifiers), C++ provides
mechanisms to protect data from unauthorized access and modification.
• Benefit: This protection helps maintain the integrity and security of the data, reducing the risk of accidental or
malicious changes.
❖ object oriented languages
Object-Oriented Languages are programming languages that support the Object-Oriented Programming (OOP)
paradigm, where software is structured around the concept of objects, which are instances of classes. These languages
allow developers to design and implement programs using fundamental OOP principles like encapsulation,
inheritance, polymorphism, and abstraction.
Here are some of the most popular Object-Oriented Languages:
1. C++
• Overview: C++ is an extension of the C programming language that supports both procedural and object-ori-
ented programming. It is widely used for system/software development and game programming.
2. Java
• Overview: Java is one of the most popular and widely used object-oriented programming languages, especially
for building web applications, mobile apps (Android), and large-scale systems.

ASST. PROF ASMAA SHAIKH 2


3. Python
• Overview: Python is a high-level, dynamically-typed language known for its simplicity and readability. It sup-
ports multiple programming paradigms, including object-oriented programming.

4. C#
• Overview: C# is a language developed by Microsoft as part of the .NET framework. It is commonly used for
developing desktop applications, web applications, and games (especially using Unity).
5. Ruby
• Overview: Ruby is a dynamically-typed, interpreted language known for its simplicity and productivity. It is
primarily used for web development, particularly with the Ruby on Rails framework.
❖ applications of object oriented programming
1. Game Development
• Description: C++ is one of the most popular languages used for developing video games due to its high per-
formance, control over system resources, and object-oriented features.
• Applications:
o Game engines like Unreal Engine (used for 3D games) rely heavily on C++.
o Game objects such as characters, weapons, vehicles, and environments are modeled as objects.
o OOP helps in managing different entities in the game world by using classes and inheritance to create
hierarchical relationships (e.g., a Player class might inherit from a Character class).
• Benefits: OOP allows for modular code, where different game elements (e.g., characters, weapons, levels) can
be developed and reused independently.
2. Software Development (Applications and Systems)
• Description: OOP in C++ is commonly used to develop both applications and system software, including
desktop applications, graphical user interfaces (GUIs), and backend systems.
• Applications:
o Microsoft Office applications, such as Word, Excel, and PowerPoint, use C++ for core system soft-
ware.
o Database management systems like MySQL and PostgreSQL use OOP to represent different compo-
nents like tables, queries, and transactions as objects.
o Embedded systems also use C++ for developing application-specific software with real-time require-
ments.
• Benefits: OOP's principles of encapsulation and inheritance provide maintainable and modular code, allowing
for the development of complex software systems with clean architecture.
3. Embedded Systems and Hardware Interfaces
• Description: C++ is frequently used in embedded systems programming due to its ability to interface with
hardware directly and its support for both high-level and low-level programming.
• Applications:
o Microcontroller programming: C++ allows efficient management of memory and system resources,
which is crucial in embedded systems where resources are limited.
o Device drivers: Hardware drivers for interacting with specific hardware components (e.g., printers,
sensors, network cards) are often written in C++ using OOP principles.
o IoT (Internet of Things) devices: C++ is used to develop software for devices like smart thermostats,
wearables, and sensors that rely on embedded systems.
• Benefits: OOP in embedded systems allows for clean and reusable code structures, such as creating different
classes for sensors or actuators.
4. Graphical User Interfaces (GUIs)
• Description: C++ can be used to develop rich desktop applications with graphical user interfaces (GUIs).
Frameworks like Qt and MFC (Microsoft Foundation Classes) enable the creation of GUI-based applications
in C++.
• Applications:
o Graphics software (e.g., Adobe Photoshop or GIMP) uses C++ for its performance and graphical
rendering features.
o CAD (Computer-Aided Design) applications, used in engineering and architecture, are often written
in C++ with OOP for organizing complex models and simulations.
• Benefits: OOP makes it easier to manage GUI components (buttons, text fields, windows) as individual objects
that interact with each other. Classes and inheritance help streamline the creation and management of user in-
terface elements.
5. Operating Systems and Kernel Development

ASST. PROF ASMAA SHAIKH 3


• Description: C++ is used in developing operating systems and their components, where OOP helps to manage
and organize low-level system resources effectively.
• Applications:
o The Linux kernel has portions written in C++ (though the core is in C).
o Operating system utilities: File systems, device management, memory management, and process
scheduling are often modeled as objects in OOP.
• Benefits: OOP allows better abstraction and modular design for complex system software, making it easier to
manage low-level operations and optimize system performance.
6. Real-Time Systems
• Description: Real-time systems require immediate processing of data and often work in environments where
timing and resource constraints are critical. C++ is suitable for such applications due to its efficiency and OOP
capabilities.
• Applications:
o Flight control systems: These systems use C++ to manage sensor data and control various compo-
nents of an aircraft in real time.
o Robotics: C++ is often used in robotics to handle sensors, actuators, and robotic arms, where OOP
helps model complex systems of interacting parts.
o Automotive control systems: C++ is widely used in embedded systems within automotive control
systems for managing sensors and other vehicle systems.
• Benefits: OOP helps in managing the complexity of real-time systems by abstracting complex behaviors into
manageable objects, each responsible for a specific functionality.
7. Financial Systems
• Description: C++ is widely used in the financial industry for the development of applications that require
high-performance computations, such as real-time stock market analysis, trading platforms, and risk manage-
ment software.
• Applications:
o Banking software: C++ is used to develop the backend systems for financial institutions, handling
transactions, account management, and data analysis.
o Trading platforms: Algorithms for real-time stock or forex trading are often written in C++ due to its
ability to handle high-frequency trading and large datasets.
• Benefits: The object-oriented nature of C++ helps model financial entities (e.g., transactions, accounts, stocks)
as objects that can interact with each other, making the code modular, flexible, and reusable.

8. Telecommunications and Networking

• Description: In telecommunications, where large-scale systems and complex data transmission


processes need to be managed, C++ is often used due to its efficiency and object-oriented ap-
proach.
• Applications:
o Telecommunication systems: C++ is used in the development of systems that handle
voice/data communication, like VoIP or cell phone networks.
o Network protocols: Developing network communication software such as TCP/IP
stacks and network routers often involves C++.
• Benefits: The modularity of OOP makes it easier to design and manage network-related classes,
such as packets, connections, and protocols, which can be extended or reused across different
projects.

❖ Tokens-keywords
A token is the smallest meaningful unit in a C++ program. When you write code, the compiler breaks it down into these
small parts (tokens) to understand it.
There are five types of tokens in C++:
1. Keywords
2. Identifiers
3. Literals
4. Operators
5. Punctuation (Separators)
1. Keywords

ASST. PROF ASMAA SHAIKH 4


Keywords are special reserved words in C++ that have predefined meanings for the compiler. You cannot use them as
names for variables, functions, or identifiers. These keywords are used to define the structure and logic of a C++
program.
Practical:-
#include <iostream> // Preprocessor directive
int main() { // int and return are keywords
int number = 10; // int is a keyword
if (number > 5) { // if is a keyword
std::cout << "Number is greater than 5" << std::endl;
} else {
std::cout << "Number is 5 or less" << std::endl;
}
return 0;
}

2. Identifiers
An identifier in C++ is the name you give to variables, functions, arrays, or any user-defined element in a program. It is
how you refer to those elements in your code.
Rules for Naming Identifiers
1. Must begin with a letter or an underscore (_).
• It cannot start with a digit.
• Example: myVariable, _temp
2. Can contain letters, digits, and underscores.
• No special characters like @, #, !, or spaces are allowed.
• Example: value1, total_sum, max_value
3. Cannot be a keyword.
• You cannot use reserved keywords (like int, if, while) as identifiers.
• Example: int is invalid as an identifier because it’s a keyword.
4. Case-sensitive.
• C++ treats uppercase and lowercase letters as different, so myVariable and MyVariable are two
different identifiers.
5. Should be meaningful (recommended).
• Use descriptive names that make your code easier to understand.

ASST. PROF ASMAA SHAIKH 5


• Example: count is better than c.
Examples of Valid and Invalid Identifiers
Identifier Valid or Invalid Reason
myVariable Valid Follows all rules
1variable Invalid Cannot start with a digit
_temp Valid Starts with an underscore
total#sum Invalid Contains a special character #
while Invalid while is a reserved keyword
MaxValue Valid Case-sensitive, no special characters

Practical:-
#include <iostream>

int main() {
int age = 25; // age is an identifier
float height = 5.9; // height is an identifier
std::cout << "Age: " << age << std::endl;
std::cout << "Height: " << height << std::endl;
return 0; // return is a keyword, not an identifier
}

3. Literals :-
A literal is a constant value directly written in the code. It represents fixed data that doesn’t change during program
execution.
Types:-
1.Integer Literals
These are whole numbers.
Examples: 10, -25, 0, 1000
2.Floating-Point (Decimal) Literals
These are numbers with decimal points.
Examples: 3.14, -0.001, 2.5e3 (scientific notation for 2500)
3.Character Literals
These are single characters enclosed in single quotes (' ').
Examples: 'a', 'Z', '#'
4.String Literals
These are sequences of characters enclosed in double quotes (" ").
Examples: "Hello", "C++ Programming"
Practical:-
#include <iostream>

int main() {
int age = 30; // Integer literal
float height = 5.9; // Floating-point literal
char initial = 'A'; // Character literal
std::string name = "Alex"; // String literal
bool isStudent = false; // Boolean literal

std::cout << "Name: " << name << ", Age: " << age << std::endl;
std::cout << "Height: " << height << ", Initial: " << initial << std::endl;
std::cout << "Is student: " << isStudent << std::endl;

return 0;
}

4. Operators
Operators are symbols that perform operations on variables and values.
Examples of operators:

ASST. PROF ASMAA SHAIKH 6


• + (addition), - (subtraction), * (multiplication), = (assignment), > (greater than)
5. Punctuation (Separators)
Punctuation includes symbols like commas, semicolons, parentheses, curly braces, etc., which help in structuring the
code.
Examples of punctuation:
• { } (curly braces) define blocks of code.
• ( ) (parentheses) are used for function calls or conditions.
• ; (semicolon) is used to terminate a statement.

❖ constants-integer
In C++, constants are fixed values that do not change during the execution of a program. Integer constants specifically
represent whole numbers without any fractional or decimal part.
Types of Integer Constants in C++
Integer constants represent fixed whole numbers.
• Types of integer constants:
• Decimal (base 10)
• Octal (base 8, prefixed with 0)
• Hexadecimal (base 16, prefixed with 0x or 0X)
• Use the const keyword or #define to create constants that cannot be changed during program execution.

Practical:-
#include <iostream>
#define MAX_SPEED 120 // Defining an integer constant using #define

int main() {
std::cout << "Max speed is: " << MAX_SPEED << std::endl;
return 0;
}
❖ character and string constants
1. Character Constants
A character constant represents a single character enclosed in single quotes ('). It can store any
single character, such as a letter, digit, or symbol.
Example:-
char letter = 'A'; // 'A' is a character constant
char digit = '5'; // '5' is a character constant
char symbol = '#'; // '#' is a character constant
Rules for Character Constants:
1. Must be enclosed in single quotes (' ').
2. Can only contain one character.
3. Can be an alphanumeric character, punctuation, or a special symbol.
4. The size of a character constant is 1 byte (since it stores an ASCII value).
Special Character Constants:
Some characters have special meanings and are represented using escape sequences. These start
with a backslash (\).
Escape Sequence Meaning
\n Newline

ASST. PROF ASMAA SHAIKH 7


Escape Sequence Meaning
\t Horizontal Tab
\\ Backslash (\)
\' Single Quote (')
\" Double Quote (")
\0 Null Character

2. String Constants
A string constant (also called a string literal) represents a sequence of characters enclosed in
double quotes (" "). Strings in C++ are actually arrays of characters, ending with a special null
character (\0).
Example: Using Character Constants in Arithmetic
#include <iostream>

int main() {
char letter = 'A';
std::cout << "ASCII value of 'A': " << int(letter) << std::endl;
std::cout << "Next letter: " << char(letter + 1) << std::endl;

return 0;
}
Example :Program Using Character and String Constants
#include <iostream>
#include <string> // For using std::string

int main() {
char grade = 'A'; // Character constant
std::string message = "Welcome!"; // String constant

std::cout << "Grade: " << grade << std::endl;


std::cout << "Message: " << message << std::endl;

return 0;
}
❖ backslash constants
In C++, backslash constants are used to represent special characters in strings and character constants. These
are called escape sequences, and they allow us to include characters that are difficult or impossible to type
directly (like newlines, tabs, or even a backslash itself).
Escape sequences always begin with a backslash (\), followed by a character or sequence of characters that
defines the special meaning.

Common Backslash Constants (Escape Sequences)


Escape
Meaning Usage Example
Sequence
\n Newline (line break) std::cout << "Hello\nWorld!";
\t Horizontal tab std::cout << "Hello\tWorld!";
\\ Backslash (\) std::cout << "C:\\Windows\\System32";

ASST. PROF ASMAA SHAIKH 8


Escape
Meaning Usage Example
Sequence
\' Single quote (') std::cout << "It\'s a great day!";
\" Double quote (") std::cout << "He said, \"Hello!\"";
\0 Null character (marks the end of a string) char str[] = "Hello\0World";
Carriage return (moves the cursor to the
\r std::cout << "Hello\rWorld";
beginning of the line)
\b Backspace (moves cursor one character back) std::cout << "Helloo\b World!";
\v Vertical tab (rarely used) std::cout << "Hello\vWorld!";
\a Bell (audible beep sound) std::cout << "\a";
\f Form feed (page break, rarely used) std::cout << "Hello\fWorld!";

Examples Using Backslash Constants


#include <iostream>

int main() {
std::cout << "Hello\nWorld!" << std::endl; // 'World!' will be printed on the next line
std::cout << "Name\tAge\tLocation" << std::endl; //Horizontal tab \tInserts a tab space between
characters.
std::cout << "John\t25\tUSA" << std::endl; ////Horizontal tab \tInserts a tab space between
characters.
std::cout << "It\'s a beautiful day!" << std::endl; //backslash \To print a backslash (\), use two
backslashes (\\).
std::cout << "It\'s a beautiful day!" << std::endl; //Single Quote (\') Used when you need to print
a single quote within a string or character constant.
std::cout << "He said, \"Hello!\"" << std::endl; // Double Quote (\") Used to print double quotes
within a string.
std::cout << str << std::endl; // Only prints "Hello" because '\0' marks the end of the string
std::cout << "Hello\rWorld!" << std::endl; //Carriage Return (\r): "World!" will overwrite
"Hello"
std::cout << "Helloo\b World!" << std::endl; // One 'o' will be removed
return 0;
}
❖ features of C++ and its basic structure
C++ is a powerful, flexible, and high-performance programming language. It is widely used for
system/software development, game development, and applications requiring high performance.
1. Object-Oriented Programming (OOP)
C++ is an object-oriented language, which means it allows the use of objects and classes. Key
OOP features include:
• Encapsulation: Grouping data and functions into classes.
• Inheritance: Creating new classes from existing ones, promoting code reuse.
• Polymorphism: Using the same function or operator for different data types.
• Abstraction: Hiding complex implementation details and exposing only the necessary parts.
2. Rich Library Support
C++ provides a rich standard library, including functions for:
• Input/Output (I/O)

ASST. PROF ASMAA SHAIKH 9


• String manipulation
• Math functions
• Algorithms (sorting, searching)
• Containers (arrays, lists, vectors, stacks, etc.)
3. High Performance and Efficiency
C++ is a compiled language, which means it is directly translated into machine code. This results
in faster execution compared to interpreted languages. C++ is also known for its manual memory
management (via pointers, new, and delete), which allows greater control over memory allocation
and deallocation.
4. Supports Multiple Paradigms
C++ supports both:
• Procedural Programming: Writing functions and procedures.
• Object-Oriented Programming (OOP): Using classes and objects.
• Generic Programming: With the use of templates, allowing code to be written in a generic
and reusable way.
5. Low-Level Manipulation
C++ allows low-level manipulation of memory, pointers, and hardware, which is useful for system
programming, embedded systems, and game engines.
Basic Structure of a C++ Program
#include <iostream> // Preprocessor directive to include the iostream library

// Function declaration
void greet();

int main() {
// Variable declaration
int number = 5;

// Output statement
std::cout << "The number is: " << number << std::endl;

// Function call
greet();

return 0; // Exit status


}

// Function definition
void greet() {
std::cout << "Hello from the greet function!" << std::endl;
}
Summary of Basic C++ Program Structure
1. Preprocessor Directives: #include for libraries.
2. Main Function: int main() — program entry point.
3. Statements and Expressions: Perform actions in the program.

ASST. PROF ASMAA SHAIKH 10


4. Comments: Explain the code for readability.
5. Variables and Data Types: Store data in the program.
6. Functions: Modularize code and improve reusability.

❖ simple C++ program without class


Practical:-
#include <iostream> // Include the iostream library for input/output

int main() {
// Declare two variables to store user input
double num1, num2, sum;

// Ask the user for two numbers


std::cout << "Enter the first number: ";
std::cin >> num1; // Take input for num1

std::cout << "Enter the second number: ";


std::cin >> num2; // Take input for num2

// Calculate the sum of the two numbers


sum = num1 + num2;

// Output the result


std::cout << "The sum of " << num1 << " and " << num2 << " is: " << sum << std::endl;

return 0; // Return 0 to indicate that the program ended successfully


}
Explanation:
1. #include <iostream>: This includes the iostream library, which is used for input and output
operations (like reading from the user and printing to the console).
2. int main(): The main function is the entry point of the program.
3. double num1, num2, sum;: Declare three variables to store the two numbers entered by the
user and their sum.
4. std::cout: Used to print messages to the console.
5. std::cin: Used to get input from the user.
6. sum = num1 + num2;: Adds the two numbers and stores the result in the sum variable.
7. return 0;: This indicates that the program ran successfully.

ASST. PROF ASMAA SHAIKH 11

You might also like