OOPS  in C++

OOPS in C++

·

4 min read

what is oops?

oops is the abbreviation of object-oriented programming. Object-oriented programming (OOP) is a way of thinking about and organizing code for maximum reusability. Object-oriented programming, simply means that a program comprises objects that can interact with the user, other objects, or other programs. This makes programs more efficient and easier to understand.

Basic Concepts in Object-Oriented Programming

  • Classes - basic template for creating objects.

  • Objects - basic runtime entities.

  • Data Abstraction & Encapsulation - Wrapping data and functions into a single unit.

  • Inheritance - Properties of one class can be inherited by others.

  • Polymorphism - ability to take more than one form.

  • Dynamic Binding - code that will execute is not known until the program runs.

  • Message Passing - Object. message(information) call format.

Classes

we are going to start from classes.so C++ was initially called 'c with classes' by Stroutstroup. why classes are introduced? because the structure has some limitations like members are public and no methods. sometimes u just want to include some members and at the same time you also want that it should not be publicly accessible and by special calling only it should be accessible otherwise not and that's why classes are introduced as it is more protective

classes contain:

  • structure + more

  • methods and properties

  • can make a few members public, private, and protected

what is a private member?

The class members declared as private can be accessed only by the member functions inside the class. They are not allowed to be accessed directly by any object or function outside the class. Only the member functions or the friend functions are allowed to access the private data of members of the class

what is a public member?

The class member can be accessed from anywhere.

what is a protected member?

Protected members are accessible in the class that defines them and in classes that inherit from that class. and also accessible by friends of its class, and by friends of its derived classes.

syntax of classes in c++

class class_name
{
 private:
  //if you don't declare any specifier, the member automatically becomes private
 public:
  //type any code
 };

objects

objects can be declared along the classes. It is a run-time entity. Once the object is created, it can be used to access the data members and functions of that class

syntax of objects in c++

class_name obj_name;
obj_name.a = xyz;

//example
int main()
{
  vehicle car;
  car.a=green;
  car.b =red;
  car.c=blue;
}

what is the friend function?

A friend function is a function that isn't a member of a class but has access to the class's private and protected members. it is not considered a class member. it is just a normal member function. it just has a privileged to access a friend function.

Inheritance

As mentioned above, the properties of one class can be inherited by others.

  • base class = a base class is an existing class from which the properties are inherited into other class

  • Derived class =A derived class is a class that is constructed from a base class. it acquires all the properties of the base class.

    syntax of inheritance in c++

      class base_class_name
      {
        public:
        //code
      };
      class derived_class_name : visibility_mode  base_class_name
      {
        //code
      };
    

    Note:

  • default visibility mode is private

  • public visibility mode: public members of the base class becomes a public member of the derived class

  • private visibility mode: public members of the base class becomes private in the derived class

  • private members never inherited

Data Abstraction

data abstraction refers to providing only essential information to the outside world and hiding their background information.

In classes, they provide sufficient private methods in which you can hide the implementation from code that uses the type.

Polymorphism

As the name suggests one name and multiple forms

There are two types of polymorphism:

  1. compile time polymorphism

    - Function overloading

    -Operator overloading

  2. Run time polymorphism

    -virtual function

Encapsulation

As we have seen in class we can bundle data members and functions that operate together inside a single class.

Encapsulation is nothing but a process of wrapping similar code in one place.

you can consider the below example

#define pi = 3.14
class circle{
public:
int radius;
int getarea(){
  return pi * radius * radius;
}
};

hence, the data member is a radius and the function is getarea. both are wrapped under a circle.