1. What is C++?
Answer: C++ is a high-level, object-oriented programming language developed by Bjarne Stroustrup in the early 1980s. It is an extension of the C programming language and supports features like classes, inheritance, polymorphism, and encapsulation.
2. What are the key features of C++?
Answer:
Object-Oriented: Supports encapsulation, inheritance, and polymorphism.
Rich Standard Library: Includes a wide range of libraries for various tasks.
Memory Management: Provides direct memory management through pointers.
Operator Overloading: Allows custom definitions for operators.
3. What is the difference between a class and a struct in C++?
Answer: The main difference is the default access level: members of a struct are public by default, while members of a class are private. This affects how data can be accessed and modified.
4. Explain the concept of constructors and destructors.
Answer:
Constructor: A special member function that initializes objects when they are created. It has the same name as the class and no return type.
Destructor: A special member function that cleans up when an object goes out of scope or is deleted. It has the same name as the class but preceded by a tilde (~).
5. What is inheritance in C++?
Answer: Inheritance allows a class (derived class) to inherit properties and behaviors (methods) from another class (base class). This promotes code reusability and establishes a relationship between classes.
6. What is polymorphism in C++?
Answer: Polymorphism allows methods to do different things based on the object it is acting upon, enabling one interface to be used for different data types. It can be achieved through function overloading and operator overloading.
7. What are virtual functions?
Answer: Virtual functions allow derived classes to override methods defined in base classes. They enable dynamic (runtime) polymorphism, ensuring that the correct method is called for an object, regardless of the type of reference used for the object.
8. What is a pure virtual function?
Answer: A pure virtual function is declared by assigning 0 to it in a class declaration. It makes the class abstract, meaning it cannot be instantiated directly:
class Base {
public:
virtual void show() = 0; // Pure virtual function
};
9. Explain operator overloading with an example.
Answer: Operator overloading allows defining custom behavior for operators when applied to user-defined types:
class Complex {
public:
float real;
float imag;
Complex operator + (const Complex& obj) {
Complex temp;
temp.real = real + obj.real;
temp.imag = imag + obj.imag;
return temp;
}
};
10. What are templates in C++?
Answer: Templates allow writing generic programs by defining functions or classes with placeholder types. They enable code reusability for different data types without rewriting code:
template <class T>
T add(T a, T b) {
return a + b;
}
11. What is exception handling in C++?
Answer: Exception handling provides a way to respond to runtime errors using try, catch, and throw keywords:
try {
// Code that may throw an exception
} catch (const std::exception& e) {
// Code to handle the exception
}
12. What is the difference between deep copy and shallow copy?
Answer:
Shallow Copy: Copies all member variables directly; if they point to dynamically allocated memory, both objects share that memory.
Deep Copy: Creates a new instance of dynamically allocated memory for each member variable, ensuring that each object has its own copy.
13. Explain the use of new and delete operators in C++.
Answer:
new: Allocates memory dynamically on the heap for an object or array.
delete: Deallocates memory previously allocated with new, preventing memory leaks.
14. What are smart pointers in C++?
Answer: Smart pointers are wrapper classes around regular pointers that manage memory automatically, ensuring proper deallocation when no longer needed. Common types include std::unique_ptr, std::shared_ptr, and std::weak_ptr.
15. How do you implement multiple inheritance in C++?
Answer: Multiple inheritance allows a class to inherit from more than one base class:
class Base1 { /*...*/ };
class Base2 { /*...*/ };
class Derived : public Base1, public Base2 { /*...*/ };
16. What is the purpose of the static keyword?
Answer:
When applied to variables inside functions, it retains their value between function calls.
When applied to class members, it indicates that they belong to the class rather than any instance.
17. Explain what namespaces are in C++.
Answer: Namespaces provide a way to group entities like classes, objects, and functions under a name to avoid name conflicts in larger projects:
namespace MyNamespace {
int value;
}
18. What are lambda expressions in C++?
Answer: Lambda expressions provide a concise way to define anonymous functions directly within code, often used for callbacks or as arguments to algorithms:
auto add = [](int x, int y) { return x + y; };
19. How do you create an abstract class in C++?
Answer: An abstract class contains at least one pure virtual function, making it impossible to instantiate directly:
class Abstract {
public:
virtual void show() = 0; // Pure virtual function
};
20. What is RAII (Resource Acquisition Is Initialization)?
Answer: RAII is a programming idiom where resource allocation is tied to object lifetime; resources are acquired during object creation and released during destruction, ensuring proper resource management without leaks.
Comments