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

Lecture 3 - Constructor

In C++, a constructor is a special member function that initializes an object's data members upon creation. There are three main types of constructors: default (no arguments), parameterized (with arguments), and copy constructors (copying an existing object). Constructor overloading allows multiple constructors with different parameters for flexibility in object creation.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture 3 - Constructor

In C++, a constructor is a special member function that initializes an object's data members upon creation. There are three main types of constructors: default (no arguments), parameterized (with arguments), and copy constructors (copying an existing object). Constructor overloading allows multiple constructors with different parameters for flexibility in object creation.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Constructor

In C++, a constructor is a special member function of a class that is automatically invoked when an object
of the class is created. It is used to initialize the object's data members. A constructor has the same name as
the class and does not have a return type, not even void.
Here’s how constructors work in C++:
Syntax for a Constructor:
class ClassName {
public:
// Constructor
ClassName() {
// Code to initialize object
}
};
Types of Constructors in C++
1. Default Constructor:
o A constructor that takes no arguments.
o If no constructor is provided by the programmer, the compiler automatically generates a default constructor.
class Car {
public:
string model;
int year;

// Default constructor
Car() {
model = "Unknown";
year = 0;
}

void display() {
cout << "Model: " << model << ", Year: " << year << endl;
}
};

int main() {
Car myCar; // Default constructor is called here
myCar.display();
return 0;
}
1. Parameterized Constructor:
o A constructor that takes arguments to initialize the object with specific values when it is created.
class Car {
public:
string model;
int year;

// Parameterized constructor
Car(string m, int y) {
model = m;
year = y;
}

void display() {
cout << "Model: " << model << ", Year: " << year << endl;
}
};

int main() {
Car myCar("Toyota", 2021); // Parameterized constructor is called here
myCar.display();
return 0;

}
1. Copy Constructor:
o A constructor that initializes a new object as a copy of an existing object.
o It is invoked when a new object is created from an existing object, typically when passing objects by value or returning objects from functions.
class Car {
public:
string model;
int year;

// Parameterized constructor
Car(string m, int y) {
model = m;
year = y;
}

// Copy constructor
Car(const Car &other) {
model = other.model;
year = other.year;
}

void display() {
cout << "Model: " << model << ", Year: " << year << endl;
}
};

int main() {
Car car1("Toyota", 2021); // Parameterized constructor is called here
Car car2 = car1; // Copy constructor is called here
car2.display();
return 0;
}
1. Constructor Overloading:
o You can define multiple constructors with different parameters to provide flexibility in object creation.
class Car {
public:
string model;
int year;

// Default constructor
Car() {
model = "Unknown";
year = 0;
}

// Parameterized constructor
Car(string m, int y) {
model = m;
year = y;
}

void display() {
cout << "Model: " << model << ", Year: " << year << endl;
}
};

int main() {
Car car1; // Calls the default constructor
car1.display();

Car car2("Honda", 2022); // Calls the parameterized constructor


car2.display();

return 0;
}
Key Points:
 Constructor Initialization: Constructors are used to initialize an object's data members when an object
is created.
 Constructor Overloading: You can have more than one constructor, provided they have different
parameters.
 Implicit and Explicit Constructor Calls: The default constructor is called implicitly when an object is
created without arguments, while parameterized constructors are called when arguments are provided.

You might also like