0% found this document useful (0 votes)
15 views2 pages

Cons of Prog

The document provides an overview of constructors in C++, detailing the default, parameterized, and copy constructors. It explains their roles in object initialization, including syntax examples and the concept of constructor overloading. Additionally, it introduces destructors and their purpose in resource management when an object goes out of scope.
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)
15 views2 pages

Cons of Prog

The document provides an overview of constructors in C++, detailing the default, parameterized, and copy constructors. It explains their roles in object initialization, including syntax examples and the concept of constructor overloading. Additionally, it introduces destructors and their purpose in resource management when an object goes out of scope.
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/ 2

Constructor in C++ 1.

Default Constructor in C++


A constructor in c++ is a special type of method or member function that is called automatically at the The default constructor in c++ does not contain any arguments and is invoked or called at the time of
time of object creation. Its main purpose is to initialize the object with values that are specified by the object creation. We can either specify the default constructor or it will be automatically called at the time
user at the time of creation and if the user doesn’t specify any value at the time of creation, then it will of object creation.
assign or initialize the object with default values.
#include <iostream>
Constructors serve as the building blocks of object creation in C++. They initialize the object’s attributes using namespace std;
class Class1
and prepare it for use. C++ offers various types of constructors, each with distinct roles and {
functionalities. public:
Class1()
***The constructor has the same name as that of the class name which is defined or mentioned by the {
user as this name is used by the compiler to have a distinguishing feature between constructor in c++ cout<<"My Default Constructor has been created"<<endl;
}
and other functions of the class. };
int main(void)
***The constructor does not have any return type nor data type or void. {
Class1 object1; //creating an object of Class1
***We can define them either outside or inside the class definition.
return 0;
}
***We do not need to call the constructor in c++ specifically as it is called automatically whenever an
object is created. It is known as a constructor in c++ because it constructs the values. Explanation: When we declare the object the default constructor is called as we have created the object.
***If you define any constructor explicitly, then the compiler will not provide default constructor and
you will have to define it yourself. 2. Parameterized Constructor in C++
A parameterized constructor is invoked when an object of the class is created using the new keyword,
Syntax for Constructor: and arguments are passed to the constructor that match its parameter list. We can pass these arguments
for object initialization during the creation of the object. We pass the parameter in the same way as
Inside the class
passing the arguments to any function. We have to define the parameterized constructor in c++ explicitly
Constructor(List of parameters){ it will not be called by default.
//definition of constructor *** This is a constructor with passed arguments.
} ***We can have more than one parameterized constructor in C++.
Outside the class
#include<iostream>
Class_name::Constructor(List of parameters){ using namespace std;
//definition of constructor
} class Rectangle {
private:
int length;
Types of Constructors int height;

public:
Rectangle(int l, int h) {
length = l;
height = h;
}
int calculateArea() {
return length * height;
}
};

int main() {
Rectangle rec1(22, 44); //Parameters are passed to the created object
cout << "Area of Rectangle 1: " << rec1.calculateArea() << endl;
1

2
Page

Page
return 0;
}
Explanation: We are passing the parameters while creating the object and in the parametrized
Destructor in C++
constructor we are initializing the object values with the user-defined values and after that performing A destructor is called when an object of a class exits its scope or the delete expression is applied to a
the function to calculate the area of the rectangle. reference to an object of that class.
*** A parameterized constructor accepts parameters and initializes attributes with specific values passed
as arguments. A default constructor has no parameters and initializes attributes with default values. A destructor can neither take nor return any parameters and will have the exact same name as the class
*** a class can have multiple constructors, a concept known as constructor overloading. Each prefixed with a tilde (). Before exiting a program, shutting files, releasing memory, and other resources
constructor can have a different parameter list, allowing flexibility in object initialization. can be released with the destructor.

3. Copy Constructor in C++ class Habari {


A copy constructor in c++ is used to create a copy or to initialize the object with the help of the already public:
existing or created object. ~Habari() {
***A copy constructor is used to copy one object to another object. cout << "Your destructor is executed"<< endl;
cout << "Bye";
#include<iostream>
using namespace std; }
};
class Rectangle { main()
private: {
int length; Habari leo;
int height;
return 0;
public: }
Rectangle(int l, int h) {
length = l;
Syntax for declaring a pointer outside the class
height = h;
} Classname ::~pointer(){
//beginning of a copy constructor }
Rectangle(Rectangle &obj) { Definitions
length = obj.length; Constructor is a method for a class that gets called automatically whenever an object of the class is
height = obj.height; created. It is used to give the class’s data initial values.
} Copy Constructor is a constructor that takes another object of the same class as its reference parameter,
int calculateArea() { and makes the new object an exact copy of the argument.
return length * height; Default Constructor is a constructor that takes no parameters, and gives the class’s data default initial
} values. It is always called for each array element when an array of the class’s objects is created.
}; Destructor is a method for a class that gets called automatically whenever an object of the class is
deleted. It is used to delete any memory that the class has dynamically allocated.
int main() { Static member is a member variable of a given class that is shared by all objects of that class, instead of
Rectangle rec1(22, 44); //Parameters are passed to the created object being unique to a particular object.
Rectangle rec2=rec1;
cout << "Area of Rectangle 1: " << rec1.calculateArea() << endl; class myClass{
cout << "Area of Rectangle 2: " << rec2.calculateArea() << endl; private:
int x, y;
return 0; public:
} myClass(); //default constructor
myClass(int x0, int y0); //Initializing constructor
Explanation: We have a class named rectangle and, in the class, we have a parameterized constructor myClass(const myClass &pt); };//Copy constructor
with the same name itself that is initializing the value of the object below we have created the object with
3

4
the help of parameterized constructor and the second object we have created with the help of copy
Page

Page
constructor in c++.

You might also like