1) Diff b/w C and CPP
C | C++ |
C is a procedure-oriented programming language. | C++ is an object-oriented programming language. |
C does not support data hiding. | C++ supports data hiding. |
C is a subset of C++ | C++ is a superset of C. |
C does not support Function and operator overloading | C++ support Function and operator overloading |
Functions can not be defined inside structures. | Functions can be defined inside structures. |
2) What is Template in CPP
Function templates allow you to write a generic function that can handle different data types without repeating the code for each type.
#include <iostream>
// Function template
template <typename T>
T add(T a, T b) {
return a + b;
}
int main() {
std::cout << "Int: " << add(1, 2) << std::endl;
std::cout << "Double: " << add(1.5, 2.3) << std::endl;
std::cout << "Float: " << add(1.1f, 2.2f) << std::endl;
return 0;
}
3) What is STL in C++?
STL is the standard template library. It is a library that allows you to use a standard set of templates for things such as: Algorithms, functions, Iterators in place of actual code.
4) What is the difference between structure and class in C++?
The difference between structure and class is as follows:
– By default, the data members of the class are private whereas data members of structure are public.
– While implementing inheritance, the access specifier for struct is public whereas for class its private.
– Structures do not have data hiding features whereas class does.
– Structures contain only data members whereas class contains data members as well as member functions.
– In structure, data members are not initialized with a value whereas in class, data members can be initialised.
– Structures are stored as stack in memory whereas class is stored as heap in memory.
5) Which operator cannot be overloaded in C++?
Some of the operators that cannot be overloaded are as follows:
– Dot operator- “.”
– Scope resolution operator- “::”
– “sizeof” operator
– Pointer to member operator- “.*”