Constructor Ppt(Madhur,Kashish,Kartikay,Raaghav)
Constructor Ppt(Madhur,Kashish,Kartikay,Raaghav)
Constructor Ppt(Madhur,Kashish,Kartikay,Raaghav)
Default Argument,
Multiple Constructor
In a Class ,
Copy Constructor
1
CONSTRUCTORS
Constructor in C++ is a special method that is
invoked automatically at the time an object of a
class is created.
Syntax of
Constructor:
<class-name> (){
... // parameters
}
2
CONSTRUCTORS
Example of Constructor:
class MyClass {
public:
MyClass() {
cout << "Hello World!";
}
};
int main() {
MyClass myObj;
return 0;
}
3
Constructor Overloading
In C++, We can have more than one constructor in a class with same
name, as long as each has a different list of arguments. This concept
is known as Constructor Overloading and is quite similar to function
overloading.
4
Advantages:
Flexibility: Allows for different ways to initialize objects
based on the data available.
Code reusability: Reduces code duplication by allowing
the reuse of constructor logic.
Readability: Simplifies code by providing multiple
constructors for different initialization scenarios.
Maintainability: Makes code easier to maintain by
clearly separating different initialization options.
5
Limitation:
No Overloading by Return Type: Unlike regular function overloading,
constructors cannot be overloaded by their return type. In C++,
constructors don’t have a return type.
Object Creation
Constructors:
MyClass()
MyClass(int a,int b)
Constructor Selection
Initialization
8
2.
CONSTRUCTOR WITH
DEFAULT ARGUMENT
9
CONSTRUCTOR WITH DEFAULT
ARGUMENT
2.
Default arguments of the
constructor are those which are
provided in the constructor
declaration. If the values are not
provided when calling the
constructor the constructor uses the
default arguments automatically
10
CONSTRUCTOR WITH DEFAULT
ARGUMENT
2.
There are two ways to define default
constructor:
Define a constructive with no arguments
Time();
provide default values for all arguments
Time(int x=10,int y=20);
11
class Simple {
int data1;
int data2;
public:
1. Simple(int a = 10, int b = 9) { // constructor takes two
parameters: a and b with default values 10 and 9
data1 = a;
data2 = b; // value of a & b are assigned to the
Example variables data1 and data2
}
void printData() {
std::cout << "The value of data1, data2 is " << data1
<< ", " << data2 << std::endl;
}
};
int main() {
Simple obj1; // creates an object obj1 with default
values
obj1.printData();
return 0;
12
}
1.
Example
13
Online compiler: scaler.com is used*
COPY CONSTRUCTOR
14
2. COPY CONSTRUCTOR
15
#include <iostream>
using namespace std;
2. class Sample {
int id;
public: // public identifier used
Example Sample(int x) { id = x; } // value of x is assigned to the
variables
void display() { // function to print id variable
cout << "ID=" << id;
}
};
int main(){
Sample obj1(10); // object is created
obj1.display();
cout << endl;
// default copy constructor used in next line
Sample obj2(obj1); // or obj2=obj1;
obj2.display();
return 0;
16
}
2.
Type of copy
constructor
17
2. User Define Copy Constructor
You can define your own copy constructor to handle the copying process.
This allows you to control the copying behavior.
18
19