What is difference between C and C++ ?

1. What is the difference between C and C++?

C++ is Multi-Paradigm ( not pure OOP, supports both procedural and object-oriented) while C follows procedural style programming.
In C data security is less, but in C++ you can use modifiers for your class members to make it inaccessible from outside.
C follows a top-down approach (the solution is created in a step by step manner like each step is processed into details as we proceed )
C++ follows a bottom-up approach ( where base elements are established first and are linked to make complex solutions ).
C++ supports function overloading while C does not support it.
C++ allows the use of functions in structures, but C does not permit that.
C++ supports reference variables ( two variables can point to the same memory location ). C does not support this.
C does not have a built-in exception handling framework, though we can emulate it with another mechanism. C++ directly supports exception handling, which makes the life of the developer easy.
2. What is a class?
The class defines a datatype, it’s type definition of the category of thing(s). But a class actually does not define the data, it just specifies the structure of data.
To use them you need to create objects out of the class.
The class can be considered as a blueprint of a building, you can not stay inside the blueprint of a building, you need to construct a building(s) out of that plan.
You can create any number of buildings from the blueprint, similarly, you can create any number of objects from a class.
3. What is an Object/Instance?
The object is the instance of a class, which is concrete. From the above example, we can create an instance of class Vehicle as given below
………………………..Vehicle object;…………….……………
We can have different objects of the class Vehicle, for example, we can have Vehicle objects with 2 tires, 4tyres, etc. Similarly different engine capacities as well.
4. What do you mean by C++ access specifiers?
Access specifiers are used to defining how the members (functions and variables) can be accessed outside the class. There are three access specifiers defined which are public, private, and protected
Members declared as private are accessible only within the same class and they cannot be accessed outside the class they are declared.
Members declared as public are accessible from anywhere.
Members declared as protected can not be accessed from outside the class except a child class. This access specifier has significance in the context of inheritance.
5. What are the basics concepts of OOP?
Encapsulation :
Encapsulation is the mechanism by which data and associated operations/methods are bound together and thus hide the data from the outside world. It’s also called data hiding.
In c++, encapsulation achieved using the access specifiers (private, public, and protected). Data members will be declared as private (thus protecting from direct access from outside) and public methods will be provided to access these data.
Data abstraction :
Data abstraction refers to hiding the internal implementations and shows only the necessary details to the outside world. In C++ data abstraction is implemented using interfaces and abstract classes.
Inheritance :
Inheritance allows one class to inherit properties of another class. In other words, inheritance allows one class to be defined in terms of another class.
6. What is the use of volatile keywords in c++? Give an example.
Most of the times compilers will do optimization to the code to speed up the program. For example in the below code,
int a = 10;
while( a == 10){
// Do something
the compiler may think that value of ‘a’ is not getting changed from the program and replace it with ‘while(true)’, which will result in an infinite loop. In the actual scenario, the value of a’ maybe getting updated from outside of the program.
Volatile keyword is used to tell the compiler that the variable declared using volatile may be used from outside of the program.
Volatile keyword is used to tell the compiler that the variable declared using volatile may be used from outside the current scope so that the compiler won’t apply any optimization. This matters only in the case of multi-threaded applications.
In the above example, if variable ‘a’ was declared using volatile, the compiler will not optimize it. In short, the value of the volatile variables will be read from the memory location directly.
7. In how many ways we can initialize an int variable in C++?
In c++, variables can be initialized in two ways, the traditional C++ initialization using the “=” operator and the second using the constructor notation.
Traditional C++ initilization int i = 10;
the variable i will get initialized to 10.
Using C++ constructor notation
int i(10);
8. What is implicit conversion/coercion in c++?
Implicit conversions are performed when a type (say T) is used in a context where a compatible type (Say F) is expected so that the type T will be promoted to type F.
short a = 2000 + 20;
In the above example, variable a will get automatically promoted from short to int. This is called implicit conversion/coercion in c++.
9. What are C++ inline functions?
C++ inline functions are special functions, for which the compiler replaces the function call with the body/definition of the function. Inline functions make the program execute faster than the normal functions since the overhead involved in saving the current state to stack on the function call is avoided. By giving the developer the control of making a function inline, he can further optimize the code based on application logic. But actually, it’s the compiler that decides whether to make a function inline or not regardless of its declaration. The compiler may choose to make a non-inline function inline and vice versa. Declaring a function as inline is in effect a request to the compiler to make it inline, which the compiler may ignore. So, please note this point for the interview that, it is up to the compiler to make a function inline or not.
10. What do you mean by translation unit in c++?
We organize our C++ programs into different source files (.cpp, .cxx etc). When you consider a source file, at the preprocessing stage, some extra content may get added to the source code ( for example, the contents of header files included) and some content may get removed ( for example, the part of the code in the ‪#‎ifdef‬ of ‪#‎ifndef‬ block which resolves to false/0 based on the symbols defined). This effective content is called a translation unit. In other words, a translation unit consists of
>Contents of the source file
>Plus contents of files included directly or indirectly
>Minus source code lines ignored by any conditional pre-processing directives ( the lines ignored by #ifdef,#ifndef, etc).
11. What do you mean by internal linking and external linking in c++?
[This interview question is related to questions on “translation unit” and “storage classes”]
A symbol is said to be linked internally when it can be accessed only from within the scope of a single translation unit. By external linking a symbol can be accessed from other translation units as well. This linkage can be controlled by using static and extern keywords.
12. What do you mean by storage classes?
Storage classes are used to specify the visibility/scope and lifetime of symbols(functions and variables). That means storage classes specify where all a variable or function can be accessed and till what time those variables will be available during the execution of the program.
13. How many storage classes are available in C++?
Storage classes are used to specify the visibility/scope and lifetime of symbols(functions and variables). That means storage classes specify where all a variable or function can be accessed and till what time those variables will be available during the execution of the program.
Following storage, classes are available in C++
Auto :
It’s the default storage class for local variables. They can be accessed only from within the declaration scope. auto variables are allocated at the beginning of the enclosing block and deallocated at the end of the enclosing block.
Register :
It’s similar to auto variables. The difference is that register variables might be stored on the processor register instead of RAM, which means the maximum size of the register variable should be the size of the CPU register ( like 16bit, 32bit, or 64bit). This is normally used for frequently accessed variables like counters, to improve performance. But note that, declaring a variable as a register does not mean that it will be stored in the register. It depends on the hardware and implementation.
Static :
A static variable will be kept in existence till the end of the program unlike creating and destroying each time they move into and out of the scope. This helps to maintain their value even if control goes out of the scope. When static is used with global variables, they will have internal linkage, which means they cannot be accessed by other source files. When static is used in the case of a class member, it will be shared by all the objects of a class instead of creating separate copies for each object.
Extern :
Extern is used to telling the compiler that the symbol is defined in another translation unit (or in a way, source files) and not in the current one. Which means the symbol is linked externally. extern symbols have static storage duration, that is accessible throughout the life of the program. Since no storage is allocated for the extern variables as part of the declaration, they cannot be initialized while declaring.
Mutable :
mutable storage class can be used only on nonstatic non-const data of a member of a class. The mutable data member of a class can be modified even if it’s part of an object which is declared as const.
14. What do you mean by persistent and non-persistent objects?
Persistent objects are the ones that we can be serialized and written to disk, or any other stream.
So before stopping your application, you can serialize the object and on the restart, you can deserialize it. [ Drawing applications usually use serializations.]
Objects that can not be serialized are called nonpersistent objects. [ Usually, database objects are not serialized because connection and session will not be existing when you restart the application. ].
15. Is it possible to get the source code back from a binary file?
Technically it is possible to generate the source code from binary. It is called reverse engineering. There are a lot of reverse engineering tools available. But, in the actual cases, most of them will not regenerate the exact source code back because much information will be lost due to compiler optimization and other interpretations.

Leave a Reply

Your email address will not be published. Required fields are marked *