0% found this document useful (0 votes)
42 views11 pages

Structures & Destructures of C++

Uploaded by

saulehbasma92
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views11 pages

Structures & Destructures of C++

Uploaded by

saulehbasma92
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

CONSTRUCTOR

AND
DESTRUCTOR
C++ Language
Constructors
A constructor in C++ is a special method that is automatically called when an object of a class is created.

To create a constructor, use the same name as the class, followed by parentheses ():

EXAMPLE:

#include <iostream>
using namespace std;
class MyClass { // The class
public: // Access specifier
MyClass() { // Constructor
cout << "Hello World!";
}
};
int main() {
MyClass myObj;
return 0;
}
Constructor Parameters ● Constructors can also take parameters (just like regular
functions), which can be useful for setting initial values for
attributes.
● The following class have brand, model and year attributes, and
a constructor with different parameters. Inside the constructor
we set the attributes equal to the constructor parameters
(brand=x, etc). When we call the constructor (by creating an
object of the class), we pass parameters to the constructor,
which will set the value of the corresponding attributes to the
same:
#include <iostream>
using namespace std;

class Car { // The class


public: // Access specifier
string brand; // Attribute
string model; // Attribute
int year; // Attribute
Example

Car(string x, string y, int z) { // Constructor with parameters


brand = x;
model = y;
year = z;
}
};
int main() {
// Create Car objects and call the constructor with different values
Car carObj1("BMW", "X5", 1999);
Car carObj2("Ford", "Mustang", 1969);

// Print values
cout << carObj1.brand << " " << carObj1.model << " " << carObj1.year << "\n";
cout << carObj2.brand << " " << carObj2.model << " " << carObj2.year << "\n";
return 0;
}
EXAMPLE 2:
#include <iostream>
using namespace std;
class Car { // The class
public: // Access specifier
string brand; // Attribute
string model; // Attribute
int year; // Attribute
Car(string x, string y, int z); // Constructor declaration
};
// Constructor definition outside the class
Car::Car(string x, string y, int z) {
brand = x;
model = y;
year = z;
}
int main() {
// Create Car objects and call the constructor with different values
Car carObj1("BMW", "X5", 1999);
Car carObj2("Ford", "Mustang", 1969);
// Print values
cout << carObj1.brand << " " << carObj1.model << " " << carObj1.year
<< "\n";
cout << carObj2.brand << " " << carObj2.model << " " << carObj2.year
<< "\n";
return 0;
}
What is destructor?
Destructor is an instance member function which is invoked
automatically whenever an object is going to destroy. Means, a
destructor is the last function that is going to be called before an
object is destroyed.

The thing to be
noted is that Syntax:
destructor doesn’t ~constructor-name();
destroys an object.
● Destructor function is automatically invoked when the objects
are destroyed.
● It cannot be declared static or const.
Properties of
Destructor:
● The destructor does not have arguments.
● It has no return type not even void.
● An object of a class with a Destructor cannot become a
member of the union.
● A destructor should be declared in the public section of the
class.
● The programmer cannot access the address of destructor.
A destructor function is called automatically when the object goes
out of scope:
When is destructor
(1) the function ends
(2) the program ends
called?

(3) a block containing local variables ends


(4) a delete operator is called
Destructors have same name as the class preceded by a tilde (~)

different from a normal


Destructors don’t take any argument and don’t return anything
How destructors are

member function?
Example:
class String {

private:

char* s;

int size;

public:

String(char*); // constructor

~String(); // destructor };

String::String(char* c)

{ size = strlen(c);

s = new char[size + 1];

strcpy(s, c); }

String::~String() { delete[] s; }
When do we need to write a user-
defined destructor?
Points To Be Noted If we do not write our own destructor in
class, compiler creates a default destructor
Can there be more for us. The default destructor works fine
than one destructor in unless we have dynamically allocated
a class? memory or pointer in class. When a class
contains a pointer to memory allocated in
No, there can only one
class, we should write a destructor to
destructor in a class with
release memory before the class instance is
classname preceded by ~, destroyed. This must be done to avoid
no parameters and no memory leak.
return type.
Thank You
Coming up next:
Access Specifiers
in C++

You might also like