OOP is a programming paradigm based on the concept of "objects," which contain data (attributes) and methods (functions) that operate on the data.
Core Principle
- Encapsulation: Bundling data and methods within a class and restricting direct access to some of the object’s components.
#include <iostream>
using namespace std;
class Student {
private:
int marks; // Private data
public:
void setMarks(int m) { // Public method to set marks
if (m >= 0 && m <= 100) {
marks = m;
} else {
cout << "Invalid marks!" << endl;
}
}
void getMarks() { // Public method to get marks
cout << "Marks: " << marks << endl;
}
};
int main() {
Student s;
s.setMarks(85); // Valid
s.getMarks();
s.setMarks(150); // Invalid, won't set
return 0;
}
- Abstraction: Hiding complex implementation details and showing only essential features of an object.
#include <iostream>
using namespace std;
class Car {
public:
void startCar() { // We just start the car, no need to know the complex details!
cout << "Car started!" << endl;
}
};
int main() {
Car myCar;
myCar.startCar(); // We don’t see how the engine works — just that it starts!
return 0;
}
- Inheritance: Allowing one class (child) to inherit properties and behavior from another class (parent).
- Polymorphism: Enabling objects of different classes to be treated as objects of a common super class; methods can behave differently based on the object’s class.
#include <iostream>
using namespace std;
class Shape {
public:
virtual void draw() { // Virtual function
cout << "Drawing a shape." << endl;
}
};
class Circle : public Shape {
public:
void draw() override { // Same function, different behavior
cout << "Drawing a circle." << endl;
}
};
int main() {
Shape* shape; // Base class pointer
Circle circle;
shape = &circle;
shape->draw(); // Calls Circle's draw method
return 0;
}
Key Concept of OOP
- Class: Blueprint for creating objects; defines properties and methods.
- Object: Instance of a class; a real-world entity with state and behavior.
- Method: A function defined inside a class that describes the behaviors of an object.
- Attribute: Variables that store the state or properties of an object.
Advantage of OOP
- Code re usability through inheritance.
- Improved security with encapsulation.
- Easier maintenance and scalability.
- Clear modular structure makes it easier to troubleshoot and debug.
Introduction Of C++
Key Feature of C++
- Object-Oriented: Supports classes, objects, inheritance, polymorphism, encapsulation, and abstraction.
- Multi-Paradigm: Supports procedural, object-oriented, and generic programming.
- Fast and Efficient: Compiled language, making it faster than many other high-level languages.
- Platform Independent: C++ code can run on different operating systems with proper compilers.
- Rich Standard Library: Offers a wide range of functions and classes for data structures, algorithms, and more.
Structure of C++
Explanation
- #include <iostream>: Library for input and output functions like cin and cout.
- using namespace std;: Allows using standard functions without prefixing them with std::.
- int main(): Entry point of every C++ program.
- cout: Prints output to the console.
- endl: Ends the line and moves the cursor to the next line.
- return 0;: Signals that the program ended successfully.
Cascading of IO Operator
Cascading of Output Operator
How it Works
- cout << "Hello, " sends the string to the console and returns cout.
- The next << continues on the same cout stream and adds more output.
- endl ends the line and flushes the output.
Cascading of Input Operator
How it Works
- cin >> a takes the first number and returns cin.
- >> b and >> c continue taking input from the same cin stream.
Manipulators
- endl – Ends the current line and flushes the output buffer.
- setw(n) – Sets the width of the next output. (Requires <iomanip>)
- setfill(char) – Fills empty spaces with a specific character. (Requires <iomanip>)
- left and right – Align output to the left or right. (Requires <iomanip>)
- fixed and setprecision(n) – Sets the number of decimal places for floating-point numbers. (Requires <iomanip>)
Why Use Manipulators?
- Better formatting: Clean and aligned output.
- Control over numbers: Set precision, scientific notation, etc.
- Flexible and powerful: Simplifies complex formatting.

0 Comments had been done yet:
Post a Comment