0% found this document useful (0 votes)
39 views6 pages

Contructor Final

The document explains constructors in C++, detailing their purpose, syntax, and types, including default, parameterized, and copy constructors. It highlights that constructors are special member functions named after the class, automatically called when an object is created, and used for initializing data members. Additionally, the document provides sample code to illustrate the implementation of different types of constructors.
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)
39 views6 pages

Contructor Final

The document explains constructors in C++, detailing their purpose, syntax, and types, including default, parameterized, and copy constructors. It highlights that constructors are special member functions named after the class, automatically called when an object is created, and used for initializing data members. Additionally, the document provides sample code to illustrate the implementation of different types of constructors.
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/ 6

INDORE INSTITUTE OF SCIENCE AND

TECHNOLOGY

Submitted By Submitted To
Sapna Verma(0818CS231171)
Soumya
Ms. Rati Gupta
Patanaik(0818CS231195)
CONSTRUCTO
R IN C++
WHAT IS CONSTRUCTOR IN C++ ?
• A constructor in C++ is a special member function with exact same name
as the class name. The constructor name is the same as the Class Name.
• Reason: Compiler uses this character to differentiate constructors from the
other member functions of the class.
• A constructor must not declare a return type or void.
• Reason: As it’s automatically called and generally used for initializing
values.They can be defined inside or outside the class definition.
• Automatically calls when an object is created for the class.
• It uses to construct that means to initialize all the data members
(variables) of the class.
SYNTAX OF CONSTRUCTOR

class CLASS_NAME
POINT TO BE REMEMBER
{  Constructors invoke simultaneously
………public : whenever an object of a class creates.
 The compiler will automatically
CLASS_NAME([parameter_list]) //constructor definition
generate a default constructor if no
{ constructor is defined explicitly.
.............  Preferably, constructors use to declare
} //other member functions
the data members(variables). Usually,
not utilized for generating input and
};
output.They also allocate memory at
run time using the ‘new’ operator in C+
+.
 More than one constructor can be
declared as well. It’s
“Constructor Overloading
TYPES OF CONSTRUCTOR

1. Default
.

Constructor
A constructor with no arguments (or
parameters) in the definition is a
default constructor. It is the type of
constructor in C++ usually used to
initialize data members (variables)
with real values.
2.Parameterized
Constructor
Unlike the Default constructor, It
contains parameters (or arguments) in
the constructor definition and 3.Copy
declaration. More than one argument
can also pass through a
parameterized constructor.
Constructor
A copy constructor is the third type
among various types of constructors in
C++. The member function initializes an
object using another object of the same
class. It helps to copy data from one
object to another.
DEFAUL PARAMETERIZE #include <iostream>
using namespace std; COPY
D
#include <iostream> // class name: Rectangle
T
#include <iostream>
using namespace std;
using namespace std; class Rectangle {
private:
// class name: Rectangle
//Class Name: Default_construct class Rectangle { double length;
class Default_construct private: double length; double breadth; public:
{ double breadth; public: // parameterized constructor
public: //parameterized constructor Rectangle(double l, double b) {
int a, b; Rectangle(double l, double b) { length = l; breadth = b; } // copy
// Default Constructor length = l; constructor with a Rectangle object as
Default_construct() breadth = b; } parameter copies data of the obj
{ double calculateArea() { parameter Rectangle(Rectangle &obj) {
a = 100; return length * breadth; length = obj.length; breadth =
b = 200; }}; obj.breadth; } double calculateArea()
}}; int main() { { return length * breadth; }}; int
int main() // create objects to call constructors main() { // create objects to call
{ Default_construct con; Rectangle obj1(10,6); constructorsRectangle
//Object created Rectangle obj2(13,8); obj1(10,6);Rectangle obj2 = obj1; //copy
cout << "Value of a: " << con.a; cout << "Area of Rectangle 1: " << the content using object //print areas of
cout<< "Value of b: " << con.b; obj1.calculateArea(); rectangles cout << "Area of Rectangle 1: "
return 0; cout << "Area of Rectangle 2: " << << obj1.calculateArea(); cout << "Area of
} obj2.calculateArea(); Rectangle 2: " <<
return 0;} obj2.calculateArea(); return 0;}

You might also like