Skip to main content

C++ OOPS Concepts & Interview questions:

Is virtual constructor possible:
    Virtual constructor is not possible. Virtual has to be used when we don't know the exact type of object and only reference is there to call it. But while creating object we need exact type to create that's why virtual constructor is not possible.

#include <iostream>

using namespace std;
class Base{
    virtual Base(){
        cout<<"virtual constructor"<<endl;
    }   
};

class Derived : public Base{
    Derived(){
        cout<<"Derived constructor"<<endl;
    }   
};

int main()
{
    Derived d;
}


How to Stop from creating Objects of a class:
In order to restrict users from creating objects of a class define the constructor as private of the class. As the object cannot call private functions of the class. It restricts the object creation as constructor call is restricted.

#include <iostream>

using namespace std;


class Derived{
    Derived(){
        cout<<"Derived constructor"<<endl;
    }   
};

int main()
{
    Derived d;
}
Compilation failed due to following error(s).

main.cpp: In function ‘int main()’:
main.cpp:23:13: error: ‘Derived::Derived()’ is private within this context
     Derived d;

What is pure virtual function:
Pure virtual function is being used when we need to create abstract class where Objects of it can't be created. A pure virtual function can be declared as:
    virtual void func() = 0;
If a derived class doesn't override the pure virtual function, then it also becomes abstract class.
class Base{
    public:
    Base(){
        cout<<"virtual constructor"<<endl;
    }
    virtual void func() = 0;
};

class Derived : public Base{
    public:
    Derived(){
        cout<<"Derived constructor"<<endl;
    }
};

int main()
{
    Derived *d = (Derived*)new Base();
}
main.cpp: In function ‘int main()’:
main.cpp:29:37: error: invalid new-expression of abstract class type ‘Base’
     Derived *d = (Derived*)new Base();

Below code works well and doesn't create any problems:

class Base{
    public:
    Base(){
        cout<<"virtual constructor"<<endl;
    }
    virtual void func() = 0;
};

class Derived : public Base{
    public:
    Derived(){
        cout<<"Derived constructor"<<endl;
    }
    void func(){}
};

int main()
{
    Derived *d = (Derived*)new Derived();
}
































main.cpp:13:18
: error: constructors cannot be declared ‘virtual’ [-fpermissive]




















Comments

Popular posts from this blog

Leet Code: Problem #710 Random Pick with Blacklist

Given a blacklist  B containing unique integers from [0, N) , write a function to return a uniform random integer from [0, N) which is NOT  in B . Optimize it such that it minimizes the call to system’s Math.random() . Note: 1 <= N <= 1000000000 0 <= B.length < min(100000, N) [0, N)  does NOT include N. See interval notation . Example 1: Input: ["Solution","pick","pick","pick"] [[1,[]],[],[],[]] Output: [null,0,0,0] Example 2: Input: ["Solution","pick","pick","pick"] [[2,[]],[],[],[]] Output: [null,1,1,1] Example 3: Input: ["Solution","pick","pick","pick"] [[3,[1]],[],[],[]] Output: [null,0,0,2] Example 4: Input: ["Solution","pick","pick","pick"] [[4,[2]],[],[],[]] Output: [null,1,3,1] Explanation of Input Syntax: The input is two lists: the subroutines called and their argume...

Creating Self Signed SSL Certificates for HTTPS Communication

Self Signed CA: Create Private Key for Self Signed CA openssl ecparam -genkey -name secp256r1 | openssl ec -out ca.key     Create CA Certificate for Self Signed CA openssl req -new -x509 -days 36500 -key ca.key -out ca.pem -subj "/C=IN/ST=Karnataka/L=Bengaluru/O=company name/OU=Prod Operations Department/CN=prodops .domain.com   Verify the content of CA certificate openssl x509 -in ca.pem -noout -text Client CERTIFICATE: CLIENT_ID="<Client-Product>" e.g. CLIENT_ID="ClientID" CLIENT_SERIAL="<Client-Release-Number>" e.g. CLIENT_SERIAL="6889" Create Private Key for Client openssl ecparam -genkey -name secp256r1 | openssl ec -out  ${CLIENT_ID}_${CLIENT_SERIAL}.key                   Generate the Certificate Signing Request CSR openssl req -new -key ${CLIENT_ID}_${CLIENT_SERIAL}.key -out ${CLIENT_ID}_${CLIENT_SERIAL}.csr -subj "/C=IN/ST=Karnataka/L=Bengalur...

Tree Data Structure related must solve programming questions: Part - 1

LeetCode Problem #687 Given a binary tree find the longest possible path with same node values. The length of the path is determined the number of edges between the node. Example 1: Input: 5 / \ 4 5 / \ \ 1 1 5 Output:  2   Example 2: Input: 1 / \ 4 5 / \ \ 4 4 5 Output:  2 Solution in C++: /**  * Definition for a binary tree node.  * struct TreeNode {  *     int val;  *     TreeNode *left;  *     TreeNode *right;  *     TreeNode() : val(0), left(nullptr), right(nullptr) {}  *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}  *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}  * };  */ class Solution ...