0% found this document useful (0 votes)
8 views

c++ viva

Uploaded by

ayushckp7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

c++ viva

Uploaded by

ayushckp7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

1.What is class?

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.

3.What is inheritance? What are the different types of inheritance?

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

 Single Inheritance: One base class, one derived class.

 Multiple Inheritance: A derived class inherits from multiple base classes.

 Multilevel Inheritance: A class inherits from a derived class.

 Hierarchical Inheritance: Multiple derived classes inherit from a single base class.

 Hybrid Inheritance: A mix of two or more types of inheritance.

4.What is polymorphism? What are the different types of polymorphism?

Polymorphism is a key concept in object-oriented programming that allows a single interface or


function to behave differently based on the context. The term polymorphism means "many
forms". In C++, it allows methods or operators to perform different tasks depending on their
usage.

Types of Polymorphism

Polymorphism in C++ is broadly categorized into two types:

1. Compile-Time Polymorphism (Static Binding):

 The behavior is determined at compile time.

 Achieved using:

 Function Overloading

 Operator Overloading

2. Run-Time Polymorphism (Dynamic Binding):

 The behavior is determined at runtime.

 Achieved using:
 Virtual Functions

 Function Overriding

5. What is Operator Overloading in C++?

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.

6. What is Function Overloading in C++?

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.

7. What is Function Overriding in C++?

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.

8. What is Runtime Polymorphism in C++?

Runtime polymorphism is a type of polymorphism that is resolved during the program's


execution (runtime). It allows a derived class to override a base class method, enabling the
program to decide at runtime which function to invoke. This is primarily achieved
through function overriding and the use of virtual functions in C++.

9. What is a Destructor in C++?

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.

10. What is a Constructor in C++?

A constructor is a special member function of a class that is automatically invoked when an


object of the class is created. It is primarily used to initialize objects and allocate resources
required for the object’s functionality.

11. What is a Parameterized Constructor in C++?

A parameterized constructor is a constructor that accepts one or more arguments (parameters)


when an object of the class is created. It allows you to initialize the object with specific values at
the time of its creation.

12.What is dynamic binding?

13.What is static binding ?


14.what is pointers?

What are Pointers in C++?

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.

What are the types of pointers?

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.

5. Constant Pointer to Constant:


A pointer that cannot change the memory address it points to nor 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.

10. Function Pointer:


A pointer that stores the address of a function, allowing dynamic function calls.

11. Dynamic Pointer:


A pointer used for dynamically allocated memory, managed through new and delete.

12. This Pointer:


An implicit pointer in non-static member functions that points to the current object of the
class.

15.Can a child class access members/data of a private class?


In C++, a child class cannot directly access the private members or data of its parent class. This
is because private members are accessible only within the class in which they are declared.
However, the child class can access the private members of the parent class
through public or protected member functions or by using protected access specifier.

16.  What is a virtual function?

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.

17. What is a function? What are the different types of functions? 

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).

Types of Functions in C++:

1. Built-in Functions:

 These are predefined functions provided by C++ libraries.

 Examples:

 sqrt() for square root

 pow() for power calculation

 cout and cin for input/output

2. User-defined Functions:

 These are custom functions created by the programmer.

3. Function Based on Parameter and Return Types:

 Functions without parameters and without return value:

cpp

Copy code

void display() { cout << "No parameters, no return value!" << endl; }

 Functions with parameters but no return value:

cpp

Copy code

void printMessage(string message) { cout << message << endl; }

 Functions without parameters but with return value:

cpp

Copy code
int getNumber() { return 42; }

 Functions with parameters and return value:

cpp

Copy code

int add(int a, int b) { return a + b; }

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

inline int square(int x) { return x * x; }

5. Recursive Functions:

 Functions that call themselves to solve a problem in smaller steps.

 Example:

cpp

Copy code

int factorial(int n) { if (n <= 1) return 1; return n * factorial(n - 1); }

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.

 Declared using the virtual keyword.

8. Pure Virtual Functions:

 Declared in abstract classes and must be implemented by derived classes.


 Example:

cpp

Copy code

class AbstractClass { public: virtual void show() = 0; // Pure virtual function };

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; } };

18. What is the difference between parameter and argument?

Parameter vs Argument in C++

Aspect Parameter Argument

An argument is the actual value


A parameter is a variable listed in the passed to the function when it is
Definition function's declaration or definition. called.

Exists at the point where the function


Scope Exists only inside the function. is called.

19.  What is a template?

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.

20.  How many destructors can be created in a class?

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.

21.  How many constructors can be created in a class?

 A class can have as many constructors as needed, provided they have different parameter
lists (overloaded constructors).

 The constructors can include:

 Default Constructor: Takes no arguments or has default arguments.

 Parameterized Constructor: Takes one or more arguments.

 Copy Constructor: Used to initialize a new object as a copy of an existing object.

You might also like