0% found this document useful (0 votes)
25 views17 pages

Constructors in C++ Concept

concept c++

Uploaded by

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

Constructors in C++ Concept

concept c++

Uploaded by

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

Constructors in C++

&
Destructors in C++
What is constructor?
• A constructor is a member function of a class
which initializes objects of a class. In C++,
Constructor is automatically called when
object(instance of class) is created. It is special
member function of the class.
• To create a constructor, use the same name as
the class, followed by parentheses ().
Example
• class MyClass { // The class
public: // Access specifier
MyClass() { // Constructor
cout << "Hello World!";
}
};
int main() {
MyClass Obj; // Create an object of MyClass
//(this will call the constructor)
return 0;
}
Constructor Rules
• The constructor has the same name as the
class and it is always public.
• Constructors can also take parameters (just
like regular functions), which can be useful for
setting initial values for attributes.
• Constructor can not be inherited, but from
the derived class we can call the base class
constructors.
Constructor Rules
• Constructors do not have return type.
• Constructor can not be virtual.
• It is not possible to refers to the address of
constructors.
• It is not possible to use the constructor as
member of union if the object is created with
constructor.
Constructor
• Just like functions, constructors can also be
defined outside the class. First, declare the
constructor inside the class, and then define it
outside of the class by specifying the name of
the class, followed by the scope resolution ::
operator, followed by the name of the
constructor (which is the same as the class):
Example
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;
}
How constructors are different from a
normal member function?
• A constructor is different from normal functions
in following ways:
• Constructor has same name as the class itself
• Constructors don’t have return type
• A constructor is automatically called when an
object is created.
• If we do not specify a constructor, C++ compiler
generates a default constructor for us (expects no
parameters and has an empty body).
Constructors in C++
1. Default Constructors
• A constructor without any arguments or with
default value for every argument, is said to
be default constructor.
• The compiler will implicitly declare default
constructor if not provided by programmer,
will define it when in need.
• It has no parameters.
2. Parameterized Constructors
• It is possible to pass arguments to
constructors. Typically, these arguments help
initialize an object when it is created. To
create a parameterized constructor, simply
add parameters to it the way you would to any
other function. When you define the
constructor’s body, use the parameters to
initialize the object.
Parameterized Constructors
Uses of Parameterized constructor:
• It is used to initialize the various data
elements of different objects with different
values when they are created.
• It is used to overload constructors.
Can we have more than one constructors in a
class?
Yes, It is called Constructor Overloading.
3. Copy Constructor
• A copy constructor is a member function
which initializes an object using another
object of the same class.
• A copy constructor has the following general
function prototype:
ClassName (const ClassName &old_obj);
When is copy constructor called?
In C++, a Copy Constructor may be called in
following cases:
1. When an object of the class is returned by
value.
2. When an object of the class is passed (to a
function) by value as an argument.
3. When an object is constructed based on
another object of the same class.
4. When the compiler generates a temporary
object.
Destructors in C++
• What is destructor?
Destructor is a member function which
destructs or deletes an object.
• When is destructor called?
A destructor function is called automatically
when the object goes out of scope:
(1) the function ends
(2) the program ends
(3) a block containing local variables ends
(4) a delete operator is called
Destructors in C++
• How destructors are different from a normal
member function?
Destructors have same name as the class
preceded by a tilde (~)
Destructors don’t take any argument and don’t
return anything.
• Can there be more than one destructor in a
class?
No, there can only one destructor in a class with
classname preceded by ~, no parameters and no
return type.
Destructors in C++
• When do we need to write a user-defined
destructor?
When a class contains a pointer to memory
allocated in class, we should write a destructor to
release memory before the class instance is
destroyed. This must be done to avoid memory
leak.
• Can a destructor be virtual?
Yes, In fact, it is always a good idea to make
destructors virtual in base class when we have a
virtual function.

You might also like