0% found this document useful (0 votes)
4 views

Constructor

The document provides an overview of constructors in C++, detailing their definition, features, and types, including default, parameterized, and copy constructors. It explains the syntax and provides examples for each type, highlighting the importance of constructors in object initialization and memory management. Additionally, it covers concepts like constructor overloading and destructors, along with a summary and quiz questions for assessment.

Uploaded by

2024-bs-cys-011
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Constructor

The document provides an overview of constructors in C++, detailing their definition, features, and types, including default, parameterized, and copy constructors. It explains the syntax and provides examples for each type, highlighting the importance of constructors in object initialization and memory management. Additionally, it covers concepts like constructor overloading and destructors, along with a summary and quiz questions for assessment.

Uploaded by

2024-bs-cys-011
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Lecture: Constructors in C++

Title: Constructors in C++


Subtitle: Understanding Object Initialization with Constructors

Introduction to Constructors
Definition:

 A constructor is a special member function of a class that initializes objects.


 It has the same name as the class.
 It is automatically invoked when an object is created.

Key Features:

 No return type (not even void).


 Can have parameters but not mandatory.
 Can be overloaded.
 Used to initialize data members.

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:

 A constructor with no parameters.


 Automatically called when an object is created.

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:

 A constructor that takes arguments to initialize object data.

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:

 A constructor that creates a new object as a copy of an existing object.

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;
}

Constructors with Default Values


Definition:

 A constructor that provides default values for parameters.


 If arguments are not provided during object creation, default values are used.

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:

 Multiple constructors with different parameters within the same class.


 The appropriate constructor is selected based on the provided arguments.

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;
}

Slide 11: Summary


 Constructors initialize objects automatically.
 No return type.
 Can be overloaded.
 Copy constructor helps in object cloning.
 Dynamic constructors manage memory allocation.
 Constructors with default values simplify object creation.
 Constructor overloading enables flexibility.
 Destructors clean up resources.

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?

You might also like