Previously, we had discussed a few important C++ interview questions for those who were interested in considering a tech-based career.
Here’s a look at some more questions related to C++ that you may come across during an interview.
- What is the difference between structure and class in C++?
Classes are building blocks of object-oriented programming. Classes contain data members and member functions that have scope only within that particular class. Think of a class of ‘birds’. Different types of birds can be the data members while their habitats, diet, and behaviour can be the member functions. The member functions happen to be private by default. To declare a class, you’ll need to use the keyword ‘class’. An object is an instance of a class and memory is allocated to a class only when an object is instantiated.
Structures are similar to classes but lack the privacy that classes offer. The member functions are public by default and will have to be explicitly defined as private. To declare a structure, you’ll need to use the keyword ‘struct’. They are used instead of arrays when we need to group together variables belonging to different data types.
- What are data members and member functions?
Data members are basically variables of any data type, pointers, references, array, and user-defined types that have been defined within a class. Data types define the type of data being stored (char for characters, float for numbers with up to 6 decimals places, int for integers).
Member functions are functions and operators that have been defined within a class and they only have scope within the class that they have been defined in.
- What are the differences between pointers and references?
All variables are stored in memory and can be accessed by making a ‘call’ directly to them or to the address in memory that holds them. Pointers are basically variables that hold another variable’s address in memory. Pointers can be reused and can also be initialized to ‘null’ to indicate that they do not point to any value in particular. Pointers are generally used for implementing data structures like trees or linked lists.
References are similar to pointers but they are mostly ‘aliases’. This means that a reference is just another name for a variable that already exists in the memory! Just like pointers, references store the address of an object too. References cannot be used to refer to another object once they have been created. That is, they cannot be reassigned. They cannot be initialized to null since these constraints ensure that an ‘exception’ situation does not occur. References are used in return types and function parameters.
- What are virtual functions?
Virtual functions are functions typically declared within a class that is later redefined by the derived class (inheritance). Virtual functions must be accessed only using pointers and references. The keyword ‘virtual’ is used to define a virtual function. Virtual functions are commonly used to achieve runtime polymorphism – the process by which a function of a base class is overridden by the derived class.
- What is data abstraction?
Data abstraction refers to the concept where only information that is deemed essential is displayed while all other details are hidden. Abstraction can be used in classes or in header files. They are implemented using the access specifiers ‘public’ and ‘private’. While members that are public can be accessed from anywhere within the program, members that are private can only be accessed from within that class alone.
Understanding the scope of the role to which you are applying is essential, especially the technology used and the responsibilities. It is a good idea to brush up on your knowledge about the languages you are expected to know before you attend the interview. Attempting basic skill tests to realize where you stand is also a good starting point. One must also be ready to upskill themselves in order to adapt to the very dynamic environment that the tech industry really is.