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 suing this track:
- Introduction to classes and objects
- Constructors and Destructors
- Operator Overloading
- Inheritance
- Polymorphism
We will be discussing major topics here and actually difficult ones. You must know basic OOPs.
Introduction
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)
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)
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{
private:
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.