UNIT : 1
Syllabus
Introduction, Principles of OOP, Features of
OOP, Beginning with C++, Tokens,
Keywords, Identifiers, Expressions and
Control Structures, Operators, Functions in
C++
What is Object Oriented Programming?
● Object-Oriented Programming (OOP) is a programming paradigm that organizes software
design around objects rather than functions and logic.
● These objects are instances of classes, and they encapsulate both data (attributes) and the
methods (functions) that operate on that data.
● OOP is built around the idea of modeling real-world entities and their interactions, which
helps in organizing code in a way that is more understandable, reusable, and scalable.
2
Why Use Object-Oriented Programming?
● Modularity: OOP promotes the creation of small, self-contained objects that can be
developed and tested independently.
● Reusability: Through inheritance, you can reuse existing code, minimizing redundancy.
● Maintainability: Code is easier to maintain because of encapsulation and abstraction,
which allow changes to be made in one place without affecting the entire system.
● Scalability: OOP makes it easier to scale the software and introduce new features without
disrupting the overall system.
3
What is C++?
● C++ is a general-purpose, high-level programming language that was created by Bjarne
Stroustrup in 1979 at Bell Labs.
● It is an extension of the C programming language and includes object-oriented
programming (OOP) features.
● C++ is widely used for system/software development, game development, applications
requiring high performance, and in fields like finance, embedded systems, and more.
4
C++ history
● Bjarne Stroustrup is known as the founder of C++ language.
● C++ was developed by Bjarne Stroustrup starting in 1979 at Bell Labs in Murray Hill, New
Jersey, as an enhancement to the C language and originally named C with Classes but later
it was renamed C++ in 1983.
● It was develop for adding a feature of OOP (Object Oriented Programming) in C without
significantly changing the C component.
● C++ is a superset of C, and that virtually any legal C program is a legal C++ program.
5
Principles of OOPs
● C++ supports the principles of OOP, which includes:
● Encapsulation: Bundling of data and methods that operate on the data within a class.
● Inheritance: Creating new classes from existing ones, allowing for code reuse.
● Polymorphism: Allowing one function or method to operate in different ways depending
on the type of object.
● Abstraction: Hiding implementation details and showing only essential features to the
user.
6
Principles of OOPs
7
Advantages of C++
Disadvantages of C++
● OOPS (Object Oriented Programming
● Complexity
System)
● Lack of Support for Modern
● Portability
Programming Concepts
● Great Memory Management
● Pointer Problem
● Multi-Paradigm Language
● No Bin Box (NO! Garbage collection)
● Compatibility With C
● Security Vulnerabilities
● Scalability
● Array Limitations
● Standardization
8
9
Difference between C and C++
C C++
Origin Based on assembly language based on C language
Developer Dennis Ritchie in 1972 Bjarne Stroustrup in 1979
Programming paradigm Procedural language Object oriented programming (OOP)
Translator Compiler only Compiler only
Platform dependency Platform dependent Platform dependent
Code execution Direct Direct
Approach Top-down approach Bottom-up approach
File generation .exe files .exe files
Keywords 32 keywords 95 keywords
Security Data is less secured in C. In C++, you can use modifiers for class members to
make it inaccessible for outside users.
Functions for In C, scanf() and printf() are mainly used for C++ mainly uses stream cin and cout to perform input
input/output input/output. and output operations.
Support Not support feature of namespace, Operator Support feature of namespace, Operator overloading,
overloading, inheritance etc.. inheritance etc.. 10
Difference between C and C++
C C++
C is (mostly) a subset of C++. C++ is (mostly) a superset of C.
For the development of code, C supports procedural C++ is known as hybrid language because C++
programming. supports both procedural and object oriented
programming paradigms.
Data and functions are separated in C because it is a Data and functions are encapsulated together in
procedural programming language. form of an object in C++.
Built-in data types is supported in C. Built-in & user-defined data types is supported in C+
+.
Standard IO header is stdio.h. Standard IO header is iostream.h.
File extension is “.c” File extension is “.cpp” or “.c++” or “.cc” or “.cxx”
Difference between procedural programming and object-oriented programming
Procedural programming Object-oriented programming
Difference between procedural programming and object-oriented programming
Procedural programming Object-oriented programming
Program is divided into small parts called functions. Program is divided into small parts called objects.
Follows a top-down approach. Follows a bottom-up approach.
Not have proper way of hiding data so it is less secure. Provides data hiding so it is more secure.
Overloading is not possible. Overloading is possible
No concept of data hiding and inheritance. Have concept of data hiding and inheritance is used.
Function is more important than the data. Data is more important than function.
Based on the unreal world. Based on the real world.
Used for designing medium-sized programs. Used for designing large and complex programs.
Code reusability absent in procedural programming, Code reusability present in object-oriented
programming.
Examples: C, FORTRAN, Pascal, Basic, etc. Examples: C++, Java, Python, C#, etc.
Applications of OOP
▪ Real Time Systems Design
▪ Simulation and Modeling System
▪ Object Oriented Database
▪ Client-Server System
▪ Neural Networking and Parallel Programming
▪ Decision Support and Office Automation Systems
▪ CIM/CAD/CAM Systems
▪ AI and Expert Systems
14
OOPs (Object Oriented Programming System)
Object means a real word entity such as pen, chair, table etc. Object-Oriented Programming is
a methodology or paradigm to design a program using classes and objects. It simplifies the
software development and maintenance by providing some concepts:
1. Object
2. Class
3. Inheritance
4. Polymorphism
5. Abstraction
6. Encapsulation
15
Object
● Any entity that has state and behavior is known as an object. For example: chair, pen, table,
keyboard, bike etc. It can be physical and logical.
● In other words, object is an entity that has state and behavior. Here, state means data and
behavior means functionality.
● Object is a runtime entity, it is created at runtime.
● Object is an instance of a class. All the members of the class can be accessed through
object.
● Let's see an example to create object of student class using s1 as the reference variable.
Student s1; //creating an object of Student
16
Class
● In C++, class is a group of similar objects. It is a template from which objects are created.
It can have fields, methods, constructors etc.
● Collection of objects is called class. It is a logical entity.
● A class is a blueprint for the object.
● We can think of a class as a sketch (prototype) of a house.
● It contains all the details about the floors, doors, windows, etc - we build the house based
on these descriptions.
● The house is the object.
17
Relationship between object and class
● Objects and classes are used to wrap related functions and data in one place in C++.
● Suppose we need to store the length, breadth, and height of a rectangular room and
calculate its area and volume.
● To handle this task, we can create three variables, say, length, breadth, and height, along
with the functions calculate_area() and calculate_volume().
● However, in C++, rather than creating separate variables and functions, we can also wrap
the related data and functions in a single place (by creating objects).
● This programming paradigm is known as object-oriented programming.
18
Relationship between object and class
Create a Class
● A class is defined in C++ using the keyword class followed by the name of the class.
● The body of the class is defined inside curly brackets and terminated by a semicolon at the
end.
● Syntax:
};
● Here, we defined a class named Room.
● The variables length, breadth, and height declared
inside the class are known as data members.
● And the functions calculate_area() and
calculate_volume () are known as member functions
of a class. 19
Relationship between object and class
Create Object
Syntax
● When a class is defined, only the specification for the object is
defined; no memory or storage is allocated.
● To use the data and access functions defined in the class, we need to
We can create objects of
create objects. Room class (defined in the
above example) as follows:
● Here, two objects room1 and room2 of the Room class are created in
sample_function().
● Similarly, the objects room3 and room4 are created in main().
● As we can see, we can create objects of a class in any function of the
program.
● We can also create objects of a class within the class itself or in other
classes.
● Also, we can create as many objects as we want from a single class.
20
Relationship between object and class
21
First C++ Program
22
Simple understanding of C++ Program
● #include<iostream.h> includes the standard input output library functions. It provides cin and cout
methods for reading from input and writing to output respectively.
● #include <conio.h> includes the console input output library functions. The getch() function is
defined in conio.h file.
● void main() The main() function is the entry point of every program in C++ language. The void
keyword specifies that it returns no value.
● cout<< "Welcome to C++ Programming." is used to print the data "Welcome to C++
Programming." on the console.
● getch() The getch() function asks for a single character. Until you press any key, it blocks the
screen. 23
C++ Program Structure
1. Preprocessor Directives:
#include <iostream>
#include: This is a preprocessor directive used to include the contents of a header file in the
program. In this case, <iostream> is a standard C++ library that contains functions for input
and output operations like cin, cout, etc.
2. Namespace:
using namespace std;
This line tells the compiler to use the standard namespace (std), which contains common
functions and classes, such as cout and cin. By adding this line, you can avoid writing std::cout
or std::cin every time.
24
C++ Program Structure
3. The main Function:
int main()
// code
main(): Every C++ program must have a main function, which serves as the entry point for
program execution. The program starts executing from here.
int: The return type of main() is typically int, meaning the program will return an integer value to
the operating system. A return value of 0 usually indicates that the program ran successfully.
25
C++ Program Structure
4. Statements:
cout << "Hello, World!" << endl;
cout: Used to output data to the console (screen).
<<: The insertion operator used to send data to cout.
endl: Used to insert a newline character and flush the output buffer.
5. Return Statement:
return 0;
The return 0; statement marks the end of the main() function and indicates that the program has
executed successfully. Returning 0 is a common convention, though non-zero values can be
returned to indicate errors or other conditions.
26
Tokens in C++
27
Keywords in C++
A list Keywords in C++ Language which are also available in C language are given below.
28
Keywords in C++
A list of Keywords in C++ Language which are not available in C language are given below.
29
C++ Identifiers
● C++ identifiers in a program are used to refer to the name of the variables, functions, arrays,
or other user-defined data types created by the programmer.
● They are the basic requirement of any language. Every language has its own rules for naming
the identifiers.
● Rules for naming identifiers
1. Identifiers can be composed of letters, digits, and the underscore character.
2. It has no limit on name length.
3. It must begin with either a letter or an underscore.
4. It is case-sensitive.
5. We cannot use keywords as identifiers. 30
C++ Identifiers
Types of Identifiers
1. Constants
2. Variables
3. Functions
4. Labels
31
5. Defined data types
Types of Identifiers
Constant Identifiers
● Constant identifiers are names assigned to constants in programming, which
represent fixed values that cannot be changed during the execution of a program.
● Here is a simple example of a valid constant identifier −
const int MAX_SIZE = 100; // 'MAX_SIZE' is an identifier for a constant
32
Types of Identifiers
Variable Identifiers
● Variable identifiers are names given to variables in programming languages, which
are used to identify reference data stored in those variables.
● Here are a few examples of valid identifiers −
int age; // 'age' is an identifier for an integer variable
double salary; // 'salary' is an identifier for a double variable
char initial_alpha; // 'initial_alpha' is an identifier for a character variable
33
Types of Identifiers
Function Identifiers
● Function identifiers are names assigned to functions in programming, which allow developers to define
and call reusable blocks of code. Some of the valid function identifiers are as follows −
void calculateSum() { // 'calculateSum' is an identifier for a function
// function implementation
int getValue() { // 'getValue' is another function identifier
return 42;
34
Types of Identifiers
Class Identifiers
● Class identifiers are names assigned to classes in object-oriented programming
which is used to define new data types that encapsulate attributes and behaviors
associated with specific entities.
● Here is a simple example of a valid class identifier −
class Person {
// 'Person' is an identifier for a class
public:
int age;
string name;
}; 35
Difference between Keywords and identifiers
Keywords Identifiers
Keyword is a predefined word. The identifier is a user-defined word
It must be written in a lowercase letter. It can be written in both lowercase and
uppercase letters.
Its meaning is pre-defined Its meaning is not defined
It is a combination of alphabetical characters. It is a combination of alphanumeric characters.
It does not contain the underscore character. It can contain the underscore character.
36
C++ Variables
● In programming, a variable is a container (storage area) to hold data.
● To indicate the storage area, each variable should be given a unique name
(identifier). For example,
int age = 14;
• Here, age is a variable of the int data type, and we have assigned an integer value 14
to it.
37
C++ Constants
● In C++, we can create variables whose value cannot be changed. For that, we use
the const keyword. Here's an example:
const int LIGHT_SPEED = 299792458;
LIGHT_SPEED = 2500 // Error! LIGHT_SPEED is a constant.
● Here, we have used the keyword const to declare a constant named LIGHT_SPEED.
If we try to change the value of LIGHT_SPEED, we will get an error.
38
C++ Literals
● Literals are data used for representing fixed values. They can be used directly in the code. For
example: 1, 2.5, 'c' etc.
● Here, 1, 2.5 and 'c' are literals. Why? You cannot assign different values to these terms.
● 1. Integers:- An integer is a numeric literal(associated with numbers) without any fractional or
exponential part. There are three types of integer literals in C++ programming:
decimal (base 10)
octal (base 8)
hexadecimal (base 16)
39
C++ Literals
2. Floating-point Literals
• A floating-point literal is a numeric literal that has either a fractional form or an exponent form. For
example:
-2.0
0.0000234
-0.22E-5
3. Characters
• A character literal is created by enclosing a single character inside single quotation marks.
For example: 'a', 'm', 'F', '2', '}' etc.
40
C++ Literals
4. String Literals
• A string literal is a sequence of characters enclosed in double-quote marks. For example:
41
C++ Data Types
42
C++ Data Types
43
C++ Data Types
44
C++ Operators
1. Arithmetic Operators
2. Assignment Operators
3. Relational Operators
4. Logical Operators
5. Bitwise Operators
6. Other Operators
45
Arithmetic Operators
46
Assignment Operators
47
C++ Relational Operators
48
C++ Logical Operators
49
C++ Bitwise Operators
50
Other C++ Operators
51
C++ Comments
● Program comments are explanatory statements that you can include in the C++ code. These
comments help anyone reading the source code. All programming languages allow for some
form of comments.
● Types of C++ Comments
1. C++ Single-line Comments
• A single-line comment starts with //, extending to the end of the line. These comments can last
only till the end of the line, and the next line leads to a new comment.
2. C++ Multi-line Comments
• Multi-line comments start with /* and end with */. Any text in between these symbols is
52
treated as a comment only.
Control Structures
● if statement
● if…else statement
● if…else nested
● switch statement
● do-while statement: An exit controlled loop
● while Statement: An entry controlled loop
● for statement: An entry controlled loop
53
if statement
An if statement consists of a boolean expression followed by one or more statements.
54
if...else statement
55
C++ switch statement
A switch statement allows a variable to be tested for equality against a list of values.
Each value is called a case, and the variable being switched on is checked for each case.
56
C++ Loop Types
● do-while statement: An exit controlled loop
● while Statement: An entry controlled loop
● for statement: An entry controlled loop
57
do-while statement: An exit controlled loop
Unlike for and while loops, which test the loop condition at the top of the loop, the do...while loop
checks its condition at the bottom of the loop.
58
while Statement: An entry controlled loop
A while loop statement repeatedly executes a target statement as long as a given condition is true.
59
for statement: An entry controlled loop
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to
execute a specific number of times.
60
C++ break statement
When the break statement is encountered inside a loop, the loop is immediately terminated and program control
resumes at the next statement following the loop. It can be used to terminate a case in the switch statement.
61
C++ continue Statement
The continue statement works somewhat like the break statement. Instead of forcing termination,
however, continue forces the next iteration of the loop to take place, skipping any code in between.
62
C++ goto Statement
A goto statement provides an unconditional jump from the goto to a labeled statement in the same function.
63
C++ Functions
• A function is a set of statements that takes input, does some specific computation, and
produces output.
• The idea is to put some commonly or repeatedly done tasks together to make a
function so that instead of writing the same code again and again for different inputs,
we can call this function.
• In simple terms, a function is a block of code that runs only when it is called.
64
C++ Functions
Function Declaration
• A function declaration tells the compiler about the number of parameters, data types of
parameters, and returns type of function.
• Writing parameter names in the function declaration is optional but it is necessary to put
them in the definition.
• Below is an example of function declarations. (parameter names are not present in the below
declarations)
65
Parameter Passing to Functions
• The parameters passed to the function are called actual parameters.
• The parameters received by the function are called formal parameters.
There are two most popular ways to pass parameters:
1. Pass by Value: In this parameter passing method,
values of actual parameters are copied to the
function’s formal parameters. The actual and
formal parameters are stored in different memory
locations so any changes made in the functions are
not reflected in the actual parameters of the caller.
2. Pass by Reference: Both actual and formal
parameters refer to the same locations, so any
changes made inside the function are reflected in
the actual parameters of the caller.
66
Category of Function
67
Difference between call by value and call by reference in C++
call by value call by reference
A copy of the value is passed to the An address of value is passed to the
function function
Changes made inside the function are not Changes made inside the function are
reflected on other functions reflected
outside the function as well
Actual and formal arguments will be Actual and formal arguments will be
created at created at
different memory location same memory location.
68
Thank You
69