C++ Programming FAQ (Frequently Asked Questions and answers) for freshers

This post lists the C++ Programming FAQ (Frequently Asked Questions and answers) for freshers for next job interview or C++ Programming exam.

C++ is an object-oriented programming language. It is a superset of C-language that has all the features of the C programming language.

#include<iostream.h> is included in c++ programs. '#' stands for preprocessor directive. iostream stands for the input-output stream. include is used to add the functionality of the iostream file in the program. 

It is a piece of code ignored by the compiler. It is written by the programmer to add the descriptive information about the source code.

  • Single line comment (//)
  • Multi-line comment (/*   */)

It supports features of C-language. The additional features C++ supports are:

  • Modularity: The software developed in c++ language runs on any platform. 
  • Object-Oriented: It includes the concepts of classes, objects, inheritance, and polymorphism. 
  • Concept of inheritance: With the help of inheritance, we can reuse the existing classes
  • Data hiding: It helps the programmer to secure the data from the attacker
  • Message Passing: A technique used for communication between the objects.

Bjarne Stroustrup

The class is the user-defined data type. The class is defined with the class keyword. A class is a blueprint of a class. It is a collection of functions and related data under a single name. 

SYNTAX: 

class class_name{

   // data member

   // Member functions

}

An Object is an instance of the class. it is the run-time entity. The class does not occupy any memory space. When an object is created with a new keyword then memory is occupied.  

There are two ways through which you can define the constant: 

  • int i=10;
  • int i(10);

It is a process of binding the data members and member functions in a single class. It prevents direct access to the data for security purposes.

It sorts out the name conflict of the identifier, which is skilled by placing them under numerous namespaces. It particularly helped in the logical division of the multiple codes. 

An abstraction in C++ means the process of hiding important information from the outside world and displaying the required information only. 

In the term, polymorphism poly means multiple, and morphism means the same form. It means we can define different functions with the same name but with different functionalities. 

There are two types of polymorphism:

  • Compile-time polymorphism: It is implemented during compile-time that's why it is known as compile-time polymorphism. It is also called static polymorphism. An example of compile-time polymorphism is Method overloading. 
    Method Overloading: It is a procedure that allows you to have more than one function with the same name but with different functionalities. 
  • Run-time polymorphism: It is implemented during run-time that's why it is known as run-time polymorphism. It is also called dynamic polymorphism. An example of run-time polymorphism is Function overriding. 
    Function Overriding: When the child class contains the same method present in the parent class. Child class overrides the method of the parent class. Both contain the same function with different functionality. 

Access specifier defines the scope of the class members i.e data members and member functions. The scope here means whether the member function is accessible outside class or not. If it is accessible, up to which we can use the class members i.e inside the class, outside the class, and anywhere in the program.  

There are three types of access specifier: 

  • Private: When the class members are defined under a private specifier, then it is accessible only inside the class. The programmer is not allowed to use outside the class.
  • Public: When defined in public, then it is accessible anywhere in the complete program. The programmer can call the member from anywhere with the help of an object of the class. In simple words, you can say it is globally accessible in the program. 
  • Protected: The protected member of the parent class is accessible only in the child classes. 
  • The data member and member functions must be declared inside the class only. 
  • It can not be re-declared. 
  • Members can not be added from somewhere else. 
  • It is accessible through class objects outside the class. 

It helps in declaring the variable as volatile. It means the value of the volatile variable is not copied to the register from memory. The operations are performed outside. The value of the volatile variable is never appeared to be modified but the value changes in different accesses.

A friend function is declared inside the class but defined outside the class. A friend function has the right to access all the private and protected members of the class. It is not a member function of the class. A friend can be a member function, class, and function. In the case of class, the entire class is a friend and all its members also. 

To avoid the function overheads in the program, C++ come up with the new functionality known as the inline function. An inline function is expanded in a single line. The inline function replaces the function call by the compiler through the entire code. It improvises the execution time of the program.

Yes, the inline function can be called recursively. The major disadvantage of calling the inline function recursively is wastage of memory but it saves the time of CPU in execution.

  • sizeof – sizeof operator
  • . – Dot operator
  • .* – dereferencing operator
  • -> – member dereferencing operator
  • :: – scope resolution operator
  • ?: – conditional operator

Pointers are nothing but a variable that the address of another variable. Pointers can be defined with different types of data types. A character type pointer holds the address of the character type variable. Through pointers, programs are stimulating call-by-reference.