Here we have last minute revision notes of object oriented programming language. These questions will familiarize you with the most important object-oriented programming concepts and help you ace your job interviews 🙌
- Object-oriented programming is a programming paradigm built on the concept of objects.
- In Other Words, it is an approach to problem-solving where all computations are carried out using objects.
Class: A class is the building block that leads to Object-Oriented programming. It is a user-defined datatype, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class.
Object: An Object is an instance of a Class. When a class is defined, no memory is allocated but when it is instantiated (i.e. an object is created) memory is allocated.
- Constructors are special class members which are called by the compiler every time an object of that class is instantiated.
- Constructors have the same name as the class looks like method and does not have return type.
- If we provide return type to constructor it becomes a method.
1. Default constructors
2. Parameterized constructors
3. Copy constructors
- Default Constructor: Default constructor is the constructor which doesn’t take any argument. It has no parameters.
- Parameterized Constructor: A constructor is called Parameterized Constructor when it accepts a specific number of parameters.
- Copy Constructor: A copy constructor is a member function which initializes an object using another object of the same class.
- Constructor has the same name as the class itself.
- Constructors don’t have a return type.
- A constructor is automatically called when an object is created.
- It must be placed in the public section of class.
- If we do not specify a constructor, C++ compiler generates a default constructor for object (expects no parameters and has an empty body).
- Constructors can be overloaded.
- Constructor cannot be declared virtual.
- A destructor is also a special member function as a constructor. Destructor destroys the class objects created by the constructor.
- Destructor has the same name as their class name preceded by a tiled (~) symbol.
- Destructor is invoked automatically by the compiler when its corresponding constructor goes out of scope and releases the memory space that is no longer required by the program.
- Destructor neither requires any argument nor returns any value therefore it cannot be overloaded.
- Destructor cannot be declared as static and const.
- Destructor should be declared in the public section of the program.
Inheritance is one of the most important features of Object-Oriented Programming. The capability of a class to derive properties and characteristics from another class is called Inheritance.
1. Single Inheritance
2. Multiple Inheritance
3. Multilevel Inheritance
4. Hierarchical Inheritance.
5. Hybrid Inheritance.
- Single Inheritance: When a subclass(child) is inherited from a base class is called single inheritance.
- Multiple Inheritance: when one subclass is inherited from more than one base class is called multiple inheritance.
- Multilevel Inheritance: In this type of inheritance, a derived class is created from another derived class.
- Hierarchical Inheritance: In this type of inheritance, more than one subclass is inherited from a single base class.
- Hybrid Inheritance: The inheritance in which the derivation of a class involves more than one form of any inheritance is called hybrid inheritance. Basically C++ hybrid inheritance is combination of two or more types of inheritance. It can also be called multi path inheritance.
- In normal term encapsulation is defined as wrapping up of data and information under a single unit.
- Encapsulation define as binding together the data and function that manipulates them.
- Increased security of data.
- Encapsulation allows access to a level without revealing the complex details below that level.
- It reduces human errors.
- Makes the application easier to understand.
- Data Abstraction is one of the most essential and important feature of Object Oriented Programming in c++.
- Abstraction means displays only the relevant attributes of objects and hides the unnecessary details like the background details and implementation.
- Helps user to avoid writing the low level code.
- Avoids code duplication and increases reusability.
- Helps to increase security of an application or program as only required details are provided to the user.
- The word polymorphism means having many forms. Polymorphism occurs when there is a hierarchical mode inheritance.
- C++ polymorphism means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function.
1. Compile time Polymorphism
2. Run time Polymorphism
- Compile-time polymorphism is a polymorphism that is, the function call is resolved during the compilation process.
- We can achieve Compile-time polymorphism by two ways:
- Function overloading :
- When there are multiple functions with the same name but take different parameters as an arguments then these function are said to be overloaded.
- Functions can be overloaded by changing the number of arguments or and changing the type of arguments.
- Operator Overloading:
- C++ also provides the option to overload operators So a single operator ‘+’, when placed between integer operands, adds them and when placed between string operands, concatenates them.
- Runtime polymorphism is also known as dynamic polymorphism or late binding. In runtime polymorphism, the function call is resolved at run time.
- This type of polymorphism is achieved by Function Overriding or Virtual function.
- Sometimes implementation of all function cannot be provided in a base class because we don’t know the implementation. Such a class is called abstract class. Example, let Shape be a base class. We cannot provide implementation of function draw() in Shape, but we know every derived class must have implementation of draw().
- Class is Abstract, if we have atleast one pure virtual function.
- Interface looks like class
- It does not have constructor
- We can not create the object of the interface
- Use implement keyword to implements interface to class
- Use to achieve multiple inheritance
- Also called Absract function.
- A pure virtual function in c++, is a virtual function for which we can have implementation, but we must override that function in the derived class, otherwise the derived class will also become abstract class.
- Private – The access level of a private modifier is only within the class. It cannot be accessed from outside the class.
- Default – The access level of a default modifier is only within the package. It cannot be accessed from outside the package. If you do not specify any access level, it will be the default.
- Protected – The access level of a protected modifier is within the package and outside the package through child class. If you do not make the child class, it cannot be accessed from outside the package.
- Public – The access level of a public modifier is everywhere. It can be accessed from within the class, outside the class, within the package and outside the package.