TECHOREVIEW

Real-Life Examples to Learn Object Oriented Programming

Spread the love
Object-Oriented Programming is the most used approach in the latest programming languages. Learning object-oriented programming is challenging to understand. Most students want to learn Object-oriented programing, but they will not find an easy way to understand it. This may be due to the language barrier. Learning concepts through real-world examples is the best and easy way to understand concepts. In this article, I will give you many real examples to understand complete Object-Oriented Concepts by just reading this article. There is a total of five main pillars of Object-Oriented Programing:  
  1. Class and Objects: If you want to learn OOP concepts, the first concept is Class and objects based on all other concepts. In simple words, every solid object in the world you can touch will be made class in OOP. In the above picture, I take an example of animals. We can touch animals as a class in OOP, and all other animal types behave like class objects. You will know data types and functions if you know any programming language. Data types in oop are called properties of the class, and Functions in oop are called Members functions of a class. This example will be implemented in oop are as follows:
    class Animals {       // The class
      public:             // Access specifier
        string name;      // Attribute (string variable)
        int weight;       // Attribute (int variable)
    
    void getname() {
          cout << "Elephant is beautiful\n" ;
        }
    };
    
    int main() {
      Animals elephant;  // Create an object of Animals
    
      // Access attributes and set values
      elephant.name = "African Elephant"; 
      elephant.weight = 700;
    
    elephant.getname();
    
      // Print attribute values
      cout << elephant.name << "\n";
      cout << elephant.weight;
      return 0;
    }
    In another example, let’s take the example of cars. You see and touch cars so it can make a class in OOP. Class properties will make data members, and its function is called Member functions. You can make a car of all color types by changing objects without changing the code. Let’s take another real-world example from your daily routine. You will place an order for fast food; the agent will pick and deliver the order to customers’ locations when it comes to the hotel. Class of order will be called, and 3 objects and methods of class order will be called as you see in the above example. By using OOP, you can call class anywhere when you needed. It’s only possible with the OOP approach by using Class and objects. The last and exciting example to understand class and objects is the mobile phone, which you can use many times in a day. As mobile is an object you can touch will make a class, and mobile phone belongs to company ‘Apple’ so we can see male Apple, a class in OOP. Mobile contains many Data members and Member functions such as Color, Price, Model No are, Data Members, and Call() and Video and Member Functions. You can quickly call any of these by using the class object in the main function.
  2. Inheritance: After learning and understanding Class and objects in oop, the following central concept is “Inheritance.” It is used for code reusability. You are familiar with the term inheritance. When your parents are dead, all the property that belongs to your parents will be yours. It means you inherit all the things your parents own will be yours. The same concept will also be used in the OOP approach. All the things belonging to a class can easily be inherited into another class. An example of the above picture will be implemented in C++ as follows:
    // Base class
    class Property {
      public:
        string name = "All my parents property will be mine";
        void getproperty() {
          cout << "I am the owner of all property of my parents \n" ;
        }
    };
    
    // Derived class
    class Myproperty: public Property {
      public:
        string home = "Parents Home";
    };
    
    int main() {
      Myproperty children;
      children.getproperty();
      cout << children.name + " " +children.home;
      return 0;
    }
    
    Another example to understand Inheritance is “You.” All the qualities and characteristics you inherit from your parents are inherited. For Example, if your eyes or hair color matches your parent’s hair color and eye, it is an inherited property. Inheritance works the same in OOP. All the data members and member functions of one class that you can inherit in another class are called inheritance in OOP.
  3. Polymorphism:
Polymorphism is the third pillar in the OOP approach. Using polymorphism, you can perform different operations using only one method or function. This is the most used real-world example to learn polymorphism. In this example, a speak function called and the same function all other class will speak but in other accent and his voice. For example, a speech function from the human class speaks in the voice of a human, and speak function from the dog class speaks in the dog’s voice. You can quickly implement that functionality using Polymorphism wherever you want in your programming language. This is called Polymorphism.
class Animal {
  public:
    void animalSound() {
    cout << "The animal makes a sound \n" ;
  }
};

// Derived class
class Pig : public Animal {
  public:
    void animalSound() {
    cout << "The pig says: wee wee \n" ;
  }
};

// Derived class
class Dog : public Animal {
  public:
    void animalSound() {
    cout << "The dog says: bow wow \n" ;
  }
};
In the above example, a BowlingMethod() from the parent class tells us that you can use this method to bowl to a player in a cricket match. But I bowl is not specified; I can bowl fast, medium or spin to the player. This is my choice. The same approach is used in OOP; you can use only one function of parent classes in all child classes with the same method to operate. But in the child class of FastPacer, the BowlingMethod() will bowl a fast delivery, and in MediumPacer class, a BowlingMethod() will bowl a medium delivery. In this example, a security guard behaves in three ways. Security guards must make sure security of a place. He makes a place secure but in three ways. He checks ID cards, Welcome admin persons, and secure admin persons. If we want to implement this example using the oop approach, we can make a class named “Security” and make a function named “secure().” In other child classes such as IDCheck, a secure() function is called, but he checks id, and in SecureAdmin class, a secure() function will secure admin people. In this example, a person must cut. But cut of what type is not defined. When a person is a surgeon, he makes a cut to a body for medical operation, if a person is a hairstylist, he will cut your hair for hair cutting, and if a person is a director or actor of a movie, he will cut scenes from a movie. Same as in OOP, the cut function of the main class behaves differently in all other child classes. In this example, if I want to save my friend’s contact with his two numbers, it is an example of polymorphism. createContact() method is the same, but it will behave differently when called with two different data types.
  1. Abstraction:
Abstraction is a fundamental concept in the OOP approach. You can easily hide all unnecessary details from the user; the above example shows only a car with a body is an example of Abstraction. You will not know about how a car and its engine works. He wants to know how to operate a car. This concept is used when you want your customer to know only how the product will work. You can quickly implement this concept using the OOP approach in your programming language. You can implement the abstraction concept by using access specifiers of Public, Private and Protected.
class implementAbstraction
{
            private:
                string engineworking;
                string carworking;
            public:
                        // method to set values of
                        // private members
                        void set(int x, int y){
                                    engineworking = x;
                                    carworking = y;
                        }
                        void display()
                        {
                                    cout<<"Engine Working = " <<engineworking << endl;
                                    cout<<"Car Working = " << carworking << endl;
                                }
}
int main()
{           
      implementAbstraction obj;
            obj.set("Engine Runs","Car Runs");
            obj.display();
            return 0;
}
In this example, the user will know how to run a car but not how it and its engine run. This can be implemented using abstraction. Without abstraction, users can know and check all the details of how the car and its engine run. In this example, the user knows how the smartphone works, sees, and uses the smartphone but does not know the working and implementation details.
  1. Encapsulation: Encapsulation is the fifth and last pillar of the OOP approach, where you can secure your data in a better way. In abstraction, people cannot see implementation details. In encapsulation, people can see and use it but cannot change any data member and function. You can secure your data using encapsulation. You can use getters and setters to perform encapsulation.In this example, people can see the model but cannot change the model data of a car. For example, the data you will not give access to the user to change, but you give access to use is called encapsulation.
    class Car {
      private:
        // Private attribute
        int model;
    
      public:
        // Setter
        void setmodel(int m) {
          model = m;
        }
        // Getter
        int getmodel() {
          return model;
        }
    };
    
    int main() {
      Car carmodel;
      carmodel.setmodel(50);
      cout << myObj.getmodel();
      return 0;
    }
    This example shows all the details and implementation of smartphones using encapsulation, but people only use a smartphone’s change data. Users cannot put new details about the smartphone. https://www.techoreview.com/cloud-security-framework/

Leave a Comment

Your email address will not be published. Required fields are marked *