OOP Module 1 chapter 1
OOP Module 1 chapter 1
Object Oriented
Programming Using C++
Module 1 Chapter 1
Introduction
Book Genre Year Published
Student Name
Introduction
● A class is a blueprint for creating objects. It defines properties (attributes) and behaviors
(methods) that the objects created from it will have.
● An object is an instance of a class, embodying its properties and methods.
Example: If "Car" is a class, a specific car like a red sedan could be an object.
Encapsulation:
● This principle hides the internal details of an object and exposes only what’s necessary. It
ensures that an object's data is protected and accessed through well-defined interfaces.
Inheritance:
● A class can inherit properties and behaviors from another class, enabling code reuse and the
creation of a hierarchy. The "parent" class shares its attributes with "child" classes.
Example: A "Vehicle" class could be a parent, and "Car" and "Truck" could inherit from it.
Polymorphism:
● Objects can take on different forms depending on their context. A single function or
method can work differently based on the object calling it.
Example: A method move() might behave differently for a "Car" versus an "Airplane."
Abstraction:
● This focuses on exposing only essential features while hiding unnecessary details. It
simplifies complex systems.
Example: When driving a car, you use the steering wheel and pedals without worrying
about the engine mechanics.
Differentiate between procedure oriented programming and object oriented
programming
Application of OOP
Key Components of a C++ Program-
Preprocessor Directives:
● Lines starting with # are preprocessor directives, such as #include or #define.
● They instruct the compiler to include libraries or perform specific tasks before compilation.
● Example: #include <iostream> is used for input/output operations.
Global Declarations (Optional):
● Variables, constants, or functions declared outside all functions have global scope and can be
accessed throughout the program.
Function Prototypes (Optional):
● Function prototypes are declarations of functions before their actual implementation.
● This helps the compiler recognize the function if it's called before its definition.
Main Function (int main()):
● The entry point of every C++ program.
● Code execution starts from the main function.
● It usually ends with a return 0; statement, signaling successful execution to the operating system.
Function Definitions:
Comments:
It is part of the iostream library and works with the extraction operator (>>) to store user input into variables.
Example-
#include <iostream>
int main() {
int a, b;
cout << "You entered: " << a << " and " << b << std::endl;
return 0;
}
C++ Tokens
A token is the smallest element of a program that is meaningful to
the compiler. Tokens can be classified as follows:
1. Keywords
2. Identifiers
3. Constants
4. Strings
5. Special Symbols
6. Operators
Keywords-pre-defined or reserved words in a programming language
C++ Variables
1.bool-Stores either value true or false.
Declarations
int i, j, k;
char c, ch;
float f, salary;
double d;
Operators in C++
An operator is a symbol that operates on a value to perform specific mathematical or
logical computations.
int main()
{
int x = 10; // integer x
char y = 'a'; // character c
return 0;
}
Explicit Type Conversion:
This process is also called type casting and it is user-defined. Here the user can typecast the result to make
it of a particular data type
1.Converting by assignment:
This is done by explicitly defining the required type in front of the expression in parenthesis. This can be also
considered as forceful casting.
Syntax:
(type) expression
int main()
{
double x = 1.2;
// Explicit conversion from double to int
int sum = (int)x + 1;
cout << "Sum = " << sum;
return 0;
}