c++ viva
c++ viva
In C++, a class is a blueprint for creating objects. It defines the structure and behavior of the objects
by specifying:
1. Data Members: These are variables that hold data specific to an object.
2. Member Functions: These are functions that define the behavior of the objects.
2.What is an object?
In C++, an object is an instance of a class. It represents a concrete entity that is created based on the
blueprint defined by a class. Objects are used to access and manipulate the data members and
member functions defined in the class.
Inheritance is an object-oriented programming feature in C++ that allows one class (the derived
class) to inherit attributes and methods from another class (the base class). It enables code
reuse, hierarchical relationships, and polymorphism.
Types of Inheritance
Hierarchical Inheritance: Multiple derived classes inherit from a single base class.
Types of Polymorphism
Achieved using:
Function Overloading
Operator Overloading
Achieved using:
Virtual Functions
Function Overriding
Operator overloading is a feature in C++ that allows you to redefine the behavior of operators
(e.g., +, -, *, ==) for user-defined types such as classes and structures. It enables operators to
work with objects in a natural way, similar to how they work with built-in data types.
Function overloading is a feature in C++ that allows multiple functions to have the same
name but operate on different types or numbers of parameters. It enables a program to use the
same function name for various tasks, making the code more readable and organized.
Function overriding occurs in C++ when a derived class provides its own definition of a function
that is already defined in its base class. The function in the derived class must have the same
name, return type, and parameters as the one in the base class.
This feature is typically used to implement runtime polymorphism and allows derived classes to
customize or completely redefine the behavior of base class functions.
A destructor in C++ is a special member function of a class that is automatically called when an
object of the class goes out of scope or is explicitly deleted. It is used to perform cleanup
operations, such as releasing resources (e.g., memory, file handles) acquired during the object's
lifetime.
A pointer is a variable in C++ that stores the memory address of another variable. Instead of
holding a direct value, a pointer holds the address where a value is stored, enabling efficient
manipulation and access to data in memory.
1. Null Pointer:
A pointer that does not point to any valid memory location and is typically initialized
to nullptr.
2. Void Pointer:
A pointer that can hold the address of any data type but must be cast to the appropriate type
before dereferencing.
3. Constant Pointer:
A pointer that cannot change the memory address it points to after initialization.
4. Pointer to Constant:
A pointer that can point to different addresses but cannot modify the value at the address it
points to.
6. Dangling Pointer:
A pointer that points to a memory location that has been deallocated or is no longer valid.
7. Wild Pointer:
A pointer that has not been initialized and points to an unknown memory location.
8. Smart Pointer:
A type of pointer in C++ that automatically manages memory
(e.g., std::unique_ptr, std::shared_ptr, std::weak_ptr).
9. Pointer to Pointer:
A pointer that stores the address of another pointer.
A virtual function in C++ is a member function in a base class that can be overridden in a derived
class. It is declared using the virtual keyword in the base class. Virtual functions enable runtime
polymorphism, meaning the function that is executed is determined by the type of the object
being pointed to, rather than the type of the pointer.
A function in C++ is a block of code that performs a specific task and can be called multiple times
in a program. Functions help in breaking down a program into smaller, manageable, and reusable
components. Functions can accept inputs (parameters) and return a result (return value).
1. Built-in Functions:
Examples:
2. User-defined Functions:
cpp
Copy code
void display() { cout << "No parameters, no return value!" << endl; }
cpp
Copy code
cpp
Copy code
int getNumber() { return 42; }
cpp
Copy code
4. Inline Functions:
Functions that are expanded in place where they are called, rather than invoking them.
Declared using the inline keyword.
Example:
cpp
Copy code
5. Recursive Functions:
Example:
cpp
Copy code
6. Friend Functions:
A function that is not a member of a class but can access its private and protected members.
Example:
cpp
Copy code
class MyClass { private: int data; public: MyClass(int val) : data(val) {} friend void
displayData(MyClass obj); }; void displayData(MyClass obj) { cout << "Data: " << obj.data <<
endl; }
7. Virtual Functions:
Functions in a base class that can be overridden in derived classes to achieve runtime
polymorphism.
cpp
Copy code
9. Static Functions:
Functions that belong to the class rather than any specific object. They can access only static
members.
Example:
cpp
Copy code
class MyClass { public: static void display() { cout << "Static function!" << endl; } };
A template in C++ is a powerful feature that allows functions and classes to operate with generic
types. It enables you to write code that can work with different data types without rewriting the
entire code for each type. Templates are especially useful in situations where the logic is the
same, but the data types may vary.
In C++, only one destructor can be created in a class. The destructor's name is always the same
as the class name, preceded by a tilde (~), and it cannot take any arguments or return a value.
A class can have as many constructors as needed, provided they have different parameter
lists (overloaded constructors).