Description
1.1 Procedure Oriented Programming (POP) vs. Object Oriented Programming (OOP)
1.2 Features of Object Oriented Programming
The key features of OOP are:
- Encapsulation: The bundling of data (attributes) and the methods (functions) that operate on that data into a single unit, an object. It also involves data hiding, restricting direct access to an object’s internal state.
- Abstraction: Hiding the complex implementation details and showing only the essential features of an object. For example, when you drive a car, you don’t need to know how the engine works, only how to use the steering wheel and pedals.
- Inheritance: A mechanism where a new class (the derived or child class) inherits properties and behaviors from an existing class (the base or parent class). This promotes code reuse and establishes a hierarchical relationship between classes.
- Polymorphism: The ability of an object to take on many forms. It allows a single interface to be used for a general class of actions. A simple example is a
+operator that can perform both addition on numbers and concatenation on strings. This is often achieved through function overloading and function overriding.
Examples of Object-Oriented Languages: Java, C++, Python, C#, Smalltalk, Ruby.
Applications of OOP:
- GUI-based applications (e.g., user interfaces for software).
- Real-time systems.
- Databases.
- Operating systems.
- Simulation and modeling systems.
- Game development.
1.3 Data Types, Variables, and Type Compatibility in C++
- Data Types: Classify the type of data that a variable can hold. C++ has built-in types like
int(for integers),floatanddouble(for floating-point numbers),char(for characters), andbool(for boolean values). It also supports user-defined types like classes and structures. - Type Compatibility: Refers to whether one data type can be assigned to a variable of another data type without a compilation error. This is often handled by type casting or type conversion.
- Declaration of Variable: The process of specifying a variable’s name and its data type. Example:
int age;. This allocates memory for the variable. - Dynamic Initialization of Variable: The initialization of a variable at runtime, where the initial value is determined by a user or a computed expression. Example:
float area = length * width;. - Reference Variable: An alias or another name for an already existing variable. It’s declared using the ampersand
&. Any changes made to the reference variable will affect the original variable. Example:int x = 10; int &ref_x = x;. - Type Casting: The process of converting one data type into another.
- Implicit Type Casting (automatic): Done by the compiler, usually for type promotion (e.g.,
inttofloat). - Explicit Type Casting (manual): Done by the programmer using a cast operator. Example:
(int)3.14;.
- Implicit Type Casting (automatic): Done by the compiler, usually for type promotion (e.g.,
1.4 Special Operators in C++
- Scope Resolution Operator (
::): Used to define a member function outside its class, access a static member of a class, or access a global variable when a local variable with the same name exists. - Memory Management Operators:
new: Used to dynamically allocate memory at runtime. For example,int *ptr = new int;allocates memory for an integer and returns a pointer to it.delete: Used to deallocate memory previously allocated withnew. It’s crucial for preventing memory leaks. For example,delete ptr;.
- Manipulators: Special functions used with input/output streams to format the output.
endl: Inserts a newline character and flushes the output buffer.setw(n): Sets the field width tonfor the next output operation.
1.5 Structure of a C++ Program & Basic I/O
A basic C++ program includes:
- Header Files: Provide declarations for functions and classes (e.g.,
<iostream>for input/output). main()function: The entry point of every C++ program.- Statements and expressions: The instructions that make up the program logic.
Example of a Simple C++ Program:
C++
#include <iostream>
int main() {
std::cout << "Hello, World!";
return 0;
}
Basic Input/Output Operators & Functions:
- Output Operator (
<<): Used withstd::cout(console output stream) to send data to the console. - Input Operator (
>>): Used withstd::cin(console input stream) to read data from the keyboard.
1.6 Class & Object
- Class: A blueprint or template for creating objects. It defines a new data type with its own attributes (data members) and behaviors (member functions). It’s a logical construct; no memory is allocated when a class is defined.
- Object: An instance of a class. It’s a real-world entity created from the class blueprint. When an object is created, memory is allocated for it.
Specifying a Class: A class is defined using the class keyword, followed by the class name. The members are enclosed in curly braces.
Access Specifiers: Control the visibility and accessibility of class members.
public: Members are accessible from outside the class.private: Members are only accessible from within the class itself. This is a key part of encapsulation and data hiding.protected: Members are accessible within the class and by derived classes.
Defining Member Functions:
- Inside Class Definition: Functions can be defined directly inside the class declaration. These are automatically considered
inlinefunctions. - Outside Class Definition: Functions are defined outside the class using the scope resolution operator (
::) to link the function definition to the class.
Creating Objects: An object is created by using the class name followed by the object name. Example: Car my_car;.
Memory Allocation for Objects: Memory is allocated for an object only when it’s created. The size of the memory allocated depends on the data members defined within the class. Member functions are stored in a common memory area and are not duplicated for each object. The object itself only holds the memory for its data members.
Keywords of this Topic: class, public, private, protected, this (a pointer to the current object), new, delete.





Reviews
There are no reviews yet.