OOPS_Questions
OOPS_Questions
2. What are the differences between the constructor and the method?
Constructor:
- A special type of function that is automatically called when an object is created.
- It is used to initialize the object.
- It has the same name as the class and no return type.
Method:
- A regular function defined inside a class.
- It is called explicitly to perform a certain task or operation.
- It can have any name and must have a return type.
Object:
- An object is an instance of a class, representing a specific example of the data and behavior defined by the class.
- It occupies memory and allows you to access class methods and attributes.
7. Does C++ compiler create a default constructor when we write our own?
In C++, the compiler by default creates a default constructor for every class. But if we define our own constructor,
the compiler doesn’t create the default constructor.
13. When should the destructor use delete to free the memory?
If the object is created using `new` or the constructor uses `new` to allocate memory that resides in the heap
memory or the free store, the destructor should use `delete` to free the memory.
26. What are the differences between Polymorphism and Inheritance in C++?
The differences between polymorphism and inheritance in C++ are as follows:
a. Inheritance represents the parent-child relationship between two classes. On the other hand, polymorphism
takes advantage of that relationship to make the program more dynamic.
b. Inheritance helps in code reusability in the child class by inheriting behavior from the parent class. On the other
hand, polymorphism enables the child class to redefine already defined behavior inside the parent class. Without
polymorphism, a child class can’t execute its own behavior.
Member Function:
- A part of the class.
- Can directly access all members (private, protected, and public) of the class.
- Defined within the class.
- Has a `this` pointer that points to the object for which it is called.
Questions :
1. Question 1
2. Question 2
3. Question 3
4. Question 4
5. Question 5
32. What is the output of this program?
Answer: 20
We are using the friend function for print width and multiplied the width value by 2, so we got the output as 20.