CPP (or) C++ Interview Questions
As an extension of the C language, C++ is an object-oriented programming language created by Bjarne Stroustrup. It was released in 1985. C++ is a superset of C with the major addition of classes in C language.
 
Initially, Stroustrup called the new language "C with classes". However, after sometime the name was changed to C++. The idea of C++ comes from the C increment operator ++.
The class is a user-defined data type. The class is declared with the keyword class. The class contains the data members, and member functions whose access is defined by the three modifiers are private, public and protected. The class defines the type definition of the category of things. It defines a datatype, but it does not define the data it just specifies the structure of data.
 
You can create N number of objects from a class.
By using the keyword class followed by identifier (name of class) you can specify the class in C++. Inside curly brackets, body of the class is defined. It is terminated by semi-colon in the end.
class name{
// some data
// some functions
};
The main difference between C and C++ are provided in the table below :

C C++
* C language was developed by Dennis Ritchie. * C++ language was developed by Bjarne Stroustrup.
* C is a structured programming language. * C++ supports both structural and object-oriented programming language.
* C is a subset of C++. * C++ is a superset of C.
* In C language, data and functions are the free entities. * In the C++ language, both data and functions are encapsulated together in the form of a project.
* C does not support the data hiding. Therefore, the data can be used by the outside world. * C++ supports data hiding. Therefore, the data cannot be accessed by the outside world.
* Namespace features are not present in C * Namespace is used by C++, which avoids name collisions.
* Functions can not be defined inside structures. * Functions can be defined inside structures.
* calloc() and malloc() functions are used for memory allocation and free() function is used for memory deallocation. * new operator is used for memory allocation and deletes operator is used for memory deallocation.
* In C, scanf() and printf() are mainly used for input/output. * C++ mainly uses stream cin and cout to perform input and output operations.
Virtual functions are used with inheritance, they are called according to the type of object pointed or referred, not according to the type of pointer or reference. In other words, virtual functions are resolved late, at runtime. Virtual keyword is used to make a function virtual.
 
Following things are necessary to write a C++ program with runtime polymorphism (use of virtual functions) :

a) A base class and a derived class.
b) A function with same name in base class and derived class.
c) A pointer or reference of base class type pointing or referring to an object of derived class.

In the following program bp is a pointer of type Base, but a call to bp->show() calls show() function of Derived class, because bp points to an object of Derived class.
#include<iostream>
using namespace std;

class Base {
public:
	virtual void show() { cout<<" In Base \n"; }
};

class Derived: public Base {
public:
	void show() { cout<<"In Derived \n"; }
};

int main(void) {
	Base *bp = new Derived;	
	bp->show(); // RUN-TIME POLYMORPHISM
	return 0;
}​

Output : In Derived
The ‘this’ pointer is passed as a hidden argument to all nonstatic member function calls and is available as a local variable within the body of all nonstatic functions. ‘this’ pointer is a constant pointer that holds the memory address of the current object. ‘this’ pointer is not available in static member functions as static member functions can be called without any object (with class name).

#include <iostream>
#include <string>
using namespace std;

int main() {
  string education = "Free Time Learning";

  cout << education << "\n";
  cout << &education << "\n";
  return 0;
}​
Output : 

Free Time Learning
0x7ffecd166cb0
In C++ a structure is the same as a class except for a few differences like security. The difference between struct and class are given below :

Structure Class
Members of the structure are public by default. Members of the class are private by default.
When deriving a struct from a class/struct, default access specifiers for base class/struct are public. When deriving a class, default access specifiers are private.
An object is a run-time entity. An object is the instance of the class. An object can represent a person, place or any other item. An object can operate on both data members and member functions. The class does not occupy any memory space. When an object is created using a new keyword, then space is allocated for the variable in a heap, and the starting address is stored in the stack memory. When an object is created without a new keyword, then space is not allocated in the heap memory, and the object contains the null value in the stack.
class Student  
{  
//data members;  
//Member functions  
}  

 

The syntax for declaring the object :
Student s = new Student();  
One of the most important concepts in object-oriented programming is that of inheritance. Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time.
 
When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class.
 
Syntax :
class derived-class: access-specifier base-class
A member function of a class is a function that has its definition or its prototype within the class definition like any other variable. It operates on any object of the class of which it is a member, and has access to all the members of a class for that object.