Constructor
Constructor
Introduction to Constructors
Definition:
Key Features:
Syntax of a Constructor
class ClassName {
public:
ClassName() { // Constructor
// Initialization code
}
};
Example:
#include <iostream>
using namespace std;
class Car {
public:
Car() { // Constructor
cout << "Car object created!" << endl;
}
};
int main() {
Car myCar; // Constructor is automatically called
return 0;
}
Types of Constructors
1. Default Constructor (No parameters)
2. Parameterized Constructor (With parameters)
3. Copy Constructor (Creates a copy of an object)
4. Constructors with Default Values
5. Constructor Overloading
6. Destructor (Opposite of constructor, releases resources)
Default Constructor
Definition:
Example 1:
class Student {
public:
Student() {
cout << "Default Constructor Called!" << endl;
}
};
int main() {
Student s1;
return 0;
}
Example 2:
class Animal {
public:
Animal() {
cout << "An animal is created!" << endl;
}
};
int main() {
Animal a1;
return 0;
}
Example 3:
class Book {
public:
Book() {
cout << "A book object is created." << endl;
}
};
int main() {
Book b1;
return 0;
}
Parameterized Constructor
Definition:
Example 1:
class Car {
string brand;
public:
Car(string b) {
brand = b;
cout << "Car brand: " << brand << endl;
}
};
int main() {
Car c1("Toyota");
return 0;
}
Example 2:
class Rectangle {
int length, breadth;
public:
Rectangle(int l, int b) {
length = l;
breadth = b;
cout << "Rectangle Area: " << length * breadth << endl;
}
};
int main() {
Rectangle r1(4, 5);
return 0;
}
Example 3:
class Laptop {
string model;
int price;
public:
Laptop(string m, int p) {
model = m;
price = p;
cout << "Laptop Model: " << model << " Price: " << price << endl;
}
};
int main() {
Laptop l1("Dell", 500);
return 0;
}
Copy Constructor
Definition:
Example 1:
class Number {
int num;
public:
Number(int n) { num = n; }
Number(const Number &obj) {
num = obj.num;
cout << "Copied Value: " << num << endl;
}
};
int main() {
Number n1(10);
Number n2 = n1;
return 0;
}
Example 2:
class Point {
int x, y;
public:
Point(int a, int b) { x = a; y = b; }
Point(const Point &p) {
x = p.x;
y = p.y;
cout << "Copied Point: (" << x << ", " << y << ")" << endl;
}
};
int main() {
Point p1(3, 4);
Point p2 = p1;
return 0;
}
Example 3:
class Student {
string name;
public:
Student(string n) { name = n; }
Student(const Student &s) {
name = s.name;
cout << "Copied Student Name: " << name << endl;
}
};
int main() {
Student s1("John");
Student s2 = s1;
return 0;
}
Example 1:
class Employee {
string name;
int age;
public:
Employee(string n = "Unknown", int a = 30) {
name = n;
age = a;
}
void display() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
int main() {
Employee e1; // Uses default values
Employee e2("Alice", 28);
e1.display();
e2.display();
return 0;
}
Constructor Overloading
Definition:
Example 1:
class Shape {
public:
Shape() {
cout << "Default constructor: No shape defined." << endl;
}
Shape(int side) {
cout << "Square with side: " << side << endl;
}
Shape(int length, int breadth) {
cout << "Rectangle with dimensions: " << length << "x" << breadth <<
endl;
}
};
int main() {
Shape s1;
Shape s2(5);
Shape s3(4, 7);
return 0;
}
Quiz Questions
1. What is a constructor in C++?
2. Can a constructor return a value?
3. What is a copy constructor used for?
4. What is the difference between a parameterized and default constructor?
5. How does a destructor work?
6. What is constructor overloading?
7. How are default values assigned in a constructor?