Mastering Object-Oriented Programming (OOPs) for SDE Interviews | Ultimate Resource

 C++ Classes and Objects

C++ is an object-oriented programming language. 
Everything in C++ is associated with classes, objects, attributes, and methods.
Attributes and methods are basically variables and functions that belong to the class. These are often referred to as "class members".
A class is a user-defined data type that we can use in our program, and it works as an object constructor or a "blueprint" for creating objects.

Creating a Class & an Object

To create a class, use the `class` keyword:
In C++, an object is made from a class.
To create an object, specify the class name, followed by the object name.
To access the class attributes, use the dot syntax (.) on the object.
In MyClass obj = MyClass();, MyClass() is an Explicit type conversion in functional notation, so it is a temporary object because it falls under "conversion that creates a prvalue".

#include <bits/stdc++.h>
using namespace std;

class MyClass{ // class keyword and Class Name
    public:  // Access Specifier
        int myNum; // Attribute
        string myString; // Attribute
};

int main(){
    MyClass obj = MyClass();
    obj.myString = "CodingKaro.in";
    cout<<(obj.myString);
    return 0;
}
Output: CodingKaro.in

Class Methods

Methods are function that belongs to the class. They define the behavior/action taken by the object.
There are two ways to define functions that belong to the class:

#include <bits/stdc++.h>
using namespace std;

class MyClass{ // class keyword and Class Name
    public:  // Access Specifier
        int myNum; // Attribute
        string myString; // Attribute

        void myMethod(){ // Method
            cout<<"Welcome to CodingKaro.in"<<endl;
        }

        void myMethod2(); // Method Declaration
};

void MyClass::myMethod2(){
    cout<<"Inside myMethod2"<<endl;
}

int main(){
    MyClass obj = MyClass();
    obj.myString = "CodingKaro.in";
    obj.myMethod2();
    obj.myMethod();
    return 0;
}
Output:   Inside myMethod2
                Welcome to CodingKaro.in

Access Specifiers

Access specifiers define how the members (attributes and methods) of a class can be accessed:
In C++, there are three access specifiers:
public - members are accessible from outside the class
private - members cannot be accessed ( or viewed ) from outside the class.
protected - members cannot be accessible from outside the class, however, they can be accessed in the inherited class.


By default, all members of a class are private if you don't specify an access specifier.



Default Copying

By default, objects can be copied. In particular, a class object can be initialized with a copy of an object of its class. By default, the copy of a class of objects is a copy of each member.
If that default is not the behavior wanted for a class X, then appropriate behavior can be provided using overloading.


Static Members of a C++ Class


We can define a class member static using the static keyword.
When we declare a member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member.
A static member is shared by all objects of the class.
Memory belongs to object not to class.
We can't put it in the class definition but it can be initialized outside the class as done in the following example by redeclaring the static variable, using the scope resolution operator ::  to identify which class it belongs to.

#include <bits/stdc++.h>
using namespace std;

class MyClass{ // class keyword and Class Name
    public:  // Access Specifier
        static int myNum; // Static Member
        string myString; // Attribute

};

int MyClass::myNum = 0; // initialize static members of class

int main(){
    cout<<MyClass().myNum<<endl;
    MyClass().myNum+=10;
    MyClass obj = MyClass();
    cout<<obj.myNum<<endl;
    return 0;
}
Output:   0
                10

Static Function Members

By declaring a function member as static, you make it independent of any particular object of the class.
A static member function can be called even if no objects of the class exist and the static functions are accessed using only the class name and the scope resolution operator ::.
 A static member function can only access static data members, other static member functions, and any other function from outside the class.
static member functions have a class scope and they do not have access to this pointer of the class.

#include <bits/stdc++.h>
using namespace std;

class MyClass{ // class keyword and Class Name
    int length;
    public:  // Access Specifier
        static int myNum; // Static Member
        static int get(){
            return myNum;
        }
};

int MyClass::myNum=100;

int main(){
    cout<<MyClass().get()<<endl;
    MyClass().myNum+=10;
    MyClass obj = MyClass();
    cout<<obj.get();
    return 0;
}

Output:  100
                110

Passing and Returning objects in C++

In C++ we can pass objects as arguments and also return them from functions the same way we pass and return other variables.

Passing an Object as an argument
To pass an object as an argument we write the object name as the argument while calling the function in the same way we do it for other variables.


#include <bits/stdc++.h>
using namespace std;

class MyClass{ // class keyword and Class Name
    int length;
    public:  // Access Specifier
        static int myNum; // Static Member
        string s;
        string get(MyClass obj){
            return obj.s+" "+s;
        }
};

int MyClass::myNum=100;

int main(){
    MyClass obj = MyClass();
    obj.s="Object 1";
    MyClass obj2 = MyClass();
    obj2.s="Object 2";
    cout<<obj2.get(obj);
    return 0;
}

Output: Object 1 Object 2

Returning Objects in C++

#include <bits/stdc++.h>
using namespace std;

class MyClass{ // class keyword and Class Name
    int length;
    public:  // Access Specifier
        static int myNum; // Static Member
        string s;
        MyClass get(MyClass obj){
            MyClass ex;
            ex.s = "Object Example";
            ex.s= obj.s + " " + s + " " + ex.s;
            return ex;
        }
};

int MyClass::myNum=100;

int main(){
    MyClass obj = MyClass();
    obj.s="Object 1";
    MyClass obj2 = MyClass();
    obj2.s="Object 2";
    cout<<obj2.get(obj).s;
    return 0;
}
OutputObject 1 Object 2 Object Example






Tags :

oops for interview
oops in c++
interview question of oops
object oriented programming in c++
oops concepts in c++
oops concepts interview questions
single inheritance in c++
java oops interview questions
object oriented programming interview questions
python oops interview questions
oops concepts in java interview questions
oops interview questions for freshers
basic concepts of oops in c++
oops interview questions c#
oops concepts in c
oops interview questions and answers
php oops interview questions
interviewbit oops
features of oop in c++
oops concepts in c++ with examples
oop in c
oops viva questions
oop questions
oops in cpp
oops interviewbit
oops concepts in python interview questions
oops in python interview questions
oops interview questions in python
oops concepts in c language
oops concepts in c# interview questions
object oriented design interview questions
oops concepts interview questions for freshers
oops concepts in c++ pdf
oops basic interview questions
oops concepts in c++ interview questions
c++ object oriented
object oriented programming c++ examples
object oriented programming using c++
oops interview bit
oops concepts for interview
oops abap interview questions
object oriented programming in c
single inheritance program in c++
features of object oriented programming in c++
oops coding questions
c++ oops interview questions
define oops in c++
object oriented interview questions
top oops interview questions
oops concepts in cpp
object oriented programming questions
single inheritance in c++ with example program
oops in c++ interview questions
oops interview questions interviewbit
oops python interview questions
ood interview questions
java oops coding questions and answers
c object oriented
oops interview questions for experienced
interview bit oops
object oriented programming c++ notes pdf
top 50 oops interview questions
oops concepts in c++ in hindi
balagurusamy c++ pdf
oops cpp
basic concepts of object oriented programming in c++
object oriented programming c++ pdf
principles of oop in c++
oops using c++
object oriented programming with c++ by balaguruswamy lecture notes pdf
oops questions in java
programming questions on classes and objects in c++
robert lafore c++
oops viva questions in c++
oops technical interview questions
oops in c language
c++ oop exercises
oops interview questions with realtime examples
python oop interview questions
oops interview questions c++
principles of object oriented programming in c++
oops concepts in c++ with real time examples
python oops concepts interview questions
object oriented programming c++ lecture notes ppt
object oriented programming with c++ by balaguruswamy 6th edition ppt
object oriented design interview
oops interview questions in c++
interviewbit oops interview questions
encapsulation in java interview questions
oops viva questions c++
oops notes c++
object oriented programming concepts in c++
object oriented programming c++ handwritten notes pdf
basic concepts of oops in c++ pdf
oops practice questions
object oriented programming c++ lecture notes pdf
oops interview questions pdf
oops concepts in c# interview questions and answers
sap abap oops interview questions
oops program in c++

Post a Comment

0 Comments