Lecture Notes: C++ Programming Basics and
Introduction to OOP
1. Basic Data Types
Data types specify the type of data a variable can hold. Common basic data types in C++ include:
Data Type Description Example
Int Integer numbers int age = 25;
float Single-precision floating-point float weight = 65.5;
double Double-precision floating-point double pi = 3.14159;
char Single character char grade = 'A';
bool Boolean (true/false) bool isPassed = true;
2. Arrays
An array is a collection of variables of the same type, stored in contiguous memory.
Syntax:
int numbers[5]; // declaration
numbers[0] = 10; // accessing elements
Initialization:
int marks[3] = {80, 85, 90};
3. Functions
Functions are reusable blocks of code that perform specific tasks.
Syntax:
int add(int a, int b) {
return a + b;
}
Calling a Function:
int result = add(5, 10); // Output: 15
4. Passing Values to Functions
By Value (Copy of Data):
void display(int x)
{
x = x + 5;
cout << x; // changes not reflected outside
}
By Reference (Affect original):
void display(int &x)
{
x = x + 5; // changes reflected outside
}
5. Passing Arrays to Functions
Arrays are passed by reference by default.
void printArray(int arr[], int size)
{
for(int i = 0; i < size; i++)
cout << arr[i] << " ";
}
int nums[] = {1, 2, 3};
printArray(nums, 3);
6. Introduction to OOP
Object-Oriented Programming (OOP) is a paradigm based on “objects,” which contain data
(attributes) and functions (methods).
Why OOP?
Models real-world entities
Promotes code reuse
Easy to maintain and scale
Core Concepts:
1. Encapsulation
Definition:
Encapsulation is the process of wrapping data and functions that operate on the data into a single unit
(i.e., a class). It restricts direct access to some of the object's components, which is a means of preventing
unintended interference and misuse.
2. Abstraction
Definition:
Abstraction is the concept of hiding complex implementation details and showing only the necessary
features to the user.
3. Inheritance
Definition:
Inheritance allows a class (child/derived) to inherit properties and behaviors (fields and methods) from
another class (parent/base).
4. Polymorphism
Definition:
Polymorphism means "many forms". It allows one interface to be used for different underlying forms
(data types).
7. Structured vs Object-Oriented Programming
Structured programming is a procedural programming paradigm. It emphasizes a top-down approach,
breaking the program into smaller, reusable functions or procedures.
OOP is based on the concept of "objects", which combine data (attributes) and functions (methods)
into a single unit. It uses a bottom-up approach to design and development.
Structured Vs Object Oriented
Structured Programming Object-Oriented Programming
Aspect
Focus Functions & Procedures Objects & Classes
Data Separate from functions Encapsulated within objects
Reusability Limited High (via inheritance & polymorphism)
Example C language C++, Java, Python
8. Structure in C++
Structures are user-defined data types that group related variables.
Declaration:
struct Student {
string name;
int rollNo;
float marks;
};
Defining a Structure Variable:
Student s1;
Accessing Members:
[Link] = "Ali";
[Link] = 101;
[Link] = 89.5;
Initialization:
Student s2 = {"Sara", 102, 91.5};