Lab P4 β Understand objects, member functions, and constructors.
Aim
Aim: Demonstrate objects, member functions, and constructors.
Theory
Demonstrate objects, member functions, and constructors.
C++ Program
#include
using namespace std;
class Student {
string name; int roll;
public:
Student(string n,int r){ name=n; roll=r; cout<<"Constructor called\n"; }
void display(){ cout< Expected Output
Constructor called
101 Ravi
Destructor calledExplanation
- Constructor initializes object.
- display() is member function.
- Destructor runs at end.
Viva / Important Questions
Short Questions
- What is constructor?
- Types of constructors?
- When destructor called?
Long Questions
- Explain the program in detail.
- Discuss key concepts demonstrated.
MCQ Practice
1. Constructor has:
- Same name as class
- Return type
- Different name
- None
Answer: Same name as class.
2. Member function is called on:
- Object
- Class
- Type
- None
Answer: Object.
3. Destructor name starts with:
- ~
- !
- #
- *
Answer: ~.
4. Object creation triggers:
- Constructor
- Destructor
- main
- None
Answer: Constructor.
5. this pointer points to:
- Current object
- Class
- Function
- None
Answer: Current object.
Quick Revision
- Constructor init; member fn uses data.
- Destructor cleanup.