Object Oriented Programming

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

It majorly Contain four major principle of Abstraction, Encapsulation, inheritance and Polymorphism which is explained as below.
  • 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).
#include <iostream>
using namespace std;

// Parent class
class Animal {
public:
    void eat() {
        cout << "This animal can eat." << endl;
    }
};

// Child class
class Dog : public Animal {
public:
    void bark() {
        cout << "Dog is barking!" << endl;
    }
};

int main() {
    Dog myDog;
    myDog.eat();          // Inherited from Animal
    myDog.bark();         // Dog’s own method
    return 0;
}
  • 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++

C++ is a powerful, high-level, general-purpose programming language developed by Bjarne Stroustrup in 1979 as an extension of the C language. It combines the efficiency of C with the features of Object-Oriented Programming (OOP). C++ is widely used in system software, game development, application software, and competitive programming.

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++

#include <iostream>      // Header file for input and output

using namespace std;     // Standard namespace

int main() {             // Main function - program starts here
    cout << "Hello, World!" << endl;  // Output statement
    return 0;            // End of the program
}


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 input (>>) and output (<<) operators in C++ means using multiple I/O operations in a single statement. It allows chaining multiple cin or cout operations together, making the code cleaner and more efficient.

Cascading of Output Operator

#include <iostream>
using namespace std;

int main() {
    cout << "Hello, " << "this is " << "cascading!" << endl;
    return 0;
}

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

#include <iostream>
using namespace std;

int main() {
    int a, b, c;
    cout << "Enter three numbers: ";
    cin >> a >> b >> c;  // Cascading input
    cout << "You entered: " << a << ", " << b << ", " << c << endl;
    return 0;
}

How it Works

    • cin >> a takes the first number and returns cin.
    • >> b and >> c continue taking input from the same cin stream.

    Manipulators

    Manipulators in C++ are special functions used to format and control the input and output (I/O) of data. They make console output more readable and help in adjusting the display of numbers, text, and other data. Manipulators are used with the cin and cout streams.
    Common Manipulators:

    • endl – Ends the current line and flushes the output buffer.
    #include <iostream>
    using namespace std;

    int main() {
        cout << "Hello, World!" << endl;
        cout << "This is C++ manipulators." << endl;
        return 0;
    }


    • setw(n) – Sets the width of the next output. (Requires <iomanip>)
    #include <iostream>
    #include <iomanip>  // Required for setw
    using namespace std;

    int main() {
        cout << setw(10) << "Apple" << endl;
        cout << setw(10) << "Banana" << endl;
        cout << setw(10) << "Cherry" << endl;
        return 0;
    }

    • setfill(char) – Fills empty spaces with a specific character. (Requires <iomanip>)

    #include <iostream>
    #include <iomanip>
    using namespace std;

    int main() {
        cout << setfill('*') << setw(10) << "Hi" << endl;
        return 0;
    }

    • left and right – Align output to the left or right. (Requires <iomanip>)

    #include <iostream>
    #include <iomanip>
    using namespace std;

    int main() {
        cout << left << setw(10) << "Left" << endl;
        cout << right << setw(10) << "Right" << endl;
        return 0;
    }

    • fixed and setprecision(n) – Sets the number of decimal places for floating-point numbers. (Requires <iomanip>)

    #include <iostream>
    #include <iomanip>
    using namespace std;

    int main() {
        double pi = 3.1415926535;
        cout << fixed << setprecision(2) << "Pi: " << pi << endl;
        return 0;
    }

    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