1.
Constructor Types
Explanation:
A constructor is a special function in a class that automatically executes when an object is
created. It has the same name as the class and has no return type.
Types of Constructors:
Default Constructor: Takes no parameters.
Parameterized Constructor: Takes arguments to initialize the object.
(Other types include copy and dynamic constructors — let me know if you want those
too!)
Program 1: Default Constructor
#include <iostream>
using namespace std;
class Car {
public:
// Default constructor (no parameters)
Car() {
cout << "Default Constructor Called" << endl;
}
};
int main() {
Car c1; // Object creation calls the constructor
return 0;
}
Program 2: Parameterized Constructor
#include <iostream>
using namespace std;
class Car {
public:
string y;
// Parameterized constructor
Car(string m) {
y = m;
cout << "Car Model: " << model << endl;
}
};
int main() {
Car c1("Honda"); // Passes "Honda" to constructor
return 0;
}
2. Destructor
Explanation:
A destructor is a special function that is called automatically when an object goes out of
scope or is explicitly deleted. It frees resources and has:
Same name as class prefixed with ~
No parameters and no return type
Program 1: Basic Destructor
#include <iostream>
using namespace std;
class Demo {
public:
Demo() {
cout << "Constructor called" << endl;
}
// Destructor
~Demo() {
cout << "Destructor called" << endl;
}
};
int main() {
Demo d1; // Constructor and Destructor will be called automatically
return 0;
}
Simple Class to Store and Display a Student's Info
#include <iostream>
using namespace std;
class Student {
private:
string name;
int roll;
public:
// Constructor
Student() {
cout << "Constructor called!" << endl;
name = "Unknown";
roll = 0;
}
// Member function to set data
void setData(string n, int r) {
name = n;
roll = r;
}
// Member function to display data
void display() {
cout << "Name: " << name << ", Roll Number: " << roll << endl;
}
// Destructor
~Student() {
cout << "Destructor called for " << name << endl;
}
};
int main() {
// Create object of Student class
Student s1;
// Set student data
s1.setData("Anjali", 101);
// Display student data
s1.display();
return 0;
}
3. Basics of Exception Handling
Explanation:
Exception Handling is used to manage runtime errors and prevent program crashes.
Keywords:
try: Block of code to test for errors
catch: Handles the error
throw: Generates an error
Program 1: Simple Try-Catch
#include <iostream>
using namespace std;
int main() {
try {
int age = 15;
if (age < 18)
throw age; // Throwing an exception
cout << "Access granted" << endl;
} catch (int x) {
cout << "Access denied. Age is " << x << endl;
}
return 0;
}
Program 2: Division by Zero
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 0;
try {
if (b == 0)
throw "Cannot divide by zero!";
cout << "Result: " << a / b << endl;
} catch (const char* msg) {
cout << "Exception caught: " << msg << endl;
}
return 0;
}
4. Exception Handling Mechanism (throw & catch)
Explanation:
The throw-catch mechanism in C++ lets us explicitly raise errors (throw) and handle them
in catch blocks. These may be of:
int, char, or string types
User-defined exception types
Program 1: Throw from a Function
#include <iostream>
using namespace std;
// Function that throws exception
void check(int num) {
if (num < 0)
throw "Negative number not allowed!";
cout << "Number is: " << num << endl;
}
int main() {
try {
check(-5); // This will throw an exception
} catch (const char* msg) {
cout << "Caught Exception: " << msg << endl;
}
return 0;
}
Program 2: Multiple Catch Blocks
#include <iostream>
using namespace std;
int main() {
try {
int choice = 1;
if (choice == 1)
throw 10; // Throwing an integer
else
throw 'A'; // Throwing a character
} catch (int x) {
cout << "Caught Integer Exception: " << x << endl;
} catch (...) {
cout << "Caught an unknown exception" << endl;
}
return 0;
}