{"id":25326372,"url":"https://github.com/adarshgupta0040/oops-in-cpp","last_synced_at":"2025-04-08T00:42:35.239Z","repository":{"id":277365124,"uuid":"932198956","full_name":"adarshgupta0040/OOPS-in-CPP","owner":"adarshgupta0040","description":"This repository contains Object-Oriented Programming (OOP) concepts in C++.","archived":false,"fork":false,"pushed_at":"2025-02-13T14:36:44.000Z","size":4697,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-08T00:42:15.836Z","etag":null,"topics":["coding","cpp17","interview-questions","oops","oops-concept","oops-concepts","oops-in-cpp","oops-in-python","oops-interview-ques"],"latest_commit_sha":null,"homepage":"https://cplusplus.com/doc/tutorial","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/adarshgupta0040.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-02-13T14:31:45.000Z","updated_at":"2025-02-13T14:40:23.000Z","dependencies_parsed_at":"2025-02-13T15:44:14.898Z","dependency_job_id":null,"html_url":"https://github.com/adarshgupta0040/OOPS-in-CPP","commit_stats":null,"previous_names":["adarshgupta0040/oops-in-cpp"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adarshgupta0040%2FOOPS-in-CPP","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adarshgupta0040%2FOOPS-in-CPP/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adarshgupta0040%2FOOPS-in-CPP/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adarshgupta0040%2FOOPS-in-CPP/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/adarshgupta0040","download_url":"https://codeload.github.com/adarshgupta0040/OOPS-in-CPP/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247755569,"owners_count":20990622,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["coding","cpp17","interview-questions","oops","oops-concept","oops-concepts","oops-in-cpp","oops-in-python","oops-interview-ques"],"created_at":"2025-02-14T01:23:24.251Z","updated_at":"2025-04-08T00:42:35.217Z","avatar_url":"https://github.com/adarshgupta0040.png","language":null,"readme":"# OOPS-in-C-\n\n# 🚀 Object-Oriented Programming (OOP) in C++\n\nThis repository contains examples and explanations of **Object-Oriented Programming (OOP) concepts in C++**.\n\n## 📌 Topics Covered\n- Object Creation (Stack vs. Heap)\n- Constructors \u0026 Destructors\n- Encapsulation \u0026 Abstraction\n- Inheritance (Types \u0026 Examples)\n- Diamond Problem \u0026 Virtual Inheritance\n- Polymorphism (Compile-time \u0026 Run-time)\n- Function \u0026 Operator Overloading\n- Method Overriding\n- Friend Functions\n- Use of `const` in C++\n\n---\n\n## 📌 1. Object Creation: Stack vs Heap\nC++ allows object creation in two ways:\n\n| Feature        | Stack Allocation | Heap Allocation (`new`) |\n|---------------|----------------|--------------------|\n| **Memory Location** | Stack         | Heap             |\n| **Lifetime**      | Automatic (destroyed when out of scope) | Manual (must use `delete`) |\n| **Performance**   | Faster        | Slightly slower   |\n| **Syntax**       | `ClassName obj;` | `ClassName* obj = new ClassName();` |\n\n📌 **Use Stack Allocation** for temporary objects.  \n📌 **Use Heap Allocation** when objects need to persist across multiple function calls.\n\n---\n\n## 📌 2. Constructors \u0026 Destructors\n**Constructor:** Special function that initializes an object.  \n**Destructor:** Cleans up when an object goes out of scope.\n\n```cpp\nclass Example {\npublic:\n    Example() { cout \u003c\u003c \"Constructor Called\\n\"; }\n    ~Example() { cout \u003c\u003c \"Destructor Called\\n\"; }\n};\n📌 3. Inheritance in C++\nC++ supports different types of inheritance:\n\nType\tDescription\nSingle\tOne class inherits from another.\nMultiple\tA class inherits from multiple base classes.\nMultilevel\tGrandparent → Parent → Child.\nHierarchical\tMultiple classes inherit from a single base class.\nHybrid\tCombination of two or more types.\n```\n📌 Example: Single Inheritance\n\nclass Animal {\npublic:\n    void eat() { cout \u003c\u003c \"Eating...\\n\"; }\n};\n\nclass Dog : public Animal {\npublic:\n    void bark() { cout \u003c\u003c \"Barking...\\n\"; }\n};\n\nint main() {\n    Dog d;\n    d.eat();  // Inherited\n    d.bark(); // Own method\n}\n```\n```\n📌 4. Diamond Problem \u0026 Virtual Inheritance\nWhen a class inherits from two classes that share a common base class, ambiguity arises.\nSolution: Use virtual inheritance.\n\nclass A { public: void show() { cout \u003c\u003c \"Class A\\n\"; } };\nclass B : virtual public A {};\nclass C : virtual public A {};\nclass D : public B, public C {};\n\nint main() {\n    D obj;\n    obj.show();  // No ambiguity\n}\n```\n```\n📌 5. Polymorphism\nTypes:\n\nCompile-time Polymorphism (Function \u0026 Operator Overloading)\nRun-time Polymorphism (Method Overriding using Virtual Functions)\n\n📌 Function Overloading Example\n\n\nclass Math {\npublic:\n    int add(int a, int b) { return a + b; }\n    double add(double a, double b) { return a + b; }\n};\n📌 Method Overriding Example (Using Virtual Functions)\n\nclass Parent {\npublic:\n    virtual void show() { cout \u003c\u003c \"Parent class\\n\"; }\n};\n\nclass Child : public Parent {\npublic:\n    void show() override { cout \u003c\u003c \"Child class\\n\"; }\n};\n\nint main() {\n    Parent* p = new Child();\n    p-\u003eshow();  // Calls Child's show()\n}\n```\n```\n📌 6. Operator Overloading (Adding Complex Numbers)\nOperator overloading allows user-defined types to use operators like +.\n\ncpp\nCopy\nEdit\nclass Complex {\npublic:\n    int real, imag;\n    Complex(int r, int i) : real(r), imag(i) {}\n\n    Complex operator+(const Complex\u0026 c) {\n        return Complex(real + c.real, imag + c.imag);\n    }\n\n    void display() { cout \u003c\u003c real \u003c\u003c \" + \" \u003c\u003c imag \u003c\u003c \"i\\n\"; }\n};\n\nint main() {\n    Complex c1(2, 3), c2(4, 5);\n    Complex c3 = c1 + c2;\n    c3.display();  // Output: 6 + 8i\n}\n```\n```\n📌 7. Friend Function (Accessing Private Members)\nA friend function is not a class member but can access private data.\n\ncpp\nCopy\nEdit\nclass Sample {\nprivate:\n    int num;\npublic:\n    Sample(int n) : num(n) {}\n\n    friend void display(const Sample\u0026 s);\n};\n\nvoid display(const Sample\u0026 s) {\n    cout \u003c\u003c \"The number is: \" \u003c\u003c s.num \u003c\u003c endl;\n}\n```\n```\n📌 8. Use of const in C++\nconst prevents modification of variables or methods.\n\ncpp\nCopy\nEdit\nclass Demo {\nprivate:\n    int data;\npublic:\n    Demo(int d) : data(d) {}\n\n    void show() const {  // Const function\n        cout \u003c\u003c \"Data: \" \u003c\u003c data \u003c\u003c endl;\n    }\n};\n```\n📌 Contributions 🤝\nFeel free to fork this repository and contribute with improvements, examples, and explanations!\n\n📌 License 📝\nThis project is open-source and available under the MIT License.\n\nLet me know if you need **changes, additions, or formatting improvements**! 🚀\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadarshgupta0040%2Foops-in-cpp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fadarshgupta0040%2Foops-in-cpp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadarshgupta0040%2Foops-in-cpp/lists"}