C++ Programming Technical Interview Questions

What is C++?

C++ is a type of programing language that was developed in 1979 by Bjarne Stroustrup. It is build up on the C Programming language, though has many significant changes to it. It is an object oriented programming (OOP) language, and as a successor of C can be used coded in "C style" or "object-oriented style." It can be coded either way, hence making it a hybrid language.

It is a middle-level programming language, which means that it combines characteristics of both high level and low level programming languages, such as being easy to read and machine independent. However, it improves on C by adding classes, virtual functions, templates, and operator overloading.

C++ is primarily preferred for graphical applications, especially those that run in Windows and Macintosh environment, as well as primarily for games. However, some evidence does suggest that the popularity of C++ is declining over the years.

What are the advantages of C++?

C++ was developed to be an improvement over C. Hence, it offers native support for object oriented programming, and hence includes classes, inheritance, polymorphism, data abstraction and encapsulation. It also uses multi-paradigm programming and follows three paradigms: Generic, Imperative, and Object Oriented.  It also provides high level abstraction and is compatible with C.

C++ is also highly portable which allows it to be used over multiple devices and platforms. It also has a rich standard library that allows one to redeclare variables in the midst of code and one line comments. It also allows reusability of code and uses inheritance and polymorphism.

C++ is generally considered to be a powerful, efficient and fast language with numerous applications, including but not limited to GUI, 3D graphics, and real-time mathematical simulations.

What are the disadvantages of C++?

C++ is a very large high level program that leads to a bulky code. It is commonly used for platform specific application, and for web applications that are complex and difficult to debug. It also requires the same operation to be done more than once for which the same sequence has to be copies at a number of places. This leads to an incremental size of program, due to which the efficiency of the program is decreased.

Additionally, C++ does not support garbage collection or Dynamic Memory Allocation. It also does not have any support for threads built in, and is not secure because it has pointer, friend function and global variable.

What is the difference between C and C++?

C++ has been developed from C. However, as it was intended to be an improvement on C, it features various differences when compared to its predecessor.

C Programming Language

C++ Programming Language

Supports procedural style programming

Supports both procedural style and object oriented programming

Follows top down approach

Follows bottom up approach

Is a middle level language.

Is a high level language

Functions are the fundamental building blocks

Objects are the fundamental building blocks

Does not support function overloading

Supports function overloading

Does not support operator overloading

Supports operator overloading

Can’t use functions in structure

Can use functions in structure.

Uses scanf() and printf() for input/output functions

Uses cin and cout for input/output functions

Data is less secure

Can use modifiers to make class members inaccessible for outside users

Variables must be defined at the beginning in the function

Variables can be defined anywhere in the function.

Does not support namespace feature

Supports namespace feature

Programs are divided into modules and functions.

Programs are divided into classes and functions.

Does not support exception handling directly

Supports exception handling via try and catch block

The main() function can be called from other functions

The main() function cannot be called from other functions as the program execution starts from the main() function.

Does not support inheritance

Supports inheritance

Does not support polymorphism

Supports polymorphism

Mapping between data and functions is difficult

Mapping between data and functions is easy

Supports only built-in data types

Supports both built-in and user-defined data types

Uses .C file extension

Uses .CPP file extension

What are the basic concepts of object oriented programming?

C++ is an object oriented programming (OOP), hence it follows the basic concepts of OOP, such as the concept of Class and Objects. It also supports all the other basic concepts associated with them such as Inheritance, Polymorphism, Abstraction, Encapsulation, etc.

Classes and Objects: Classes specify the structure of the data. They also define data type. Objects are created from classes. They are the instances of classes.

Inheritance: Inheritance is used to inherit the property of one class into another class. Basically, it allows the defining of one class in the context of another.

Polymorphism: Polymorphism means ability to take more than one form. Hence, in C++, operations may take on different behaviors in different instances depending on the types of data used in the operation.

Abstraction: Abstraction is used to hide the internal implementations of the program. Hence, only the necessary detail will be visible to others. Data abstraction is implemented using interfaces and abstract classes in C++.

Encapsulation: Encapsulation is also known as data hiding. It allows the data and associated operations to be bound together and thus hiding the data from different classes.

What is a class?

A class is considered to be a blueprint. Officially, it is known as a user-defined data type. Basically, it defines the category of things, i.e. the datatype. However, it does not actually define the data itself, just its structure and category. It covers what the class name means and what an object of the class will consist of, as well as what operations can be performed on such an object.

Unlimited number of objects can be created from a class.

What is an object?

An object is the basic unit of object oriented programing. It can also be said that an object is the basic form of data, as well as a function combined into one unit. It is an instance of a class that has data members and uses various member functions to perform tasks. The objects of a class are declared with the same sort of declaration as variables of basic types.

What is the difference between an object and a class?

An object is the basic unit of object oriented programing. It is created from a class. Every object belongs to a class and every class contains one or more related objects. The class defines what kind of data is an object, as well as what operations can be performed on such an object.

A class is also static, which means that all the attributes of a class are fixed before, during, and after the execution of a program. Similarly, the class to which an object belongs to also usually static, which means that, it will not change since the time it has been created till the time it is destroyed.

However, an object has a limited lifespan, unlike a class. The objects are created and eventually destroyed. Also the attributes of the object may change over its lifetime.

What is the difference between class and structure?

Class and structure are two different types of objects. However, each one is very different from the other and each has its own set of advantages and disadvantages.

Class

Structure

Is a reference type

Is a value type

Its object is created on Heap memory.

Its object is created on Stack memory.

Can inherit another class.

Does not support the inheritance and cannot inherit another class.

Can have constructor and destructor of all types.

Structure can only have the parameterized constructor.

The member variable can be initialized directly.

The member variable cannot be initialized directly.

Class object cannot be created without using the new keyword

Structure object can be created without using the new keyword.

Members of a class are private by default

Members of structure are public by default.

Are used to model more complex behavior or data that is intended to be modified after a class object is created.

Are best suited for small data structures that contain primarily data that is not intended to be modified after the structure is created.

Can be abstract

Can't be abstract

Contains data member and member function.

Contains only data member

Values are assigned

Can't initialize the value to the variable

Can contain a volatile field

Can't contain a volatile field

Can't use sizeof with classes

Can use sizeof with classes

Fields are automatically initialized with classes to 0/false/null

Fields are not automatically initialized

What is an object based programming language?

In the simplest of terms, an object based programming language is any programming language that uses objects, specifically, the ‘idea of encapsulating state and operations inside "objects".’ Object based programming languages are also often referred to as "object-oriented".  However, unlike object oriented languages, object based programming languages so do not support all the features of object oriented programming (OOPs) like Polymorphism and Inheritance.

Additionally, unlike OOPs, object-based languages need not support inheritance or subtyping. Ada, Visual Basic (VB), and Fortran 90 are some examples of languages that can are object-based, but not object-oriented.

Basically, object-based programming languages are like object oriented languages, but do not support all the features of object oriented programming.

What is an object oriented language?

An object oriented language is a type of programming language that uses objects, specifically, the ‘idea of encapsulating state and operations inside "objects".’ It incorporates all of the features of object oriented programming (OOPs) including Polymorphism and Inheritance.

In OOP, the programmers define not only the data type of a data structure, but also the types of operations or functions that can be applied to the data structure. Hence, the object can include both data and functions. Additionally, a relationship can be created between the object and another. An object can also inherit characteristics from other objects.

Some significant object-oriented languages include Java, C++, C#, Python, PHP, Ruby, Perl, Object Pascal, Objective-C, Dart, Swift, Scala, Common Lisp, and Smalltalk.

What is OOPs?

OOPs stands for Object-oriented programming. It is a type of programming that deals with objects and data, rather than actions and logic. OOPs programming languages follow a logical procedure that takes input data, processes it, and produces output data.

OOPs is an extension of Procedure Oriented Programming Language. The primary benefit of OOPs is that it ends up reducing the code of the program due to Polymorphism. In addition to this, OOPs has various others properties such as Data Hiding, Inheritance, Data Abstraction, Data Encapsulation, etc. all of which help develop a more user friendly, robust, and efficient software.

What are the benefits of OOPs?

OOPs stands for object-oriented programming, which deals with objects and data, rather than actions and logic. It is a subset of Procedure Oriented Programming language, however it has certain benefits over it, such as:

  • Code Reusability: code can easily be reused
  • Inheritance: Eliminates redundant code and extends the use of existing classes
  • Encapsulation: Data and functions can be hid in one class from another
  • Easy Maintenance
  • Easy to upgrade
  • Polymorphism: Different methods can have the same name but different behaviors.
  • Abstraction: Common definition of a base class that multiple derived classes can share
What is public, protected, and private?

In object oriented programming, public, private and protected are types of access modifiers which help in Encapsulation. Here, data and functions can be hid in one class, so that another may not be able to access it. Public, private and protected are three different categorizations that tell the compiler which other classes should have access to the field or method being defined.

  • Private - Only the current class will have access to the field or method.
  • Protected - Only the current class and subclasses of this class will have access to the field or method. Sometimes the same-package classes may also have access.
  • Public - Any class can refer to the field or call the method.

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.