Commonly Asked C++ Interview Questions

[]
Technical Questions and Answers

C++Server Side ProgrammingProgramming

Here we will see some important C++ interview questions.

Key C C++
Introduction C was developed by Dennis Ritchie in around 1969 at AT&T Bell Labs. C++ was developed by Bjarne Stroustrup in 1979.
Language Type As mentioned before C is procedural programming. On the other hand, C++ supports both procedural and object-oriented programming paradigms.
OOPs feature Support As C does not support the OOPs concept so it has no support for polymorphism, encapsulation, and inheritance. C++ has support for polymorphism, encapsulation, and inheritance as it is being an object-oriented programming language
Data Security As C does not support encapsulation so data behave as a free entity and can be manipulated by outside code. On another hand in the case of C++ encapsulation hides the data to ensure that data structures and operators are used as intended.
Driven type C in general known as function-driven language. On the other hand, C++ is known as object driven language.
Feature supported C does not support function and operator overloading also do not have namespace feature and reference variable functionality. On the other hand, C++ supports both function and operator overloading also have namespace feature and reference variable functionality.

The main differences between pointers and references are –

  • References are used to refer an existing variable in another name whereas pointers are used to store address of variable.

  • References cannot have a null value assigned but pointer can.

  • A reference variable can be referenced by pass by value whereas a pointer can be referenced but pass by reference.

  • A reference must be initialized on declaration while it is not necessary in case of pointer.

  • A reference shares the same memory address with the original variable but also takes up some space on the stack whereas a pointer has its own memory address and size on the stack

  • What is virtual function in C++?

Virtual functions in C++ use to create a list of base class pointers and call methods of any of the derived classes without even knowing the kind of derived class object. Virtual functions are resolved late, at the runtime.

If A virtual function in a base class declared as once a member function, it becomes virtual in every class derived from that base class. So, use of the keyword virtual is not necessary in the derived class while declaring redefined versions of the virtual base class function.

Example

 Live Demo

#include using namespace std; class B {    public:    virtual void s() {       cout<<" In Base n";    } }; class D: public B {    public:    void s() {       cout<<"In Derived n";    } }; int main(void) {    D d; // An object of class D    B *b= &d; // A pointer of type B* pointing to d    b->s(); // prints”D::s() called”    return 0; }

Output

In Derived

What is this pointer in C++?

Every object in C++ has access to its own address through an important pointer called this pointer. The this pointer is an implicit parameter to all member functions. Therefore, inside a member function, this may be used to refer to the invoking object.

Friend functions do not have a this pointer, because friends are not members of a class. Only member functions have a this pointer.

Let us try the following example to understand the concept of this pointer −

Example

 Live Demo

#include using namespace std; class Box {    public:    // Constructor definition    Box(double l = 2.0, double b = 2.0, double h = 2.0) {       cout <<"Constructor called." << endl;       length = l;       breadth = b;       height = h;    }    double Volume() {       return length * breadth * height;    }    int compare(Box box) {       return this->Volume() > box.Volume();    }    private:    double length; // Length of a box    double breadth; // Breadth of a box    double height; // Height of a box }; int main(void) {    Box Box1(3.3, 1.2, 1.5); // Declare box1    Box Box2(8.5, 6.0, 2.0); // Declare box2    if(Box1.compare(Box2)) {       cout << "Box2 is smaller than Box1" <Output

Constructor called. Constructor called. Box2 is equal to or larger than Box1 raja

Published on 03-Jan-2020 15:25:25

Advertisements


Source

Leave a comment

Your email address will not be published. Required fields are marked *