Lab P4 β€” Understand objects, member functions, and constructors.

NBC351 Β· Program 4⏱ 8 min read❓ 5 MCQs

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 called

Explanation

  • Constructor initializes object.
  • display() is member function.
  • Destructor runs at end.

Viva / Important Questions

Short Questions

  1. What is constructor?
  2. Types of constructors?
  3. When destructor called?

Long Questions

  1. Explain the program in detail.
  2. 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.