C++ Important Questions by Unit
C++ Important Questions by Unit
In procedure-oriented programming (POP), the program is divided into functions, focusing primarily on procedures. Data is mainly exposed and can be accessed freely throughout the program, which can lead to security issues. In contrast, object-oriented programming (OOP) organizes programs into objects, which encapsulates code and data together, promoting reusable and modular code. OOP provides access specifiers that control the visibility of data, leading to more secure programs. OOP design enhances program structure by supporting inheritance, polymorphism, and encapsulation, enabling more flexible and maintainable code .
Static members in C++ are shared among all instances of a class, meaning they exist only once per class rather than once per object. 'Static' can be applied to both variables and methods. A static variable retains its value across function calls and is initialized only once. Static functions can access only static data members and other static functions within that same class. These members enhance memory efficiency and are ideal for defining constants and utility functions applicable to all objects of the class .
Function overloading in C++ allows multiple functions to have the same name with different parameters (number, type, or order of parameters). This promotes code efficiency and readability by enabling programmers to use intuitive names rather than cluttering code with different function names for similar operations. It enhances code maintainability by allowing simple extensions or modifications to function overloads without altering calls throughout the codebase .
Polymorphism in C++ allows objects to be treated as instances of their parent class, enabling one interface to be used for a general class of actions. Runtime polymorphism is achieved through virtual functions, which are member functions marked with the 'virtual' keyword in the base class. Derived classes can override these functions, enabling dynamic binding at runtime. This allows the appropriate function to be called on an object, depending on the type of reference (base or derived class), supporting flexibility and extensibility .
Inheritance in C++ allows the creation of a new class (derived class) based on an existing class (base class), inheriting its properties and behaviors. Types of inheritance in C++ include single inheritance (one base class), multiple inheritance (multiple base classes), multilevel inheritance (a class derived from another derived class), hierarchical inheritance (multiple classes derived from a single base class), and hybrid inheritance (a mix of above forms). In single inheritance, a derived class might extend the functionality of a base class, e.g., class 'Dog' inheriting from class 'Animal'. In multiple inheritance, a class 'AmphibiousVehicle' might inherit from both 'Car' and 'Boat' .
Class templates in C++ provide a way to create a single class definition that can work with any data type. They are essential for generic programming because they enable code reusability and type safety, allowing a generic class to operate on any data type without requiring separate code for each type. This reduces redundancy and errors in code, facilitating maintenance and improving performance by reducing the need for explicit type handling .
The Standard Template Library (STL) in C++ provides a set of common data structures (containers) and algorithms, which implement many common programming tasks and data manipulation strategies. STL benefits include increased reliability due to well-tested components, efficient data handling, and enhanced code reusability and productivity by providing generic classes and functions. It allows developers to focus on problem-specific parts rather than reimplementing data structures and algorithms .
Operator overloading in C++ enables user-defined types to behave like built-in types by redefining the behavior of operators (e.g., +, -, *, =). This enhances usability by allowing for intuitive code, where complex user-defined object manipulations use conventional operator syntax. For example, to overload the '+' operator for a class Complex: class Complex { public: int real, imag; Complex operator+ (const Complex &obj) { Complex temp; temp.real = real + obj.real; temp.imag = imag + obj.imag; return temp; } }; This allows instances of Complex to be added using the '+' symbol .
Access specifiers in C++ (public, private, and protected) determine the accessibility of class members. 'Public' members are accessible from anywhere within the program. 'Private' members are accessible only within the same class, ensuring data hiding and security. 'Protected' members are accessible within the same class and to derived classes. These specifiers allow fine-grained control over who can access the members and how, thus playing a crucial role in protecting the data and supporting encapsulation .
Virtual functions in C++ allow derived classes to override methods of a base class, enabling dynamic polymorphism and facilitating object-orientation principles like flexibility and reuse. The key advantage is that they enable functions to act differently based on the object type, regardless of referencing type. The constraints of virtual functions involve slight overhead due to dynamic dispatch tables, and they cannot be static or inline. Careful management is required to avoid performance degradation or errors in complex inheritance schemes .