Object-Oriented Programming (OOP) in C++
Introduction to OOP
- OOP is a programming paradigm based on the concept of "objects".
- Objects can contain data, in the form of fields (attributes) and code, in the form of procedures (methods).
Key Concepts in OOP
- Attributes and Methods
- Attributes (Data members): Variables that hold data specific to a class or object.
- Methods (Member functions): Functions that define the behavior of objects.
Basics of C++
- C++ is an object-oriented programming language that supports the creation and manipulation of objects.
- Classes and objects are fundamental to C++.
Classes and Objects
- Class: A blueprint for creating objects. Defines a datatype by bundling data and methods that work on the data.
- Object: An instance of a class.
- Example:
class Rect { int length; int width; void printArea(); };
Access Specifiers
- Public: Members are accessible from outside the class.
- Private: Members cannot be accessed from outside the class.
- Protected: Members are accessible within the class and by derived class.
Working with Classes in C++
- Declaration and Definition:
- Declaration:
class ClassName { //members };
- Definition: This is where the methods' functionality is implemented.
Constructors and Destructors
- Constructor: A special method called when an object is created.
- Destructor: A special method called when an object is destroyed.
Inline Functions
- Defined inside a class and intended for small, frequently used functions to increase performance.
Getters and Setters
- Methods used to retrieve and update private data members.
- Example:
int getLength() { return length; } void setLength(int myLength) { length = myLength; }
Struct vs Class
- Struct: Default access specifier is public.
- Class: Default access specifier is private.
Header and Source Files
- Header File (.h): Contains declarations of functions and variables.
- Source File (.cpp): Contains the implementation of functions.
Preprocessor Directives
- Used for file inclusion (
#include), macro definitions (#define), and conditional compilations (#ifdef).
Example Programs
- Rational Numbers Program:
- Demonstrates setting numerators and denominators, reducing fractions, and multiplying fractions.
- Utilizes private data members and public methods for operations.
Encapsulation and Abstraction
- Encapsulation: Bundling data with methods that operate on the data.
- Abstraction: Hiding complex reality while exposing only the necessary parts.
Practical Components
- Linking and Compilation: Understanding how the compiler converts source code to machine code.
- Preprocessor: Instructs the compiler to process certain directives before actual compilation.
Important Notes
- Use of
#ifdef and #endif to ensure header files are included only once.
#pragma once is an alternative to #ifndef / #define / #endif mechanism for preventing multiple inclusions.
This lecture provides an overview of OOP concepts in C++ with examples and explains the role of classes, objects, and various access specifiers, as well as covering essential programming constructs like constructors, destructors, and inline functions.