├── .github ├── CODEOWNERS ├── ISSUE_TEMPLATE │ ├── bug_report.yml │ ├── config.yml │ ├── feature_request.yml │ └── other.yml ├── PULL_REQUEST_TEMPLATE.md └── stale.yml ├── C++ ├── .gitkeep ├── Abstract │ └── Abstract.cpp ├── AccessModifier │ ├── PrivateAccessModifier.cpp │ ├── ProtectedAccessModifier.cpp │ └── PublicAccessModifier.cpp ├── Classes │ └── Class.cpp ├── ConstructorOverloading │ └── ConstructorOverloading.cpp ├── Constructors │ ├── Constructor.cpp │ ├── CopyConstructor.cpp │ ├── DefaultConstructor.cpp │ └── ParameterizedConstructor.cpp ├── Destructors │ ├── Destructor.cpp │ └── VirtualDestructor │ │ └── VirtualDestructor.cpp ├── FriendFunctionAndClass │ ├── FriendClass │ │ └── FriendClass.cpp │ └── FriendFun │ │ ├── FriendFunMultiple.cpp │ │ ├── GlobalFriendFun.cpp │ │ └── MemberFriendFun.cpp ├── Inheritence │ ├── ConstructorInheritence │ │ └── ConstructorInheritence.cpp │ ├── Examples │ │ └── InheritenceExample1.cpp │ ├── MultilevelInheritence │ │ └── MultilevelInheritence.cpp │ ├── MultipleInheritence │ │ └── MultipleInheritence.cpp │ └── SingleInheritence │ │ └── SingleInheritence.cpp ├── Object │ └── Object.cpp ├── Polymorphism │ ├── CompiletimePolymorphism │ │ ├── CompiletimePolymorphismFunctionOverloading.cpp │ │ └── CompiletimePolymorphismOperatorOverloading.cpp │ └── RuntimePolymorphism │ │ ├── RuntimePolymorphismFunctionOverriding.cpp │ │ └── RuntimePolymorphismWithDataMember.cpp └── StaticMember │ ├── DataMember │ └── StaticDataMember.cpp │ └── MemberFunction │ └── StaticMemberFun.cpp ├── CONTRIBUTING.md ├── Golang └── .gitkeep ├── Java ├── Abstraction │ ├── Abstraction.java │ └── AbstractionInterface.java ├── Classes │ ├── Classes.java │ └── OOPs.java ├── Encapsulation │ └── Encapsulation.java ├── Object │ └── Object.java └── Polymorphism │ ├── CompileTime │ └── CompileTimePolymorphism.java │ └── RunTime │ └── RunTimePolymorphism.java ├── JavaScript ├── Classes │ └── Class.js ├── Constructor │ └── Constructor.js └── Object │ ├── Object.js │ └── ObjectEx2.js ├── Python ├── .gitkeep └── Class │ └── Class.py ├── README.md └── TypeScript ├── .gitkeep └── Classes └── Shape.ts /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @dev-madhurendra @Suryac72 -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.yml: -------------------------------------------------------------------------------- 1 | name: Bug report 2 | description: 'Create a report to help us improve' 3 | title: '[BUG]: ' 4 | labels: ['bug'] 5 | body: 6 | - type: markdown 7 | attributes: 8 | value: '### Before you open an issue, please verify if a similar one already exists or has been closed before. More details about the process of contributing can be found in [CONTRIBUTING.md](https://github.com/dev-madhurendra/OOPs/blob/main/CONTRIBUTING.md).' 9 | - type: textarea 10 | id: description 11 | attributes: 12 | label: Description 13 | description: Explain what the problem is. 14 | validations: 15 | required: true 16 | - type: textarea 17 | id: expectedbhv 18 | attributes: 19 | label: Expected Behavior 20 | description: Describe what was the expected behavior. 21 | validations: 22 | required: true 23 | - type: textarea 24 | id: actualbhv 25 | attributes: 26 | label: Actual Behavior 27 | description: Describe what actually happens. 28 | validations: 29 | required: true 30 | - type: textarea 31 | id: steps 32 | attributes: 33 | label: Steps to reproduce (if applicable) 34 | description: List steps to reproduce the behavior. 35 | placeholder: | 36 | 1. 37 | 2. 38 | 3. 39 | 4. 40 | validations: 41 | required: false 42 | - type: textarea 43 | id: extra_information 44 | attributes: 45 | label: Additional information 46 | description: Is there anything else we should know about this bug report? 47 | validations: 48 | required: false 49 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/config.yml: -------------------------------------------------------------------------------- 1 | blank_issues_enabled: false 2 | contact_links: 3 | - name: Discord community 4 | url: https://discord.gg/bUHuNRq7 5 | about: Have any questions or found any bugs? Contact us via our Discord community. -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.yml: -------------------------------------------------------------------------------- 1 | name: Feature request 2 | description: 'Suggest features, propose improvements, discuss new ideas' 3 | title: '[FEATURE]: ' 4 | labels: ['enhancement'] 5 | body: 6 | - type: markdown 7 | attributes: 8 | value: | 9 | ## This issue template is not for requesting new algorithms. For new algorithms, PRs should be opened directly. 10 | ## Make sure your issue isn't a duplicate and you follow our [contributing guidelines](https://github.com/dev-madhurendra/OOPs/blob/main/CONTRIBUTING.md) 11 | - type: textarea 12 | id: description 13 | attributes: 14 | label: Motivation 15 | description: Describe what is the motivation behind this feature. 16 | validations: 17 | required: true 18 | - type: textarea 19 | id: examples 20 | attributes: 21 | label: Examples 22 | description: If possible, provide examples of how this feature can be used. 23 | validations: 24 | required: false 25 | - type: textarea 26 | id: workarounds 27 | attributes: 28 | label: Possible workarounds 29 | description: If possible, describes possible workarounds to this feature. 30 | validations: 31 | required: false 32 | - type: textarea 33 | id: extra_information 34 | attributes: 35 | label: Additional information 36 | description: Is there anything else we should know about this feature request? 37 | validations: 38 | required: false 39 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/other.yml: -------------------------------------------------------------------------------- 1 | name: Other issue 2 | description: Use this for any other issues. Do NOT create blank issues 3 | title: "[OTHER]: " 4 | labels: ["awaiting triage"] 5 | body: 6 | - type: textarea 7 | id: description 8 | attributes: 9 | label: What would you like to share? 10 | description: Provide a clear and concise explanation of your issue. 11 | validations: 12 | required: true 13 | - type: textarea 14 | id: extrainfo 15 | attributes: 16 | label: Additional information 17 | description: Is there anything else we should know about this issue? 18 | validations: 19 | required: false -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | ### Describe your change 2 | 3 | - [ ] Add a concept ? 4 | - [ ] Fix a bug or typo in an existing concept ? 5 | - [ ] Documentation change? 6 | 7 | ### Checklist 8 | 9 | - [ ] I have read [CONTRIBUTING.md](https://github.com/dev-madhurendra/OOPs/blob/main/CONTRIBUTING.md). 10 | - [ ] This pull request is all my own work -- I have not plagiarized. 11 | - [ ] I know that pull requests will not be merged if they fail the automated tests. 12 | - [ ] This PR only changes one file. To ease review, please open separate PRs for separate concepts. 13 | - [ ] All new folders are placed in an existing directory. 14 | - [ ] All new files are placed inside an existing directory. 15 | - [ ] All folder names should use the UpperCamelCase (PascalCase) style. There should be no spaces in filenames. 16 | **Example:**`Golang` is allowed but `golang` is not 17 | - [ ] All filenames should use the UpperCamelCase (PascalCase) style. There should be no spaces in filenames. 18 | **Example:**`Class.cpp` is allowed but `classe.cpp` is not 19 | - [ ] All new concepts have a URL in their comments that points to Wikipedia or another similar explanation. 20 | - [ ] If this pull request resolves one or more open issues then the commit message contains `Fixes: #{$ISSUE_NO}` 21 | -------------------------------------------------------------------------------- /.github/stale.yml: -------------------------------------------------------------------------------- 1 | # Number of days of inactivity before an issue becomes stale (2 weeks) 2 | daysUntilStale: 14 3 | # Number of days of inactivity before a stale issue is closed (a week) 4 | daysUntilClose: 7 5 | # Issues with these labels will never be considered stale 6 | exemptLabels: 7 | - bug 8 | - help wanted 9 | - OK to merge 10 | # Label to use when marking an issue as stale 11 | staleLabel: stale 12 | # Comment to post when marking an issue as stale. Set to `false` to disable 13 | markComment: > 14 | This issue has been automatically marked as stale because it has not had 15 | recent activity. It will be closed if no further activity occurs. Thank you 16 | for your contributions. 17 | # Comment to post when closing a stale issue. Set to `false` to disable 18 | closeComment: > 19 | Please reopen this issue once you commit the changes requested or 20 | make improvements on the code. Thank you for your contributions. -------------------------------------------------------------------------------- /C++/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dev-madhurendra/OOPs/97ca90e99766f4e022f2c14e297055c7e49ddd65/C++/.gitkeep -------------------------------------------------------------------------------- /C++/Abstract/Abstract.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* 5 | Polymorphism : 6 | 1.Base class pointer and derived class object 7 | 2.Virtual function 8 | 3.Abstract class 9 | 10 | 11 | when we create pointer of base class and object of derived class then 12 | base class object always initiated so we use virtual function to access the 13 | feature of derived class and if we want that base class object never 14 | initated then we use abstract class. 15 | 16 | 3.Abstract class : 17 | ->abstract class is used when we never want to initiate base class. 18 | 19 | -> if pure virtual function exist in any class then that class become 20 | abstract class. 21 | 22 | ->if we don't want to perform any activity of base class then we use 23 | pure virtual function. 24 | 25 | 26 | 27 | 28 | 29 | function overloading 30 | / 31 | Compile time - 32 | / \ 33 | / operator overloading 34 | Polymorphism - 35 | \ virtual function 36 | \ / 37 | runtime - 38 | \ 39 | function overriding 40 | 41 | 42 | 43 | 44 | */ 45 | 46 | class Shape { 47 | public: 48 | // Pure virtual function 49 | /* 50 | A pure virtual function is a special type of virtual function declared 51 | in a base class but without providing an implementation in the base class. 52 | Instead, it is meant to be overridden (provided with an implementation) by 53 | derived classes. Pure virtual functions are also referred to as abstract 54 | functions. 55 | */ 56 | virtual void calculateArea() = 0; 57 | }; 58 | 59 | class Circle : public Shape { 60 | private: 61 | double radius; 62 | 63 | public: 64 | Circle(double r) : radius(r) {} 65 | 66 | // Override the pure virtual function 67 | void calculateArea() override { 68 | double area = 3.14159 * radius * radius; 69 | cout << "Circle Area: " << area << endl; 70 | } 71 | }; 72 | 73 | class Rectangle : public Shape { 74 | private: 75 | double width; 76 | double height; 77 | 78 | public: 79 | Rectangle(double w, double h) : width(w), height(h) {} 80 | 81 | // Override the pure virtual function 82 | void calculateArea() override { 83 | double area = width * height; 84 | cout << "Rectangle Area: " << area << endl; 85 | } 86 | }; 87 | 88 | int main() { 89 | Circle circle(5.0); 90 | Rectangle rectangle(4.0, 6.0); 91 | 92 | // Polymorphic behavior using pointers to the base class 93 | Shape* shapePtr1 = &circle; 94 | Shape* shapePtr2 = &rectangle; 95 | 96 | shapePtr1->calculateArea(); // Calls Circle's implementation 97 | shapePtr2->calculateArea(); // Calls Rectangle's implementation 98 | 99 | return 0; 100 | } -------------------------------------------------------------------------------- /C++/AccessModifier/PrivateAccessModifier.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Private Access Modifier: 3 | - Members declared as private are not accessible from outside the class, 4 | even in derived classes. 5 | - Private members are used to encapsulate the internal implementation 6 | details and protect them from external interference. 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | 12 | class MyClass { 13 | private: 14 | int privateVar; // Private data member 15 | 16 | public: 17 | // setter method 18 | void setPrivateVar(int val) { 19 | privateVar = val; // Accessible through a public member function 20 | } 21 | 22 | 23 | // getter method 24 | int getPrivateVar() { 25 | return privateVar; // Accessible through a public member function 26 | } 27 | }; 28 | 29 | int main() { 30 | MyClass obj; 31 | obj.setPrivateVar(42); // Accessing private data member indirectly 32 | int value = obj.getPrivateVar(); // Accessing private data member indirectly 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /C++/AccessModifier/ProtectedAccessModifier.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* 5 | Protected Access Modifier: 6 | - Members declared as protected are accessible within the class and in 7 | derived classes (subclasses). 8 | - Protected members are used to provide limited access to derived classes 9 | while still keeping them hidden from external code. 10 | */ 11 | 12 | class BaseClass { 13 | protected: 14 | int protectedVar; // Protected data member 15 | 16 | public: 17 | void setProtectedVar(int val) { 18 | protectedVar = val; // Accessible through a public member function 19 | } 20 | 21 | int getProtectedVar() { 22 | return protectedVar; 23 | } 24 | }; 25 | 26 | 27 | /* 28 | public members of BaseClass remain public in DerivedClass, 29 | and protected members of BaseClass remain protected in DerivedClass. 30 | */ 31 | 32 | class DerivedClass : public BaseClass { 33 | public: 34 | void accessBaseClassMembers() { 35 | protectedVar = 42; // Accessible in derived class 36 | } 37 | }; 38 | 39 | int main() { 40 | DerivedClass obj; 41 | obj.setProtectedVar(42); // Accessing protected member via base class function 42 | cout << obj.getProtectedVar() << endl; // output will be 42 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /C++/AccessModifier/PublicAccessModifier.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Public Access Modifier: 3 | - Members declared as public are accessible from anywhere, both within 4 | the class and from external code. 5 | - Public members are part of the class's interface, and they can be used 6 | to interact with objects of the class. 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | class MyClass { 12 | public: 13 | int publicVar; // Public data member 14 | 15 | void publicMethod() { 16 | // Public member function 17 | } 18 | }; 19 | 20 | int main() { 21 | MyClass obj; 22 | obj.publicVar = 42; // Accessing public data member 23 | obj.publicMethod(); // Calling public member function 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /C++/Classes/Class.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | /* 4 | Class : 5 | - A class is a blueprint or template for creating objects (instances). 6 | - It defines the structure and behavior of objects that will be created based on it. 7 | - A class encapsulates data (attributes) and functions (methods) that operate on that data into a single unit. 8 | - A class is a user-defined data type. It allows you to define a new data type based on the structure and behavior you define within the class. 9 | 10 | */ 11 | 12 | // Here's the basic syntax for defining a class in C++: 13 | 14 | class Person { 15 | 16 | // data members 17 | private: 18 | string name; 19 | int age; 20 | 21 | public: 22 | // Constructor 23 | Person(string n, int a) { 24 | name = n; 25 | age = a; 26 | } 27 | 28 | // Public member functions 29 | void introduce() { 30 | cout << "Hello, I'm " << name << " and I'm " << age << " years old." << endl; 31 | } 32 | }; 33 | 34 | int main() { 35 | Person alice("Alice", 30); 36 | alice.introduce(); // Output: Hello, I'm Alice and I'm 30 years old. 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /C++/ConstructorOverloading/ConstructorOverloading.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | /* 6 | Constructor Overloading means when we have two or more constructors but change in number of parameters, 7 | or change in order of parameters, or change in data types of parameters are present 8 | 9 | Overloaded Costructor : 10 | 1.constructor overloading is when we overload default constructor and 11 | parameterized constructor like this 12 | 13 | Default : 14 | A(){ 15 | name = ""; 16 | } 17 | 18 | Parameterized: 19 | A(string name_a){ 20 | name = name_a; 21 | } 22 | 23 | Overloaded: 24 | A(string name_a = ""){ 25 | name = name_a; 26 | } 27 | */ 28 | 29 | class Student { 30 | private: 31 | string name; 32 | int rollNumber; 33 | int age; 34 | 35 | public: 36 | // Constructor with no arguments (default constructor) 37 | Student() { 38 | name = "Unknown"; 39 | rollNumber = 0; 40 | age = 0; 41 | } 42 | 43 | // Constructor with name and roll number 44 | Student(const string& studentName, int studentRollNumber) { 45 | name = studentName; 46 | rollNumber = studentRollNumber; 47 | age = 0; // Age is set to 0 by default 48 | } 49 | 50 | // Constructor with name, roll number, and age 51 | Student(const string& studentName, int studentRollNumber, int studentAge) { 52 | name = studentName; 53 | rollNumber = studentRollNumber; 54 | age = studentAge; 55 | } 56 | 57 | // Constructor can also be overloaded by changing order of parameters of constructor 58 | Student(int studentRollNumber, const string& studentName, int studentAge){ 59 | name = studentName; 60 | rollNumber = studentRollNumber; 61 | age = studentAge; 62 | } 63 | 64 | 65 | 66 | // Display student information 67 | void display() { 68 | cout << "Name: " << name << endl; 69 | cout << "Roll Number: " << rollNumber << endl; 70 | cout << "Age: " << age << " years" << endl; 71 | } 72 | }; 73 | 74 | int main() { 75 | // Create Student objects using different constructors 76 | Student student1; // Default constructor 77 | Student student2("Alice", 101); // Constructor with name and roll number 78 | Student student3("Bob", 102, 18); // Constructor with name, roll number, and age 79 | Student student4(12,"Lucky",15); // Constructor with roll number, name and age (by changing order of parameters); 80 | 81 | // Display student information 82 | cout << "Student 1:\n"; 83 | student1.display(); 84 | cout << "\nStudent 2:\n"; 85 | student2.display(); 86 | cout << "\nStudent 3:\n"; 87 | student3.display(); 88 | cout<< "\nStudent 4:\n"; 89 | student4.display(); 90 | 91 | return 0; 92 | } 93 | -------------------------------------------------------------------------------- /C++/Constructors/Constructor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* 5 | Constructor : 6 | - Constructor is a function which name is same as class name and 7 | having no return type. 8 | 9 | - Constructor is a special method that is invoked automatically 10 | at the time of object creation. 11 | 12 | - It is used to initialize the data members of new objects generally. 13 | 14 | - Constructors are used to initialize the data members of the object and perform any necessary setup. 15 | 16 | 17 | Constuctor is of three type. 18 | 1.Default or Non-parameterized 19 | 2.Parameterized 20 | 3.Copy 21 | 22 | 23 | Default Constructor : 24 | - Default Constructors don’t have input argument however. 25 | 26 | - If we do not specify a constructor, C++ compiler generates a default 27 | constructor for object (expects no parameters and has an empty body). 28 | 29 | - A default constructor is a constructor with no parameters. 30 | It is automatically called when an object is created if no other constructor is specified. 31 | 32 | Parameterized Constructor : 33 | - A parameterized constructor accepts one or more parameters to initialize 34 | the object with specific values. 35 | 36 | Copy Constructor : 37 | - A copy constructor creates a new object by copying the values of another 38 | object of the same class. It is used when objects are passed by value or when objects are created as 39 | */ 40 | 41 | //class 42 | class Dog{ 43 | //data member 44 | private: 45 | string name; 46 | public: 47 | //Default constructor 48 | Dog(){ 49 | name = ""; 50 | } 51 | //parameterized constructor 52 | Dog(string name_a){ 53 | name = name_a; 54 | } 55 | //copy constructor 56 | Dog(Dog &dog_1){ 57 | name = dog_1.name; 58 | } 59 | //member function 60 | void run(){ 61 | cout << name << " is running." << endl; 62 | } 63 | }; 64 | int main(){ 65 | Dog dg1;// dg is object of class Dog 66 | Dog dg2("Pitbull"); 67 | Dog dg3(dg2); 68 | 69 | dg1.run();// Output will be is running. 70 | dg2.run();// Output will be Pitbull is running. 71 | dg3.run();// Output will be Pitbull is running. 72 | 73 | return 0; 74 | } -------------------------------------------------------------------------------- /C++/Constructors/CopyConstructor.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copy Constructor : 3 | - A copy constructor is a special constructor in C++ that is used to 4 | create a new object as a copy of an existing object of the same class. 5 | It is often used when objects are passed by value, returned by value 6 | from a function, or when you explicitly create a new object as a copy 7 | of an existing one. 8 | */ 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | class Person { 15 | // data members 16 | private: 17 | string name; 18 | int age; 19 | 20 | public: 21 | // Parameterized constructor 22 | Person(string n, int a) { 23 | name = n; 24 | age = a; 25 | } 26 | 27 | // Copy constructor 28 | Person(const Person& other) { 29 | name = other.name; 30 | age = other.age; 31 | } 32 | 33 | // Member function to display person's information 34 | void displayInfo() { 35 | cout << "Name: " << name << endl; 36 | cout << "Age: " << age << endl; 37 | } 38 | }; 39 | 40 | int main() { 41 | // Creating a Person object using the parameterized constructor 42 | Person person1("Alice", 25); 43 | 44 | // Creating another Person object as a copy of person1 using the copy constructor 45 | Person person2 = person1; 46 | 47 | // Accessing the object's member function 48 | person1.displayInfo(); // Output: Name: Alice Age: 25 49 | person2.displayInfo(); // Output: Name: Alice Age: 25 50 | 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /C++/Constructors/DefaultConstructor.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Default Constructor : 3 | - A default constructor is a constructor that is automatically provided by 4 | the C++ compiler if no constructor is explicitly defined within a class. 5 | It initializes the object with default values or performs no initialization 6 | at all, depending on how it's implemented. 7 | */ 8 | #include 9 | using namespace std; 10 | 11 | class MyClass { 12 | public: 13 | // Default constructor 14 | MyClass() { 15 | cout << "Default constructor called." << endl; 16 | } 17 | }; 18 | 19 | int main() { 20 | // Creating an object of MyClass 21 | MyClass obj; // Default constructor is called automatically 22 | // Output will be Default constructor called. 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /C++/Constructors/ParameterizedConstructor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Person { 6 | private: 7 | string name; 8 | int age; 9 | 10 | public: 11 | // Parameterized constructor 12 | Person(string n, int a) { 13 | name = n; 14 | age = a; 15 | } 16 | 17 | // Member function to display person's information 18 | void displayInfo() { 19 | cout << "Name: " << name << endl; 20 | cout << "Age: " << age << endl; 21 | } 22 | }; 23 | 24 | int main() { 25 | // Creating a Person object using the parameterized constructor 26 | Person person1("Alice", 25); 27 | 28 | // Accessing the object's member function 29 | person1.displayInfo(); // Output: Name: Alice Age: 25 30 | 31 | // Creating another Person object with different values 32 | Person person2("Bob", 30); 33 | person2.displayInfo(); // Output: Name: Bob Age: 30 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /C++/Destructors/Destructor.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Destructor ; 3 | - A destructor in C++ is a special member function of a class that is 4 | called when an object of the class goes out of scope or is explicitly 5 | deleted. The destructor is responsible for releasing resources or 6 | performing cleanup tasks associated with the object before it is 7 | destroyed. 8 | */ 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | class Person { 15 | private: 16 | string name; 17 | int age; 18 | 19 | public: 20 | // Parameterized constructor 21 | Person(string n, int a) { 22 | name = n; 23 | age = a; 24 | cout << "Constructor called for " << name << endl; 25 | } 26 | 27 | // Destructor 28 | ~Person() { 29 | cout << "Destructor called for " << name << endl; 30 | } 31 | 32 | // Member function to display person's information 33 | void displayInfo() { 34 | cout << "Name: " << name << endl; 35 | cout << "Age: " << age << endl; 36 | } 37 | }; 38 | 39 | int main() { 40 | // Creating a Person object 41 | Person person1("Alice", 25); 42 | 43 | // Accessing the object's member function 44 | person1.displayInfo(); // Output: Name: Alice Age: 25 45 | 46 | // The destructor is automatically called when person1 goes out of scope 47 | return 0; // Destructor called for Alice 48 | } 49 | /* 50 | Output will be : 51 | Constructor called for Alice 52 | Name: Alice 53 | Age: 25 54 | Destructor called for Alice 55 | */ -------------------------------------------------------------------------------- /C++/Destructors/VirtualDestructor/VirtualDestructor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | /* 4 | Virtual Destructor : 5 | If we create pointer of base class which is pointing to the 6 | derived class then we only base will be destroyed and derived 7 | class will not be destroyed. 8 | 9 | For destroying both base and derived class we need to create virtual 10 | destructor by using virtual keyword in front of destructor of base class 11 | 12 | Virtual function can't be overloaded. 13 | 14 | */ 15 | 16 | 17 | //Base class or Parent class or Super class 18 | class Base{ 19 | public: 20 | // destructor 21 | /* 22 | in this case output will be -> 23 | 24 | Base class destroyed 25 | */ 26 | 27 | // ~Base(){ 28 | // cout << "Base class destroyed" << endl; 29 | // } 30 | 31 | 32 | // After using virtual keyword 33 | /* 34 | output will be -> 35 | 36 | derived class destroyed 37 | Base class destroyed 38 | 39 | */ 40 | virtual ~Base(){ 41 | cout << "Base class destroyed" << endl; 42 | } 43 | 44 | }; 45 | 46 | //Child class or sub class or derived class 47 | class Derived:public Base{ 48 | public: 49 | //destructor 50 | ~Derived(){ 51 | cout << "derived class destroyed" << endl; 52 | } 53 | }; 54 | 55 | int main(){ 56 | 57 | Base *ptr = new Derived;//pointer of base class pointing to derived class 58 | delete ptr; 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /C++/FriendFunctionAndClass/FriendClass/FriendClass.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* 5 | 6 | Friend Class : 7 | A friend class can access private and protected members of other classes 8 | in which it is declared as a friend. It is sometimes useful to allow a 9 | particular class to access private and protected members of other classes. 10 | For example, a LinkedList class may be allowed to access private members of 11 | Node.We can declare a friend class in C++ by using the friend keyword. 12 | 13 | We can declare friend class or function anywhere in the base class body whether 14 | its private, protected or public block. It works all the same. 15 | */ 16 | 17 | class FriendClassImpl { 18 | private: 19 | int private_variable; 20 | 21 | protected: 22 | int protected_variable; 23 | 24 | public: 25 | FriendClassImpl() { 26 | private_variable = 10; 27 | protected_variable = 99; 28 | } 29 | 30 | friend class FriendClass; 31 | }; 32 | 33 | /* 34 | Here, class FriendClass is declared as a friend inside class FriendClassImpl. 35 | Therefore,FriendClass is a friend of class FriendClassImpl. Class FriendClass 36 | can access the private members of class FriendClassImpl. 37 | */ 38 | class FriendClass { 39 | public: 40 | void display(FriendClassImpl& fci) 41 | { 42 | cout << "The value of Private Variable = " 43 | << fci.private_variable << endl; 44 | cout << "The value of Protected Variable = " 45 | << fci.protected_variable; 46 | } 47 | }; 48 | 49 | // Driver code 50 | int main() 51 | { 52 | FriendClassImpl fci; 53 | FriendClass fc; 54 | fc.display(fci); 55 | return 0; 56 | } -------------------------------------------------------------------------------- /C++/FriendFunctionAndClass/FriendFun/FriendFunMultiple.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Forward declarations 5 | class TemperatureSensor; 6 | class HumiditySensor; 7 | 8 | class TemperatureSensor { 9 | int temperature; 10 | 11 | public: 12 | void setTemperature(int value) { 13 | temperature = value; 14 | } 15 | 16 | friend void reportWeather(TemperatureSensor, HumiditySensor); 17 | }; 18 | 19 | class HumiditySensor { 20 | int humidity; 21 | 22 | public: 23 | void setHumidity(int value) { 24 | humidity = value; 25 | } 26 | 27 | friend void reportWeather(TemperatureSensor, HumiditySensor); 28 | }; 29 | 30 | void reportWeather(TemperatureSensor tempSensor, HumiditySensor humiditySensor) { 31 | cout << "Today's Weather Report:" << endl; 32 | cout << "Temperature: " << tempSensor.temperature << " degrees Celsius" << endl; 33 | cout << "Humidity: " << humiditySensor.humidity << "%" << endl; 34 | } 35 | 36 | int main() { 37 | TemperatureSensor temperatureSensor; 38 | HumiditySensor humiditySensor; 39 | 40 | temperatureSensor.setTemperature(28); 41 | humiditySensor.setHumidity(60); 42 | 43 | // Calling the friend function to report the weather 44 | reportWeather(temperatureSensor, humiditySensor); 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /C++/FriendFunctionAndClass/FriendFun/GlobalFriendFun.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* 5 | 6 | 7 | Friend Function 8 | 9 | Like a friend class, a friend function can be granted special access to 10 | private and protected members of a class in C++. They are the non-member 11 | functions that can access and manipulate the private and protected members 12 | of the class for they are declared as friends. 13 | 14 | A friend function can be: 15 | 16 | a) A global function 17 | We can declare any global function as a friend function. 18 | The following example demonstrates how to declare a global 19 | function as a friend function in C++: 20 | b) A member function of another class 21 | 22 | */ 23 | 24 | // Imagine a class representing a Person 25 | class Person { 26 | private: 27 | string name; // Private member: name 28 | int age; // Private member: age 29 | 30 | protected: 31 | string address; // Protected member: address 32 | 33 | public: 34 | Person(const string& n, int a, const string& addr) 35 | : name(n), age(a), address(addr) {} 36 | 37 | // Friend function declaration 38 | friend void displayPersonInfo(const Person& person); 39 | }; 40 | 41 | // Friend function definition to display Person's info 42 | void displayPersonInfo(const Person& person) { 43 | cout << "Name: " << person.name << endl; 44 | cout << "Age: " << person.age << " years" << endl; 45 | cout << "Address: " << person.address << endl; 46 | } 47 | 48 | int main() { 49 | // Create a Person object 50 | Person person1("Alice", 30, "123 Main St"); 51 | 52 | // Use the friend function to display the Person's info 53 | displayPersonInfo(person1); 54 | 55 | return 0; 56 | } 57 | 58 | -------------------------------------------------------------------------------- /C++/FriendFunctionAndClass/FriendFun/MemberFriendFun.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* 5 | 6 | Friend Function 7 | 8 | Like a friend class, a friend function can be granted special access to 9 | private and protected members of a class in C++. They are the non-member 10 | functions that can access and manipulate the private and protected members 11 | of the class for they are declared as friends. 12 | 13 | A friend function can be: 14 | 15 | a) A global function 16 | We can declare any global function as a friend function. 17 | The following example demonstrates how to declare a global 18 | function as a friend function in C++: 19 | 20 | b) A member function of another class 21 | We can also declare a member function of another class as a friend function 22 | in C++. The following example demonstrates how to use a member function of 23 | another class as a friend function in C++: 24 | 25 | */ 26 | 27 | 28 | /* 29 | The order in which we define the friend function of another class is important 30 | and should be taken care of. We always have to define both the classes before 31 | the function definition. Thats why we have used out of class member function 32 | definition. 33 | */ 34 | class Person; // Forward declaration 35 | 36 | class Friend { 37 | public: 38 | void introduce(const Person& person); 39 | }; 40 | 41 | class Person { 42 | private: 43 | string name; 44 | int age; 45 | 46 | protected: 47 | string address; 48 | 49 | public: 50 | Person(const string& n, int a, const string& addr) 51 | : name(n), age(a), address(addr) {} 52 | 53 | // Friend function declaration 54 | friend void Friend::introduce(const Person& person); 55 | }; 56 | 57 | // Friend function definition to introduce a person 58 | void Friend::introduce(const Person& person) { 59 | cout << "Hello, my name is " << person.name << "." << endl; 60 | cout << "I am " << person.age << " years old." << endl; 61 | cout << "I live at " << person.address << "." << endl; 62 | } 63 | 64 | int main() { 65 | // Create a Person object 66 | Person person1("Alice", 30, "123 Main St"); 67 | 68 | // Create a Friend object 69 | Friend friend1; 70 | 71 | // Use the friend function to introduce the person 72 | friend1.introduce(person1); 73 | 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /C++/Inheritence/ConstructorInheritence/ConstructorInheritence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | 6 | class Base { 7 | public: 8 | // Constructor of the Base class 9 | Base(int x) { 10 | this->x = x; 11 | cout << "Base class constructor called with x = " << x << endl; 12 | } 13 | 14 | void showX() { 15 | cout << "Value of x in Base class: " << x << endl; 16 | } 17 | 18 | // data member 19 | private: 20 | int x; 21 | }; 22 | 23 | /* 24 | Derived class is inheriting base class publicly tha means Public inheritance 25 | is a way of expressing an "is-a" relationship between classes. It means that 26 | the derived class Derived is a type of the base class Base and inherits 27 | its public and protected properties. Public members of Base remain public in 28 | Derived,allowing them to be used within Derived and by external code. 29 | */ 30 | class Derived : public Base { 31 | public: 32 | // Constructor of the Derived class 33 | Derived(int x, int y) : Base(x) { 34 | this->y = y; 35 | cout << "Derived class constructor called with y = " << y << endl; 36 | } 37 | 38 | void showY() { 39 | cout << "Value of y in Derived class: " << y << endl; 40 | } 41 | 42 | // data member 43 | private: 44 | int y; 45 | }; 46 | 47 | int main() { 48 | // Creating an object of the Derived class 49 | Derived derivedObj(10, 20); 50 | 51 | // Accessing members of both the Base and Derived classes 52 | derivedObj.showX(); // Accessing the Base class member function 53 | derivedObj.showY(); // Accessing the Derived class member function 54 | 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /C++/Inheritence/Examples/InheritenceExample1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | /* 4 | Inheritence : 1.The capability of a class to derive properties and 5 | characteristics from another class is called Inheritance. 6 | 7 | 2.The new class which inherited the base class is called derived 8 | class or child class or "sub class" and the existing class is known as the 9 | base class or parent class or "super class. 10 | 11 | Used to improve code reuseability 12 | Derived class can access both derived and base class. 13 | 14 | 15 | */ 16 | 17 | //Base class or Parent class or Super class 18 | class Rectangle { 19 | public: 20 | //data member 21 | int length , breadth; 22 | public: 23 | //member function 24 | void show(){ 25 | cout << "Length is " << length << "\n" << "Breadth is " << breadth << endl; 26 | } 27 | 28 | void area(){ 29 | cout << "Area is " << length*breadth << endl; 30 | } 31 | }; 32 | 33 | 34 | //Child class or sub class or derived class 35 | class Cubeoid:public Rectangle { 36 | public: 37 | //data member 38 | int height; 39 | 40 | //member function 41 | void display(){ 42 | cout << "Height is " << height << endl; 43 | } 44 | 45 | void volume(){ 46 | cout << "Volume is " << length*breadth*height << endl; 47 | } 48 | }; 49 | int main(){ 50 | Cubeoid c;//object created of Cuboid class 51 | c.length = 10 , c.breadth = 20 , c.height = 30; 52 | c.show();//called function of Rectangle class 53 | c.display();//called function of Cubeoid class 54 | c.volume(); 55 | c.area(); 56 | return 0; 57 | } -------------------------------------------------------------------------------- /C++/Inheritence/MultilevelInheritence/MultilevelInheritence.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | In multilevel inheritance, a derived class inherits from another derived class. 3 | This creates a chain of inheritance. 4 | 5 | class c inherits class B 6 | class B inherites class A 7 | C <- B <- A 8 | 9 | [ <- ] : inheritence 10 | 11 | */ 12 | #include 13 | using namespace std; 14 | 15 | class Grandparent { 16 | public: 17 | int grandparentData; 18 | }; 19 | 20 | class Parent : public Grandparent { 21 | public: 22 | int parentData; 23 | }; 24 | 25 | class Child : public Parent { 26 | public: 27 | int childData; 28 | }; 29 | 30 | int main() { 31 | // Create an object of the Child class 32 | Child childObj; 33 | 34 | // Assign values to data members inherited from Grandparent, Parent, and Child 35 | childObj.grandparentData = 42; 36 | childObj.parentData = 24; 37 | childObj.childData = 12; 38 | 39 | // Display the values of data members 40 | cout << "Grandparent Data: " << childObj.grandparentData << endl; 41 | cout << "Parent Data: " << childObj.parentData << endl; 42 | cout << "Child Data: " << childObj.childData << endl; 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /C++/Inheritence/MultipleInheritence/MultipleInheritence.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Multiple Inheritence : 3 | - In multilevel inheritance, a derived class inherits from another 4 | derived class. This creates a chain of inheritance. 5 | 6 | class C inherits class A 7 | class B inherits class A 8 | 9 | C <- A 10 | B <- A 11 | 12 | [ <- ] : Inheritence 13 | 14 | 15 | 16 | */ 17 | #include 18 | using namespace std; 19 | 20 | class Base1 { 21 | public: 22 | int data1; 23 | }; 24 | 25 | class Base2 { 26 | public: 27 | int data2; 28 | }; 29 | 30 | class Derived : public Base1, public Base2 { 31 | public: 32 | void display() { 33 | cout << "Data1: " << data1 << "\n" << ", Data2: " << data2 << endl; 34 | } 35 | }; 36 | 37 | int main() { 38 | // Create an object of the Derived class 39 | Derived derivedObj; 40 | 41 | // Assign values to data members inherited from Base1 and Base2 42 | derivedObj.data1 = 42; 43 | derivedObj.data2 = 24; 44 | 45 | // Call the 'display' member function from the Derived class 46 | derivedObj.display(); // Output: Data1: 42, Data2: 24 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /C++/Inheritence/SingleInheritence/SingleInheritence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Base { 5 | private: 6 | int data; 7 | public: 8 | void setData(int val) { 9 | data = val; 10 | } 11 | int getData() { 12 | return data; 13 | } 14 | }; 15 | /* 16 | The keyword public in this context is used to specify the access control 17 | for the inheritance relationship between the Derived class and the Base 18 | class. It determines how the members (data members and member functions) 19 | of the base class Base will be accessible in the derived class Derived. 20 | In this specific case: 21 | 22 | Public Members: 23 | Any public members (data members and member functions) of the 24 | base class Base will remain public in the derived class Derived. This means 25 | that they can be accessed both from within the derived class itself and from 26 | external code that creates objects of the derived class. 27 | 28 | Protected Members: 29 | Any protected members of the base class Base will become 30 | protected members in the derived class Derived. This means that they are 31 | accessible within the derived class and any classes derived from Derived, 32 | but they are not accessible from external code. 33 | 34 | Private Members: 35 | Private members of the base class Base remain private to 36 | the base class and are not accessible in the derived class Derived. 37 | 38 | Public inheritance is a way of expressing an "is-a" relationship between classes. 39 | It means that the derived class Derived is a type of the base class Base and inherits 40 | its public and protected properties. Public members of Base remain public in Derived, 41 | allowing them to be used within Derived and by external code. 42 | */ 43 | class Derived : public Base { 44 | public: 45 | void display() { 46 | cout << "Data: " << getData() << endl; 47 | } 48 | }; 49 | int main() { 50 | // Create an object of the Derived class 51 | Derived derivedObj; 52 | 53 | // Assign a value to the 'data' member inherited from the Base class 54 | derivedObj.setData(45); 55 | 56 | // Call the 'display' member function from the Derived class 57 | derivedObj.display(); // Output: Data: 45 58 | 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /C++/Object/Object.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Objects : 3 | - An object is an instance of a class. 4 | - Objects are the fundamental building blocks of OOP and are used to model and represent real-world entities or concepts in your program. 5 | 6 | Here are the key characteristics and concepts related to objects in C++ OOP: 7 | 8 | - Instance of a Class: 9 | An object is created based on a class definition. 10 | The class acts as a blueprint or template that defines the structure 11 | (data members) and behavior (member functions) that objects of that 12 | class will have. 13 | 14 | - Data and Behavior: 15 | Objects encapsulate both data (attributes or properties) and 16 | behavior (methods or functions). 17 | The data represents the state of the object, while the behavior 18 | defines the actions or operations that can be performed on the 19 | object's data. 20 | 21 | - Instantiation: 22 | Creating an object is referred to as "instantiating" the class. 23 | When you create an object, memory is allocated for its data members, 24 | and the constructor (a special member function) is called to initialize the object. 25 | 26 | - Identity: 27 | Each object has a unique identity, which distinguishes it 28 | from other objects. Even if two objects of the same class have 29 | the same data values, they are still distinct objects with 30 | separate memory locations. 31 | 32 | - Usage: 33 | Objects are used to interact with the data and behavior defined by 34 | the class. You can invoke methods on objects, access their data 35 | members, and modify their state. 36 | */ 37 | #include 38 | using namespace std; 39 | 40 | class Rectangle { 41 | // data members 42 | private: 43 | double length; 44 | double width; 45 | 46 | public: 47 | // Constructor to initialize the object 48 | Rectangle(double l, double w) { 49 | length = l; 50 | width = w; 51 | } 52 | 53 | // Member function to calculate and return the area 54 | double calculateArea() { 55 | return length * width; 56 | } 57 | 58 | // Member function to calculate and return the perimeter 59 | double calculatePerimeter() { 60 | return 2 * (length + width); 61 | } 62 | 63 | // Getter functions to access private data members 64 | double getLength() { 65 | return length; 66 | } 67 | 68 | double getWidth() { 69 | return width; 70 | } 71 | }; 72 | 73 | int main() { 74 | // Creating two Rectangle objects 75 | // This is a one way of creating an object 76 | Rectangle rectangle1(5.0, 3.0); 77 | Rectangle rectangle2(4.0, 4.0); 78 | 79 | // Creating objects using new keyword 80 | // This is another way of creatin an object. 81 | Rectangle* rectangle3 = new Rectangle(4.0,6.0); 82 | 83 | // Accessing member functions to perform operations 84 | double area1 = rectangle1.calculateArea(); 85 | double perimeter1 = rectangle1.calculatePerimeter(); 86 | 87 | double area2 = rectangle2.calculateArea(); 88 | double perimeter2 = rectangle2.calculatePerimeter(); 89 | 90 | double area3 = rectangle3->calculateArea(); 91 | double perimeter3 = rectangle3->calculatePerimeter(); 92 | 93 | // Displaying results 94 | cout << "Rectangle 1:" << endl; 95 | cout << "Length: " << rectangle1.getLength() << " Width: " << rectangle1.getWidth() << endl; 96 | cout << "Area: " << area1 << " Perimeter: " << perimeter1 << endl; 97 | 98 | cout << "\nRectangle 2:" << endl; 99 | cout << "Length: " << rectangle2.getLength() << " Width: " << rectangle2.getWidth() << endl; 100 | cout << "Area: " << area2 << " Perimeter: " << perimeter2 << endl; 101 | 102 | cout << "\nRectangle 3:" << endl; 103 | cout << "Length: " << rectangle3->getLength() << " Width: " << rectangle3->getWidth() << endl; 104 | cout << "Area: " << area3 << " Perimeter: " << perimeter3 << endl; 105 | 106 | return 0; 107 | } 108 | -------------------------------------------------------------------------------- /C++/Polymorphism/CompiletimePolymorphism/CompiletimePolymorphismFunctionOverloading.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | /* 4 | Polymorphism 5 | Polymorphism is a fundamental concept in C++ and object-oriented programming 6 | (OOP) that allows objects of different classes to be treated as objects of a 7 | common base class. It enables you to write more flexible and generic code by 8 | abstracting away the specific implementation details of classes. There are two 9 | main types of polymorphism in C++: compile-time (or static) polymorphism and 10 | runtime (or dynamic) polymorphism. Here's an explanation and example code for 11 | both types: 12 | 13 | (i) Compile-time (Static or Overloading or Early binding) Polymorphism: 14 | This type of polymorphism is resolved at compile time. 15 | It is achieved through function overloading and operator overloading. 16 | 17 | 18 | (a) Function Overloading: 19 | Function overloading allows you to define multiple functions with 20 | the same name but different parameter lists. The appropriate function 21 | to be called is determined by the number and types of arguments at 22 | compile time. 23 | 24 | (b) Operator Overloading: 25 | Operator overloading allows you to define the behavior of operators 26 | for user-defined types. It enables you to use operators like 27 | +, -, *, etc., with objects of custom classes. 28 | 29 | (ii) Runtime (Dynamic or Late binding or Overriding) Polymorphism: 30 | Runtime polymorphism is achieved through virtual functions and is 31 | resolved at runtime. It allows you to call the appropriate method of 32 | an object based on its actual type, even when accessed through a 33 | pointer or reference to a base class. 34 | 35 | (a) Runtime Polymorphism with Data Members 36 | 37 | (b) Virtual Function 38 | */ 39 | class Calculator { 40 | public: 41 | /* 42 | Here is the function overloading on add function: 43 | 1. return type of add fun is different 44 | 2. parameter of add fun is also different 45 | */ 46 | int add(int a, int b) { 47 | return a + b; 48 | } 49 | 50 | double add(double a, double b) { 51 | return a + b; 52 | } 53 | 54 | string add(string a, string b) { 55 | return a+b; 56 | } 57 | 58 | }; 59 | 60 | int main() { 61 | Calculator calc; 62 | int sum1 = calc.add(5, 3); 63 | double sum2 = calc.add(2.5, 3.7); 64 | string concate = calc.add("Madhurendra Nath " , "Tiwari"); 65 | 66 | cout << "Sum 1: " << sum1 << endl; 67 | cout << "Sum 2: " << sum2 << endl; 68 | cout << "My name is: " << concate << endl; 69 | 70 | return 0; 71 | } -------------------------------------------------------------------------------- /C++/Polymorphism/CompiletimePolymorphism/CompiletimePolymorphismOperatorOverloading.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Complex { 5 | public: 6 | Complex(double r, double i) : real(r), imag(i) {} 7 | 8 | Complex operator+(const Complex& other) { 9 | return Complex(real + other.real, imag + other.imag); 10 | } 11 | 12 | Complex operator-(const Complex& other) { 13 | return Complex(real - other.real, imag - other.imag); 14 | } 15 | 16 | Complex operator*(const Complex& other) { 17 | return Complex(real * other.real, imag * other.imag); 18 | } 19 | 20 | 21 | Complex operator/(const Complex& other) { 22 | return Complex(real / other.real, imag / other.imag); 23 | } 24 | 25 | 26 | void display() { 27 | cout << real << " + " << imag << "i" << endl; 28 | } 29 | 30 | private: 31 | double real; 32 | double imag; 33 | }; 34 | 35 | int main() { 36 | Complex a(3.0, 4.0); 37 | Complex b(1.5, 2.5); 38 | Complex c = a + b; 39 | Complex d = a - b; 40 | Complex e = a * b; 41 | Complex f = a / b; 42 | 43 | 44 | c.display(); // 4.5 + 6.5i 45 | d.display(); // 1.5 + 1.5i 46 | e.display(); // 4.5 + 10i 47 | f.display(); // 2 + 1.6i 48 | 49 | return 0; 50 | } -------------------------------------------------------------------------------- /C++/Polymorphism/RuntimePolymorphism/RuntimePolymorphismFunctionOverriding.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Shape { 5 | public: 6 | // Virtual function to calculate area 7 | virtual double calculateArea() { 8 | return 0.0; // Default implementation for the base class 9 | } 10 | }; 11 | 12 | class Circle : public Shape { 13 | private: 14 | double radius; 15 | 16 | public: 17 | Circle(double r) : radius(r) {} 18 | 19 | // Override the virtual function for Circle 20 | double calculateArea() override { 21 | return 3.14159 * radius * radius; 22 | } 23 | }; 24 | 25 | class Rectangle : public Shape { 26 | private: 27 | double width; 28 | double height; 29 | 30 | public: 31 | Rectangle(double w, double h) : width(w), height(h) {} 32 | 33 | // Override the virtual function for Rectangle 34 | double calculateArea() override { 35 | return width * height; 36 | } 37 | }; 38 | 39 | int main() { 40 | Shape* circle = new Circle(6.0); 41 | 42 | cout << "Area of circle : " << circle->calculateArea() << endl; 43 | 44 | 45 | Shape* shapes[3]; 46 | shapes[0] = new Circle(5.0); 47 | shapes[1] = new Rectangle(4.0, 6.0); 48 | shapes[2] = new Circle(3.0); 49 | 50 | for (int i = 0; i < 3; i++) { 51 | cout << "Area of shape " << i+1 << ": " << shapes[i]->calculateArea() << endl; 52 | delete shapes[i]; 53 | } 54 | 55 | return 0; 56 | } 57 | /* 58 | Output will be : 59 | Area of circle : 113.097 60 | Area of shape 1: 78.5397 61 | Area of shape 2: 24 62 | Area of shape 3: 28.2743 63 | */ 64 | -------------------------------------------------------------------------------- /C++/Polymorphism/RuntimePolymorphism/RuntimePolymorphismWithDataMember.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Animal { 5 | public: 6 | string color = "Black"; 7 | }; 8 | 9 | // inheriting Animal class. 10 | class Dog : public Animal { 11 | public: 12 | string color = "Grey"; 13 | }; 14 | 15 | // Driver code 16 | int main(void) { 17 | Dog dog = Dog(); 18 | Animal d = Dog(); // accessing the field by reference 19 | // variable which refers to derived 20 | cout << d.color << endl; // Grey 21 | cout << dog.color << endl; // Black 22 | } -------------------------------------------------------------------------------- /C++/StaticMember/DataMember/StaticDataMember.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* 5 | Static Data Members: 6 | 7 | Static data members are class-level variables that are shared among all 8 | instances (objects) of the class.They are declared with the static keyword 9 | and must be defined separately outside the class declaration.Each instance 10 | of the class can access and modify the same static data member.Static data 11 | members are initialized only once, typically during program startup. 12 | 13 | */ 14 | class StaticDataMemberClass { 15 | public: 16 | static int count; // Declaration of a static data member 17 | }; 18 | 19 | // Definition and initialization of the static data member 20 | int StaticDataMemberClass::count = 0; 21 | 22 | int main() { 23 | StaticDataMemberClass obj1,obj2,obj3; 24 | 25 | obj1.count = 1; 26 | obj2.count = 2; 27 | obj3.count = 3; 28 | 29 | std::cout << obj1.count << " " << obj2.count << " " << obj3.count << std::endl; // Outputs: 2 2 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /C++/StaticMember/MemberFunction/StaticMemberFun.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | /* 4 | Static Member Functions: 5 | Static member functions are class-level functions that do not operate on 6 | specific instances of the class. They are associated with the class itself 7 | rather than objects.They are declared with the static keyword and can be 8 | called using the class name, without the need to create instances of the 9 | class.Static member functions can only access static data members and other 10 | static member functions of the class since they don't have access to instance 11 | specific data. 12 | */ 13 | 14 | class StaticDataMemberFunClass { 15 | public: 16 | static int add(int a, int b) { 17 | return a + b; 18 | } 19 | }; 20 | 21 | int main() { 22 | int result = StaticDataMemberFunClass::add(3, 5); // Calling a static member function 23 | cout << "Result: " << result << endl; // Outputs: 8 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to [OOPs](https://github.com/dev-madhurendra/OOPs) 2 | 3 | Thank you for your interest in contributing to [OOPs](https://github.com/dev-madhurendra/OOPs)! We welcome contributions from the community to make our project better. 4 | 5 | Please take a moment to read the following guidelines to understand how you can contribute effectively. 6 | 7 | ## Table of Contents 8 | 9 | - [Code of Conduct](#code-of-conduct) 10 | - [How Can I Contribute?](#how-can-i-contribute) 11 | - [Reporting Bugs](#reporting-bugs) 12 | - [Suggesting Enhancements](#suggesting-enhancements) 13 | - [One Concept per PR](#one-concept-per-pr) 14 | - [Pull Request Guidelines](#pull-request-guidelines) 15 | - [Branch Naming Conventions](#branch-naming-convention) 16 | - [Development Setup](#development-setup) 17 | - [License](#license) 18 | 19 | ## Code of Conduct 20 | 21 | Please note that this project has adopted a [Code of Conduct](CODE_OF_CONDUCT.md). We expect all contributors to adhere to it. Please read the full text so that you understand the behavior we expect. 22 | 23 | ## How Can I Contribute? 24 | 25 | There are several ways you can contribute to this project: 26 | 27 | - Reporting bugs 28 | - Suggesting enhancements 29 | - Writing code 30 | - Improving documentation 31 | - Sharing your feedback and ideas 32 | - Add a newconcepts 33 | 34 | Please follow the guidelines outlined below for each type of contribution. 35 | 36 | ## Reporting Bugs 37 | 38 | If you come across a bug or an issue with the project, please follow these steps: 39 | 40 | 1. **Search for Existing Issues**: Check if the issue has already been reported by searching the [GitHub Issues](https://github.com/dev-madhurendra/OOPs/issues) page. 41 | 42 | 2. **Create a New Issue**: If the issue is not already reported, please [create a new issue](https://github.com/dev-madhurendra/OOPs/issues/new). Be sure to include as much detail as possible, including a clear description and any relevant code or error messages. 43 | 44 | 3. **Follow Up**: Be prepared to provide additional information or clarification if needed and engage in discussions to help us investigate and resolve the issue. 45 | 46 | ## Suggesting Enhancements 47 | 48 | If you have an idea for an enhancement or a new feature, please follow these steps: 49 | 50 | 1. **Check Existing Enhancements**: Look through the [GitHub Issues](https://github.com/dev-madhurendra/OOPs/issues) to see if your idea has already been suggested. 51 | 52 | 2. **Create a New Issue**: If it's a new idea, please [create a new issue](https://github.com/dev-madhurendra/OOPs/issues/new) and clearly describe the enhancement you have in mind. Be specific and provide context for why it would be valuable. 53 | 54 | 3. **Discuss and Collaborate**: Engage in discussions with the maintainers and the community to refine the enhancement proposal and get feedback. 55 | 56 | ## Pull Request Guidelines 57 | 58 | We welcome contributions in the form of pull requests (PRs). Here are some guidelines for creating PRs: 59 | 60 | 1. **Fork the Repository**: Start by forking the [OOPs-Repository](https://github.com/dev-madhurendra/OOPs) to your own GitHub account. 61 | 62 | 2. **Create a Branch**: Create a new branch for your changes. Use a descriptive branch name (e.g., `feature/my-new-feature`). 63 | 64 | 3. **Make Changes**: Make your changes, adhering to the project's coding style and guidelines. 65 | 66 | 4. **Test Your Changes**: Ensure that your changes work as intended and do not introduce new issues. 67 | 68 | 5. **Documentation**: If your changes affect user documentation, please update the documentation accordingly. 69 | 70 | 6. **Submit a Pull Request**: Open a pull request from your forked repository to the original repository's `main` branch. 71 | 72 | 7. **Code Review**: Be prepared to participate in code reviews and address any feedback or suggestions from the maintainers. 73 | 74 | 8. **Squash Commits**: If you make multiple commits, consider squashing them into a single commit with a clear and concise commit message. 75 | 76 | 9. **License**: Ensure that your contributions comply with the project's license. 77 | 78 | ## One Concept per PR 79 | 80 | We follow a strict guideline in this project: **One Concept per Pull Request (PR)**. When contributing code or changes, please ensure that each PR focuses on a single concept, feature, or bug fix. 81 | 82 | This guideline helps maintain code clarity, simplifies code reviews, and ensures that each PR is well-contained and easy to understand. It also allows for more straightforward testing and troubleshooting. 83 | 84 | When creating a PR, make sure to: 85 | 86 | - Clearly describe the concept, feature, or fix addressed by the PR. 87 | - Avoid bundling unrelated changes in a single PR. 88 | - Keep the scope of the PR limited to the chosen concept. 89 | 90 | By following this guideline, we can maintain a clean and organized codebase that is easier to maintain and collaborate on. 91 | 92 | ## Branch Naming Convention 93 | 94 | In this project, we follow a specific branch naming convention to organize code related to different programming languages and Object-Oriented Programming (OOP) concepts. Branch names should follow the format: 95 | 96 | - `[DirName]`: The name of the directory or programming language you are working with. For example, `Java`, `Cpp`, `Python`, etc. 97 | - `[Oops-Concept]`: The specific OOP concept you are exploring or implementing. For example, `Inheritance`, `Polymorphism`, `Encapsulation`, etc. 98 | 99 | Here are some examples of valid branch names: 100 | 101 | - `Java_Multiple-Inheritance` 102 | - `Cpp_Class` 103 | - `Python_Polymorphism` 104 | 105 | By following this naming convention, it becomes easier to track and manage branches related to different programming languages and OOP concepts within the project. 106 | 107 | ## Development Setup 108 | 109 | If you're interested in contributing code, please refer to the [development setup instructions](DEVELOPMENT.md) for details on setting up your development environment. 110 | 111 | ## License 112 | 113 | By contributing to [Your Repository Name], you agree that your contributions will be licensed under the [LICENSE](LICENSE) of the project. 114 | 115 | Thank you for your contributions, and we appreciate your help in making [Your Repository Name] a better project! 116 | 117 | If you have any questions or need assistance, please feel free to reach out to us. 118 | 119 | Writer [@dev-madhurendra](https://github.com/dev-madhurendra) and contributors, Sept 2023. 120 | -------------------------------------------------------------------------------- /Golang/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dev-madhurendra/OOPs/97ca90e99766f4e022f2c14e297055c7e49ddd65/Golang/.gitkeep -------------------------------------------------------------------------------- /Java/Abstraction/Abstraction.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Abstraction is a process of hiding the implementation details and showing only functionality to the user. 3 | * Another way, it shows only essential things to the user and hides the internal details, for example, sending SMS where you type the text and send the message. 4 | * You don't know the internal processing about the message delivery. 5 | * 6 | * Points to Remember 7 | * ---------------------- 8 | 1) An abstract class must be declared with an abstract keyword. 9 | 2) It can have abstract and non-abstract methods. 10 | 3) It cannot be instantiated. 11 | 4) It can have constructors and static methods also. 12 | 5) It can have final methods which will force the subclass not to change the body of the method. 13 | */ 14 | 15 | 16 | 17 | /** 18 | * Use of Abstract class to implement abstraction 19 | */ 20 | abstract class Shape { 21 | String color; 22 | 23 | // these are abstract methods 24 | abstract double area(); 25 | 26 | public abstract String toString(); 27 | 28 | // abstract class can have the constructor 29 | public Shape(String color) { 30 | System.out.println("Shape constructor called"); 31 | this.color = color; 32 | } 33 | 34 | // this is a concrete method 35 | public String getColor() { 36 | return color; 37 | } 38 | } 39 | 40 | class Circle extends Shape { 41 | double radius; 42 | 43 | public Circle(String color, double radius) { 44 | 45 | // calling Shape constructor 46 | super(color); 47 | System.out.println("Circle constructor called"); 48 | this.radius = radius; 49 | } 50 | 51 | @Override 52 | double area() { 53 | return Math.PI * Math.pow(radius, 2); 54 | } 55 | 56 | @Override 57 | public String toString() { 58 | return "Circle color is " + super.getColor() 59 | + "and area is : " + area(); 60 | } 61 | } 62 | 63 | class Rectangle extends Shape { 64 | 65 | double length; 66 | double width; 67 | 68 | public Rectangle(String color, double length, 69 | double width) { 70 | // calling Shape constructor 71 | super(color); 72 | System.out.println("Rectangle constructor called"); 73 | this.length = length; 74 | this.width = width; 75 | } 76 | 77 | @Override 78 | double area() { 79 | return length * width; 80 | } 81 | 82 | @Override 83 | public String toString() { 84 | return "Rectangle color is " + super.getColor() 85 | + "and area is : " + area(); 86 | } 87 | } 88 | 89 | /** 90 | * Here we implement abstraction with the help of interfaces 91 | */ 92 | class Bird implements AbstractionInterface { 93 | 94 | public int calculateDamage(int strength, double velocity, double angle) { 95 | return (int) ((int)strength*velocity); 96 | } 97 | } 98 | 99 | public class Abstraction { 100 | public static void main(String[] args) { 101 | Shape s1 = new Circle("Red", 2.2); 102 | Shape s2 = new Rectangle("Yellow", 2, 4); 103 | Bird bird = new Bird(); 104 | 105 | System.out.println(bird.calculateDamage(89, 110.11, 45)); 106 | System.out.println(s1.toString()); 107 | System.out.println(s2.toString()); 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /Java/Abstraction/AbstractionInterface.java: -------------------------------------------------------------------------------- 1 | interface AbstractionInterface { 2 | int calculateDamage(int strength, double velocity, double angle); 3 | } 4 | -------------------------------------------------------------------------------- /Java/Classes/Classes.java: -------------------------------------------------------------------------------- 1 | package Classes; 2 | /** 3 | * Class : A class is a group of objects which have common properties. It is a template or blueprint from which objects are created. It is a logical entity. It can't be physical. 4 | * 5 | */ 6 | class Vehicle { 7 | 8 | //data members, private in nature 9 | private String vehicleNumber; 10 | private float price; 11 | private String model; 12 | 13 | 14 | //getters and setters for data members 15 | public String getVehicleNumber() { 16 | return vehicleNumber; 17 | } 18 | 19 | public void setVehicleNumber(String vehicleNumber) { 20 | this.vehicleNumber = vehicleNumber; 21 | } 22 | 23 | public float getPrice() { 24 | return price; 25 | } 26 | 27 | public void setPrice(float price) { 28 | this.price = price; 29 | } 30 | 31 | public String getModel() { 32 | return model; 33 | } 34 | 35 | public void setModel(String model) { 36 | this.model = model; 37 | } 38 | 39 | 40 | @Override 41 | public String toString() { 42 | return "Vehicle [vehicleNumber=" + vehicleNumber + ", price=" + price + ", model=" + model + ", color=" + "]"; 43 | } 44 | 45 | } 46 | 47 | public class Classes { 48 | public static void main(String args[]) { 49 | //creating car object from its blueprint class 50 | Vehicle car = new Vehicle(); 51 | car.setModel("MX11"); 52 | car.setPrice(1000000); 53 | car.setVehicleNumber("1S23DDE"); 54 | 55 | System.out.println(car.toString()); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /Java/Classes/OOPs.java: -------------------------------------------------------------------------------- 1 | // Defining the OOPs class 2 | /** 3 | * 4 | * In Object-Oriented Programming (OOP), classes are used to define blueprints 5 | * or templates for creating objects. Objects, in turn, are instances of classes 6 | * and encapsulate both data (attributes or fields) and behavior (methods or 7 | * functions). Java is a popular programming language that fully supports OOP 8 | * principles, and you can create your own classes in Java to model and structure 9 | * your programs. 10 | * 11 | */ 12 | public class OOPs { 13 | // Attributes (data members) 14 | private String language; 15 | private int year; 16 | 17 | // Constructor to initialize attributes 18 | public OOPs(String language, int year) { 19 | this.language = language; 20 | this.year = year; 21 | } 22 | 23 | // Method to display information about the OOPs language 24 | public void displayInfo() { 25 | System.out.println("Language: " + language); 26 | System.out.println("Year of Introduction: " + year); 27 | } 28 | 29 | // Getter method for the language attribute 30 | public String getLanguage() { 31 | return language; 32 | } 33 | 34 | // Setter method for the language attribute 35 | public void setLanguage(String language) { 36 | this.language = language; 37 | } 38 | 39 | // Getter method for the year attribute 40 | public int getYear() { 41 | return year; 42 | } 43 | 44 | // Setter method for the year attribute 45 | public void setYear(int year) { 46 | this.year = year; 47 | } 48 | 49 | // Main method to demonstrate the OOPs class 50 | public static void main(String[] args) { 51 | // Create an instance of the OOPs class 52 | OOPs oopsLanguage = new OOPs("Java", 1995); 53 | 54 | // Display information about the OOPs language 55 | oopsLanguage.displayInfo(); 56 | 57 | // Update the year attribute 58 | oopsLanguage.setYear(1996); 59 | 60 | // Display the updated information 61 | oopsLanguage.displayInfo(); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /Java/Encapsulation/Encapsulation.java: -------------------------------------------------------------------------------- 1 | package Encapsulation; 2 | 3 | /** 4 | * Encapsulation in Java is a process of wrapping code and data together into a single unit, for example, a capsule which is mixed of several medicines. 5 | * We can create a fully encapsulated class in Java by making all the data members of the class private. Now we can use setter and getter methods to set and get the data in it. 6 | * The Java Bean class is the example of a fully encapsulated class. 7 | */ 8 | 9 | class Student { 10 | private String name; 11 | 12 | public void setName(String name){ 13 | this.name = name; 14 | } 15 | public String getName(){ 16 | return name; 17 | } 18 | } 19 | 20 | //A Java class to test the encapsulated class. 21 | public class Encapsulation{ 22 | public static void main(String[] args){ 23 | //creating instance of the encapsulated class 24 | Student s=new Student(); 25 | //setting value in the name member 26 | s.setName("vijay"); 27 | //getting value of the name member 28 | System.out.println(s.getName()); 29 | } 30 | } -------------------------------------------------------------------------------- /Java/Object/Object.java: -------------------------------------------------------------------------------- 1 | /* 2 | Object: 3 | An object is an instance of a class. You create objects from a class to 4 | work with the attributes and methods defined in that class. 5 | */ 6 | class Car { 7 | // Attributes or Fields 8 | String make; 9 | String model; 10 | int year; 11 | 12 | // Constructor 13 | public Car(String make, String model, int year) { 14 | this.make = make; 15 | this.model = model; 16 | this.year = year; 17 | } 18 | 19 | // Method to display car information 20 | public void displayInfo() { 21 | System.out.println("Make: " + make); 22 | System.out.println("Model: " + model); 23 | System.out.println("Year: " + year); 24 | } 25 | } 26 | public class Object { 27 | public static void main(String[] args) { 28 | // Creating objects of the Car class 29 | Car car1 = new Car("Toyota", "Camry", 2022); 30 | Car car2 = new Car("Honda", "Civic", 2023); 31 | 32 | // Accessing object attributes and methods 33 | car1.displayInfo(); 34 | System.out.println(); // Separating the two cars' information 35 | car2.displayInfo(); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Java/Polymorphism/CompileTime/CompileTimePolymorphism.java: -------------------------------------------------------------------------------- 1 | package Polymorphism.CompileTime; 2 | 3 | 4 | /** 5 | * Polymorphism in Java is a concept by which we can perform a single action in 6 | * different ways. Polymorphism is derived from 2 Greek words: poly and morphs. 7 | * The word "poly" means many and "morphs" means forms. So polymorphism means 8 | * many forms. 9 | * 10 | * There are two types of polymorphism in Java: compile-time polymorphism and 11 | * runtime polymorphism. We can perform polymorphism in java by method 12 | * overloading and method overriding. 13 | * 14 | * In method overloading, name of methods are same but there is change in 15 | * signature and return type 16 | * of methods which are present in same class 17 | * 18 | * 19 | * Run time polymorphism ---> Method overriding 20 | */ 21 | 22 | 23 | 24 | /** 25 | * Compile time polymorphism 26 | * Compile time polymorphism ---> Method overloading 27 | * 28 | * Compile-time polymorphism is also known as static polymorphism or early binding. 29 | * Compile-time polymorphism is a polymorphism that is resolved during the compilation process. 30 | * Overloading of methods is called through the reference variable of a class 31 | * 32 | */ 33 | class CompileTimePolymorphismExample { 34 | String firstName; 35 | String secondName; 36 | 37 | public void echoName(String name, int times) { 38 | for (int time = 1; time <= times; time++) { 39 | System.out.println("Echo name" + " " + name + " " + time + " " + "times"); 40 | } 41 | } 42 | 43 | public String echoName(String name) { 44 | return name; 45 | } 46 | } 47 | 48 | public class CompileTimePolymorphism { 49 | public static void main(String args[]){ 50 | CompileTimePolymorphismExample obj = new CompileTimePolymorphismExample(); 51 | obj.firstName = "Surya"; 52 | obj.secondName= "Prakash"; 53 | 54 | obj.echoName(obj.firstName, 5); 55 | System.out.println(obj.echoName(obj.secondName)); 56 | } 57 | } -------------------------------------------------------------------------------- /Java/Polymorphism/RunTime/RunTimePolymorphism.java: -------------------------------------------------------------------------------- 1 | package Polymorphism.RunTime; 2 | 3 | /** 4 | * Runtime polymorphism or Dynamic Method Dispatch is a process in which a call 5 | * to an overridden method is resolved at runtime rather than compile-time. 6 | * 7 | * In this process, an overridden method is called through the reference 8 | * variable of a superclass. The determination of the method to be called is 9 | * based on the object being referred to by the reference variable. 10 | */ 11 | 12 | class Parents { 13 | String father; 14 | String mother; 15 | 16 | public void habit() { 17 | System.out.println("Parents habit"); 18 | } 19 | } 20 | 21 | class Child extends Parents { 22 | public void habit() { 23 | System.out.println("Child habit"); 24 | } 25 | } 26 | 27 | public class RunTimePolymorphism { 28 | public static void main(String args[]){ 29 | 30 | Parents parents = new Parents(); 31 | parents.habit(); 32 | 33 | Parents child = new Child(); 34 | child.habit(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /JavaScript/Classes/Class.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @author dev-madhurendra 3 | * 4 | * 5 | * @class : In Object-Oriented Programming (OOP), a class is a blueprint or template 6 | * for creating objects (instances). It defines the properties (also known as 7 | * attributes or fields) and methods (functions) that the objects created from 8 | * the class will have. JavaScript, starting from ECMAScript 6 (ES6), introduced 9 | * support for classes, making it easier to work with OOP principles in JavaScript. 10 | * 11 | * Represents a basic shape. 12 | * @class 13 | */ 14 | class Shape { 15 | /** 16 | * Create a new shape. 17 | * @param {number} x - The X coordinate of the shape's position. 18 | * @param {number} y - The Y coordinate of the shape's position. 19 | */ 20 | constructor (x, y) { 21 | this.x = x 22 | this.y = y 23 | } 24 | 25 | /** 26 | * Get the X coordinate of the shape. 27 | * @returns {number} The X coordinate. 28 | */ 29 | getX () { 30 | return this.x 31 | } 32 | 33 | /** 34 | * Get the Y coordinate of the shape. 35 | * @returns {number} The Y coordinate. 36 | */ 37 | getY () { 38 | return this.y 39 | } 40 | 41 | /** 42 | * Move the shape to a new position. 43 | * @param {number} newX - The new X coordinate. 44 | * @param {number} newY - The new Y coordinate. 45 | */ 46 | move (newX, newY) { 47 | this.x = newX 48 | this.y = newY 49 | } 50 | } 51 | 52 | export default Shape 53 | -------------------------------------------------------------------------------- /JavaScript/Constructor/Constructor.js: -------------------------------------------------------------------------------- 1 | class Person { 2 | constructor(name, age) { 3 | this.name = name; 4 | this.age = age; 5 | } 6 | 7 | sayHello = () => { 8 | console.log(`Hello, my name is ${this.name} and I'm ${this.age} years old.`); 9 | } 10 | } 11 | 12 | // Creating an object using the class constructor 13 | const person1 = new Person("John", 30); 14 | console.log(person1.sayHello()); 15 | const person2 = new Person("Alice", 25); 16 | console.log(person2.sayHello()); 17 | 18 | 19 | -------------------------------------------------------------------------------- /JavaScript/Object/Object.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @Explanation 3 | * Objects are instances of classes or constructors in OOP. 4 | * In JavaScript, objects are a fundamental data type, and they can store 5 | * both data (properties) and behavior (methods). 6 | * 7 | */ 8 | 9 | // Object with properties 10 | const Person = { 11 | firstName: "John", 12 | lastName: "Doe", 13 | age: 30, 14 | greet: function () { 15 | console.log(`Hello, ${this.firstName} ${this.lastName}`); 16 | } 17 | }; 18 | 19 | // Accessing object properties 20 | console.log(Person.firstName); // "John" 21 | Person.greet(); // Outputs: Hello, John Doe 22 | -------------------------------------------------------------------------------- /JavaScript/Object/ObjectEx2.js: -------------------------------------------------------------------------------- 1 | class Student { 2 | constructor(firstName, lastName, id) { 3 | this.firstName = firstName 4 | this.lastName = lastName 5 | this.id = id 6 | } 7 | 8 | getName() { 9 | return this.firstName + " " + this.lastName; 10 | } 11 | 12 | getStudentDetail() { 13 | return "Student is " + this.firstName + " " + this.lastName + "\n id is " + this.id; 14 | } 15 | } 16 | 17 | const student1 = new Student("Madhurendra Nath " , "Tiwari" , 1); 18 | console.log(student1.getStudentDetail()); -------------------------------------------------------------------------------- /Python/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dev-madhurendra/OOPs/97ca90e99766f4e022f2c14e297055c7e49ddd65/Python/.gitkeep -------------------------------------------------------------------------------- /Python/Class/Class.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Class : 3 | 1- Class is a user-define data type. It consist of data members and member function. 4 | 2- Class gives the blueprint structure that how the memory will get allocated when object of class get created. 5 | 3- In python for creating class we use keyword "class" along with the name of class what you want. 6 | Object : 7 | 1- Object is a real world entity which used to access class functionality. 8 | 2- Memory gets allocated only when object get created. 9 | ''' 10 | 11 | 12 | # Here we define a new class named "Bike" 13 | class Bike: 14 | # This is data member/attribute of class. 15 | # This will get attached with every object of this class. 16 | speed = 32 17 | 18 | # This is member function of this class. 19 | # In member function first parameter always reference to the object through which this member function is called. 20 | # Here I give name "self" reference to object, You can give any name what you want. 21 | def showVariable(self): 22 | 23 | # for accessing data members of object we need to use reference of that object 24 | print(self.speed) 25 | print(self.mielage) 26 | 27 | 28 | # This is how we create object of Class. 29 | obj1 = Bike() 30 | 31 | # We can also define some attribute of object like this. This attribute only belongs to only this object. 32 | obj1.mielage = 13 33 | 34 | # Here we call member function of class 35 | obj1.showVariable() 36 | 37 | 38 | # Here we created one more object of class First 39 | obj2 = Bike() 40 | 41 | ''' 42 | When we call this function of class using object "obj2" then you will get error because 43 | in this function we try to print some attribute value which does not belong to object "obj2" 44 | ''' 45 | obj2.showVariable() 46 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OOPs 2 | 3 | This repository is your one-stop resource for exploring Object-Oriented Programming (OOP) concepts in different programming languages. 4 | 5 | ![OOPs](https://github.com/dev-madhurendra/OOPs/assets/68775519/89bfad58-2511-433c-b29e-667ce56cf6be) 6 | 7 | 8 |
9 | 10 | [![Discord chat][chat]][discord-server] 11 | 12 |
13 | 14 | Before contributing to this repository, make sure to read our [Contribution Guidelines](CONTRIBUTING.md). Our maintainers will guide you through how to make your contribution properly if you make any mistakes. The names of the maintainers of this repository are listed in the [CODEOWNERS file](.github/CODEOWNERS). 15 | 16 | You can find a list of the language oops concepts currently in the repository in the [directory](DIRECTORY.md). Explanations of 17 | many of the algorithms can be found in the [wiki][explanation]. 18 | 19 | ## One Concept per PR 20 | 21 | We follow a strict guideline in this project: **One Concept per Pull Request (PR)**. When contributing code or changes, please ensure that each PR focuses on a single concept, feature, or bug fix. 22 | 23 | This guideline helps maintain code clarity, simplifies code reviews, and ensures that each PR is well-contained and easy to understand. It also allows for more straightforward testing and troubleshooting. 24 | 25 | When creating a PR, make sure to: 26 | 27 | - Clearly describe the concept, feature, or fix addressed by the PR. 28 | - Avoid bundling unrelated changes in a single PR. 29 | - Keep the scope of the PR limited to the chosen concept. 30 | 31 | By following this guideline, we can maintain a clean and organized codebase that is easier to maintain and collaborate on. 32 | 33 | ## Branch Naming Convention 34 | 35 | In this project, we follow a specific branch naming convention to organize code related to different programming languages and Object-Oriented Programming (OOP) concepts. Branch names should follow the format: 36 | 37 | - `[DirName]`: The name of the directory or programming language you are working with. For example, `Java`, `Cpp`, `Python`, etc. 38 | - `[Oops-Concept]`: The specific OOP concept you are exploring or implementing. For example, `Inheritance`, `Polymorphism`, `Encapsulation`, etc. 39 | 40 | Here are some examples of valid branch names: 41 | 42 | - `Java_Multiple-Inheritance` 43 | - `Cpp_Class` 44 | - `Python_Polymorphism` 45 | 46 | By following this naming convention, it becomes easier to track and manage branches related to different programming languages and OOP concepts within the project. 47 | 48 | ## Thanks to all the contributors ❤️ 49 | 50 | 51 | 52 | 53 | 54 |

55 | devmadhurendra 56 | dev-madhurendra 57 | dev.madhurendra 58 | dev.madhurendra 59 | https://discord.gg/bUHuNRq7 60 |

61 | 62 | 63 | 64 | [chat]: https://img.shields.io/discord/808045925556682782.svg?logo=discord&colorB=7289DA 65 | 66 | 67 | [discord-server]: https://discord.gg/bUHuNRq7 68 | 69 | -------------------------------------------------------------------------------- /TypeScript/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dev-madhurendra/OOPs/97ca90e99766f4e022f2c14e297055c7e49ddd65/TypeScript/.gitkeep -------------------------------------------------------------------------------- /TypeScript/Classes/Shape.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * @author dev-madhurendra 3 | * 4 | * 5 | * @class : In Object-Oriented Programming (OOP), a class is a blueprint or template 6 | * for creating objects (instances). It defines the properties (also known as 7 | * attributes or fields) and methods (functions) that the objects created from 8 | * the class will have. JavaScript, starting from ECMAScript 6 (ES6), introduced 9 | * support for classes, making it easier to work with OOP principles in JavaScript. 10 | * 11 | * Represents a basic shape. 12 | * @class 13 | */ 14 | class Shape { 15 | x: number 16 | y: number 17 | /** 18 | * Create a new shape. 19 | * @param {number} x - The X coordinate of the shape's position. 20 | * @param {number} y - The Y coordinate of the shape's position. 21 | */ 22 | constructor(x: number, y: number) { 23 | this.x = x 24 | this.y = y 25 | } 26 | 27 | /** 28 | * Get the X coordinate of the shape. 29 | * @returns {number} The X coordinate. 30 | */ 31 | getX() { 32 | return this.x 33 | } 34 | 35 | /** 36 | * Get the Y coordinate of the shape. 37 | * @returns {number} The Y coordinate. 38 | */ 39 | getY() { 40 | return this.y 41 | } 42 | 43 | /** 44 | * Move the shape to a new position. 45 | * @param {number} newX - The new X coordinate. 46 | * @param {number} newY - The new Y coordinate. 47 | */ 48 | move(newX: number, newY: number) { 49 | this.x = newX 50 | this.y = newY 51 | } 52 | } 53 | 54 | export default Shape 55 | --------------------------------------------------------------------------------