What are the basic concepts of Object-Oriented Programming (OOP)?
- Encapsulation hides data, inheritance allows class hierarchies, polymorphism lets methods do different things based on context, and abstraction simplifies complex systems by modeling classes appropriate to the problem.
What is the difference between C and C++?
- C is procedural; C++ is both procedural and object-oriented. C++ supports classes and objects, while C does not. C++ offers features like function overloading and templates which C lacks.
What are the various data types in C++?
- Primitive data types: int, char, float. Derived types: arrays, pointers, references. User-defined types: classes, structures, enums.
Explain the concept of constructors and destructors in C++.
- Constructors initialize objects, destructors clean up before an object is destroyed. Constructors can be overloaded, destructors cannot.
What is operator overloading?
- Operator overloading allows custom implementation for operators in user-defined types. For example,
+
can be overloaded to add two complex numbers.
- Operator overloading allows custom implementation for operators in user-defined types. For example,
What is function overloading?
- Function overloading allows multiple functions with the same name but different parameters. It enables functions to handle different data types or argument lists.
Explain the concept of inheritance in C++.
- Inheritance allows a class to inherit properties and methods from another class. Types include single, multiple, multilevel, hierarchical, and hybrid inheritance.
What is polymorphism in C++?
- Polymorphism allows methods to do different things based on the object they are called on. It can be achieved via function overloading (compile-time) or virtual functions (run-time).
What are virtual functions?
- Virtual functions support runtime polymorphism. They are defined in a base class and overridden in derived classes.
What is a pure virtual function?
- A pure virtual function is declared by assigning 0 in its declaration. It makes a class abstract, meaning it cannot be instantiated.
Explain the concept of templates in C++.
- Templates allow functions and classes to operate with generic types. This enables code reusability for different data types.
What is the Standard Template Library (STL)?
- STL provides a set of common classes for data structures and algorithms, like vectors, lists, queues, and algorithms like sort and search.
What is the difference between a pointer and a reference?
- Pointers can be reassigned and can point to
NULL
. References are aliases and must be initialized when declared, and cannot beNULL
.
- Pointers can be reassigned and can point to
What is RAII (Resource Acquisition Is Initialization)?
- RAII ties resource management to object lifetime. Resources are acquired during object creation and released during destruction.
Explain the difference between
new
andmalloc
.new
initializes objects and calls constructors,malloc
does not.new
returns an object of the correct type,malloc
returnsvoid*
.
What is exception handling in C++?
- Exception handling uses try, catch, and throw to manage errors. It provides a way to handle errors gracefully without crashing the program.
What are smart pointers in C++?
- Smart pointers like
unique_ptr
,shared_ptr
, andweak_ptr
manage memory automatically. They help avoid memory leaks by ensuring proper cleanup.
- Smart pointers like
What is the difference between
struct
andclass
in C++?- The key difference is default access;
struct
members are public by default, whereasclass
members are private. Both can have methods and inheritance.
- The key difference is default access;
Explain the concept of namespaces in C++.
- Namespaces prevent name conflicts by grouping entities like classes, objects, and functions under a name. The
std
namespace is an example.
- Namespaces prevent name conflicts by grouping entities like classes, objects, and functions under a name. The
What is the Rule of Three/Five/Zero in C++?
- If a class needs a custom destructor, copy constructor, or copy assignment operator, it likely needs all three (Rule of Three). The Rule of Five adds move constructor and move assignment operator. The Rule of Zero suggests using RAII and smart pointers to avoid manual resource management.