Constructor Ppt(Madhur,Kashish,Kartikay,Raaghav)

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

Constructor With

Default Argument,
Multiple Constructor
In a Class ,
Copy Constructor

BY:- Madhur Bansal, Raaghav Ahuja,


Kashish Malik, Kartikay Sharma

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.

Overloaded constructors essentially have the same name (exact


name of the class) and different by number and type of arguments.

A constructor is called depending upon the number and type


of arguments passed.

While creating the object, arguments must be passed to let


compiler know, which constructor needs toa be called.

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.

Constructors cannot be overloaded based on const or


volatile qualifiers: You can't have two constructors where
one takes a const object and the other takes a non-const
object.

Ambiguity in Constructor Selection: If a constructor is provided


with parameters that can be matched by more than one
overloaded constructor, C++ might not be able to decide
which constructor to call, leading to ambiguity.
6
Example
#include <iostream>
void display(){
using namespace std;
cout<< area<< endl;
}
class MyClass
};
{
public:
int main()
float area;
{
MyClass obj1;
MyClass(){
MyClass obj2( 10, 20);
area = 0;
}
obj1.display();
obj2.display();
MyClass(int a, int b){
return 0;
area = a * b;
}
}
7
Class MyClass

Object Creation

Obj1() & Obj2()

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

A copy constructor in C++ is a special type of


constructor that initializes an object using
another object of the same class. It is used to
create a duplicate copy of an existing object.

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

Default copy User Define


constructor 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.

Syntax : class_Name(const class_Name &object_name);

Example : class MyClass { public: MyClass(const MyClass& other) { } };

Default Copy Constructor


The compiler defines default copy constructor if the user defines no copy
constructor compiler supplies its constructor

Example : Sample obj2(obj1); or obj2=obj1;

18
19

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy