Class Name
Object Oriented
Programming Using C++
Module 1 Chapter 1
Introduction
Book Genre Year Published
Student Name
Introduction
Object-Oriented Programming (OOP) is a programming paradigm centered around the
concept of "objects," which are instances of classes
Key Concepts of OOP
Classes and Objects:
● 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:
● Functions provide reusable blocks of code.
● User-defined functions must be defined either before or after the main function (with
prototypes declared if after).
Comments:
● Single-line comments: // This is a comment.
● Multi-line comments: /* This is a multi-line comment. */
cout
cout object in C++ is used for outputting data to the standard output device, typically the console (screen).
It is part of the iostream library and works with the insertion operator (<<) to display text, variables, or
results.
Example-
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}
Cin
cin object in C++ is used for taking input from the user via the keyboard.
It is part of the iostream library and works with the extraction operator (>>) to store user input into variables.
Example-
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b; // Input two numbers
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.
2. char-Typically a single octet (one byte).
3.int-The most natural size of integer for the machine.
4.float-A single-precision floating point value.
5. double-A double-precision floating point value.
6.void-Represents the absence of type.
Variable definition in c++
A variable definition tells the compiler where and how much storage to create for
the variable.
Syntax- type variable_list;
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.
Operators in C++ can be classified into 6 types:
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
6. Ternary or Conditional Operator
Constants in C++
constants are given to such variables or values in C/C++ programming language
which cannot be modified once they are defined. They are fixed values in a
program. There can be any types of constants like integer, float, octal,
hexadecimal, character constants, etc.
Type Conversion in C++
A type cast is basically a conversion from one type to another.
There are two types of type conversion:
1. Implicit Type Conversion Also known as ‘automatic type conversion’
int main()
{
int x = 10; // integer x
char y = 'a'; // character c
// y implicitly converted to int. ASCII
// value of 'a' is 97
x = x + y;
// x is implicitly converted to float
float z = x + 1.0;
cout << "x = " << x << endl
<< "y = " << y << endl
<< "z = " << z << endl;
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;
}