abdude824's blog

By abdude824, history, 13 months ago, In English

OOPs are actually very simple but we can have some difficult questions on OOPs as well. We will first have short notes of theory(which can be asked in form of questions) and then shift to questions.

I am refering E balaguruswamy book for this. These are short notes and may miss something, if you think something is missing please comment. Also, we would be using this track:

  1. Introduction to classes and objects
  2. Constructors and Destructors
  3. Operator Overloading
  4. Inheritance
  5. Polymorphism

We will be discussing major topics here and actually difficult ones. You must know basic OOPs.


C structures Vs C++ Classes
General structure of classes
Visibility labels(Public, private, protected
Some terms and their meaning
For CP lovers, here is class based implementation of segment trees and Euler tours, representing inheritance as well(although we don't actually need inheritance here)
Memory allocation of member variables and functions accross objects

Static Data members

As we saw in memory allocation diagram, copies of member variables are made for different objects. But if we want a common variable for all objects of class? For example in an employee class, we need to find the number of employees at a specific time. We can do it using a global variable but that variable can interfere in the whole working of program. We can use a static data member. Static members are initialized to 0 and one important thing to note is that type and scope of static variable is defined outside the class. This is because they are not of every object but are unique to a class. And where we have variables, we do have functions also. Static member functions are also specific to a class, and can access only static members. Please have a code for implementation of both(Here I have kept the static member function outside to generalize the concept that static members are specific to class but they can be defined and implemented inside the class as well)

Sample Code for static data members

All other operations like making an array, vector, or any other STL container works the same for classes(Although you have to make a custom comparator in case of sorting through STL) remains almost same. We can also pass objects as arguments which is self explanatory.

Friendly Functions

There can be a case when we want some function to access private members of a class. For example, suppose there are two classes, Software engineers and Software testers. We want to find maximum income between both of the classes(Although many testers may not be eligible to pay taxes in India(Minimum salary is 5LPA for tax payers)). But we don't want to use the private member "income" as we can't access private members using objects directly. So, what we do is we make a friend function which can access private members of both the classes outside of both of them. This way, it can access both the classes without actually being a part of the class. (Obviously, we have to define that a particular function is friend inside the class as it would be a security threat if any function can be friend)

Code for Friend Functions

If a member function do not alter any data, we put a const ahead of its definition: void changeNothing() const;

Pointers to members of a class

Suppose this is the class:

class A{
    int x;

We define a pointer to X as: int A ::* it=&A :: m;

Now, it contains address of m and we can refer to m using *it.

Constructors and Destructors


Special member functions having the same name as the class used to initialize objects. Whenever a new object is created, this member function is called.

Constructor example in segment tree implementation

Constructors can be parametrized or without any parameters as well. Constructors without any parameters are default constructors. Please note that if a parameterized constructor is present and no default constructor is present, this type of initialization will give you an error: segTree s; because compiler couldn't find any default constructor. But if no constructor is present, compiler supplies one.

Some properties of constructors:

The parameters of a constructor can be of any type except for that class only but it can accept a reference to the class. These constructors are called copy constructors. Copy Constructors can be used to create two objects with same member variables.

Example of Copy Constructors

We can also use constructors dynamically. That is, we can use them after initialization of objects also.

Code for Dynamic Initialization of Objects


A destructor, as name suggests, is used to delete objects that have been created by a constructor. As we saw in memory allocation, each object has its own memory for its member variables. Suppose we have a vector and we are resizing it inside the constructor. Then we should use a destructor as well to free that memory. (Not in general, but in big firms, yes its necessary)

Destructor Example

Operator Overloading

This is perhaps the most interesting part of OOPs. As we saw initially, we can't do a (+) operation on structures, but we can do it in classes using operator overloading. For example, we can use a (+) operator for strings to concatenate them in C++. Similarly, we can modify the meaning of an operator for a class. The possibilities with this are immense. Just imagine, you can actually add maps, sets according to the meaning interpreted by you. You can practically create your own language with this feature. But there are some operators you can't overload.

List of operators you can't overload

Please note that the precedence of the operators don't change. Multiplication will have a higher precedence than addition.

Let's Overload some Unary and Binary Operators!

  1. Unary Operator:
Example of Unary operator, in a class complex, which is like an actual complex number

Now, you might be having a feel how all the STL containers are made. And that's why OOPs are hell awesome!

Now let's overload a binary operator.

Overloading subtraction operator

Left hand operand is used to invoke the operator and right hand operand is passed as an argument.

Diagram representing the operator overloading process

As we used a friend function in dealing with "<<" operator, we have to use a friend member for dealing with any operator in which any one operator is not of the same type.

Type Conversion

We all know type conversions very well (Assuming you are familiar with C++). We use type conversions to convert a double to int, or a character to integer. Although it's not extreme necessary, but it saves some time. These are the three cases we can have :

  1. Basic to User defined class.
  2. User defined class to Basic
  3. User defined to another user defined

To be updated


Here comes the interesting part of OOPs. With this we can actually support reusability. It's quite easy to understand as well.

1. Types of Inheritance
2. Access modifiers in Inheritance
3. Ambiguities in Inheritance
4. Virtual base classes and their applications
5. Abstract classes and real life application
6. Nesting classes

If you want to see the real life application of inheritance, see this code snippet and working here(links would be attached soon). (It was made by me so, maybe a bit clumsy at first look, but see the working, it's my new project(The full project will be uploaded soon))


To be updated

  • Vote: I like it
  • +80
  • Vote: I do not like it

13 months ago, # |
  Vote: I like it -7 Vote: I do not like it

Thanks, your blogs help a lot! Please update the Operating System blog as well.

13 months ago, # |
  Vote: I like it 0 Vote: I do not like it

I am sure this took a lot of effort, and it is really nice that you are willing to put so much of effort into this stuff. Maybe you can start your own blog/github page where you put all of these notes which can help people who need them, but CodeForces is a competitive programming platform, so it is quite irrelevant here. The term "blog" can be a bit misleading.

  • »
    13 months ago, # ^ |
      Vote: I like it -12 Vote: I do not like it

    Yeah, I actually wanted to post it on my own blogspot. But as my intern season is going on, I don't want to go to development again. I will just make another website and post these there when I get time(Maybe in January) but I am just posting these here only because of the great markdown codeforces provide. These are kind of my personal notes. I am just giving them public for last day revision for others. And I agree with the fact that this is a Competitive programming platform, but come on, I get a great markdown here. And people generally doing competitive programming are searching for jobs for which these subjects are important, so maybe irrevelant for some of the people, but helpful for others as well.

  • »
    13 months ago, # ^ |
      Vote: I like it +4 Vote: I do not like it

    While I agree that OOP isn't too relevant to cp, I have seen a lot of cf blogs that are way less useful/productive than this one.

13 months ago, # |
  Vote: I like it 0 Vote: I do not like it

I am by no means an OOP expert but I don't understand the need for the Tree class to inherit from SegTreeclass. Wouldn't it be much simpler if you removed the "inheritance" and just kept a SegTree instance inside the Tree class? Seems like a forced and totally un-idiomatic usage of inheritance.

Also as suggested by naman1601, OOP is not very relevant to competitive programming.

13 months ago, # |
  Vote: I like it -17 Vote: I do not like it

Thanks a lot for the precise compilation, it will be very helpful. Please also add Computer Network & DBMD Notes and Design Patterns as well as System Design.

13 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Added to favorite . Plz continue updating. Thanks!

13 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by abdude824 (previous revision, new revision, compare).

13 months ago, # |
  Vote: I like it +5 Vote: I do not like it

Operator overloading works on C++ structs almost the same way they do on classes. link

Nice blog though

  • »
    13 months ago, # ^ |
    Rev. 3   Vote: I like it 0 Vote: I do not like it

    Ohh, I didn't know that. Thanks for sharing! Actually I was referring to E. Balaguruswamy book. Operator overloading in structs is not possible in C. I forgot to mention that. Thanks again.

    UPD: Fixed.

13 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Oops concept may not be relevant to CP. but it is relevant to any coder in general.

7 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by abdude824 (previous revision, new revision, compare).