Interview Questions on C++
- What is the difference between c and c++?
Ans . C is a procedural programming language whereas C++ supports both procedural and Object-Oriented programming.
- What are the various features of c++?
- Data Abstraction
- Objects and Classes
- Define Object
Ans . Any entity that has state and behaviour is known as an object. Objects represent instances of a class. Objects are basic run time entities in an object-oriented system.
- Define class
Ans . It is a blue print of an object that describe the behaviour if the object.
- Define Inheritance:
Ans . Inheritance is the process of forming a new class from an existing class or base class. The base class is also known as parent class or super class.
- Define Polymorphism
Ans . An ability to take more than one form is called as polymorphism.
C++, we use Function overloading and Function overriding to achieve polymorphism.
- Define Abstraction
Ans . Hiding internal details and showing functionality is known as abstraction.
In C++, we use abstract class and interface to achieve abstraction.
- Define Encapsulation
Ans . Binding or wrapping of code and data together into a single unit is called as encapsulation
- Define Modularity
Ans . The act of partitioning a complex program into simpler fragments called modules is called as modularity.
- What are the various types of access specifiers in C++ programming language?
Ans . There are three main types of access specifiers in C++ programming language:
- private: Class members declared as private can be used only by member functions and friends of the class.
- public: Class members declared as protected can be used by member functions and friends of the class. Additionally, they can be used by classes derived from the class.
- Protected: Class members declared as public can be used by any function.
- How to create the object of the class?
Ans . Creating object or defining the object of a class:
The syntax of defining the object of a class is: –
Example: Student stud1; // declare stud1 of type Student
- What is the general syntax for accessing data member of a class is?
Ans . Object_name.Data_member = value;
- Advantage of OOPs over Procedure-oriented programming language.
Ans . In Object oriented programming language following important feature are present which is not present in procedural language
- Which function we used in c++ to perform input and output operations.
Ans . cin() and cout()
cin() for the reading the data from the user.
cout() is used to print data on to the screen
- Explain C++ Constructor
Ans . Constructor is a special method which invoked automatically at the time of object creation. It is used to initialize the data members of new object generally. The constructor in C++ has the same name as that of class or structure.
- Write a Sample code to define constructor
Ans . #include <iostream>
using namespace std;
cout<<“Default Constructor is Invoked”<<endl;
Student s1; //creating an object of Student
Default Constructor is Invoked
Default Constructor is Invoked
- What are the types of constructors?
- Default Constructor
- Parameterized Constructors
- Explain Default Constructor
Ans . A constructor that accepts no parameter is called the Default Constructor. If you don’t declare a constructor or a destructor, the compiler makes one for you.
- Explain Parameterized Constructors:
Ans . A constructor that accepts parameters for its invocation is known as parameterized Constructors, also called as Regular Constructors.
- Explain destructor in c++:
Ans . A destructor is also a member function whose name is the same as the class name but is preceded by tilde (“~”). It is automatically by the compiler when an object is destroyed.