SlideShare a Scribd company logo
2
Most read
3
Most read
7
Most read
BY
N.RUBA
ASST.PROF/DEPT. OF CA,
BONSECOURS COLLEGE FOR WOMEN,
THANJAVUR.
 Constructor is a special member function of
a class that initializes the object of the
class.
 Constructor name is same as class name
and it doesn’t have a return type.
#include <iostream>
using namespace std;
class number
{
public:
int num;
char ch;
number()
{
num = 100;
ch = 'A';
}
};
int main()
{
Number obj;// create the object of class,
cout<<"num: "<<obj.num<<endl;// access data
//members using object
cout<<"ch: "<<obj.ch;
return 0;
}
Output:
num: 100
ch: A
 Constructor is different from member function of the
class.
1) Constructor doesn’t have a return type.
Member function has a return type.
2) Constructor is automatically called when we
create the object of the class.
 Member function needs to be called explicitly using
object
of class.
3) When we do not create any constructor in our
class, C++ compiler generates a default constructor
and insert it into our code. The same does not apply
to member functions.
This is how a compiler generated default
constructor looks:
class XYZ
{
....
XYZ()
{
----
}
};
There are two types of constructor in C++.
1) Default constructor
2) Parameterized constructor
1) Default Constructor
A default constructor doesn’t have any arguments (or parameters)
#include <iostream>
using namespace std;
class Website{
public:
//Default constructor
Website()
{
cout<<"Welcome to Bonsecours"<<endl;
}
};
int main(void){
Website obj1;// creating two objects of class Website.
Website obj2;// constructor should have been invoked
twice.
return 0;
}
Output:
Welcome to Bonsecourss
Welcome to Bonsecours
When you don’t specify any constructor in the class, a
default constructor with no code (empty body) would be
inserted into your code by compiler.
 Constructors with parameters are known as
Parameterized constructors. These type of
constructor allows us to pass arguments while
object creation.
Parameterized Constructor:
XYZ(int a, int b)
{
}
...
XYZ obj(10, 20);
Example:
#include <iostream>
using namespace std;
class Add
{
public:
Add(int num1, int num2) //Parameterized constructor
{
cout<<(num1+num2)<<endl;
}
};
int main(void)
{
Add obj1(10, 25);//method 1:for creating object(implicit)
Add obj2 = Add(50, 40);// method 2:for creating
object(explicit)
return 0;
}
Output:
35
90
Constructors in C++

More Related Content

PPTX
Constructor and Types of Constructors
PPTX
Tower Of Hanoi
DOCX
srs for railway reservation system
PPTX
Encapsulation
PPTX
Ppt semi conductor
PPTX
Animal distribution
PPTX
Queue in Data Structure
PDF
Attendance management system project report.
Constructor and Types of Constructors
Tower Of Hanoi
srs for railway reservation system
Encapsulation
Ppt semi conductor
Animal distribution
Queue in Data Structure
Attendance management system project report.

What's hot (20)

PDF
Function overloading ppt
PDF
Constructor and Destructor
PPTX
classes and objects in C++
PPTX
07. Virtual Functions
PPS
Wrapper class
PPTX
Constructor in java
PPTX
Static Data Members and Member Functions
PPT
Class and object in C++
PPT
Basic concept of OOP's
PPT
Operator Overloading
PPTX
Constructor in java
PPT
Strings
PPTX
Basics of Object Oriented Programming in Python
PPTX
Strings in c++
PPTX
Basic Concepts of OOPs (Object Oriented Programming in Java)
PPTX
Constructor and destructor
PPTX
Member Function in C++
PPT
structure and union
PPTX
Stream classes in C++
PDF
Constructors and Destructors
Function overloading ppt
Constructor and Destructor
classes and objects in C++
07. Virtual Functions
Wrapper class
Constructor in java
Static Data Members and Member Functions
Class and object in C++
Basic concept of OOP's
Operator Overloading
Constructor in java
Strings
Basics of Object Oriented Programming in Python
Strings in c++
Basic Concepts of OOPs (Object Oriented Programming in Java)
Constructor and destructor
Member Function in C++
structure and union
Stream classes in C++
Constructors and Destructors
Ad

Similar to Constructors in C++ (20)

PPTX
Constructor&amp; destructor
PPT
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
PPTX
constructors.pptx
PPTX
Constructor and destructor in oop
PPTX
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
PDF
chapter-9-constructors.pdf
PPTX
An introduction to Constructors and destructors in c++ .pptx
PDF
Constructors & Destructors [Compatibility Mode].pdf
PPTX
Constructor and Destructor
PDF
Constructors in Java (2).pdf
PPT
Constructors and destructors in C++
PPTX
PPTX
Constructor and desturctor
PPTX
OOPs & C++ UNIT 3
PDF
classes and objects.pdfggggggggffffffffgggf
PPTX
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
PPT
Constructor
PPT
Core java day4
PPTX
Java Constructor
PPT
ConsTRUCTION AND DESTRUCTION
Constructor&amp; destructor
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
constructors.pptx
Constructor and destructor in oop
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
chapter-9-constructors.pdf
An introduction to Constructors and destructors in c++ .pptx
Constructors & Destructors [Compatibility Mode].pdf
Constructor and Destructor
Constructors in Java (2).pdf
Constructors and destructors in C++
Constructor and desturctor
OOPs & C++ UNIT 3
classes and objects.pdfggggggggffffffffgggf
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
Constructor
Core java day4
Java Constructor
ConsTRUCTION AND DESTRUCTION
Ad

More from RubaNagarajan (19)

PPTX
Computer graphics-CRT.pptx
PPTX
Matrix representation- CG.pptx
PPTX
Personality development.pptx
PPTX
TRANSFORMATION-CG.pptx
PPTX
dda algorithm-cg.pptx
PPTX
line attributes.pptx
PPT
Java files and io streams
PPTX
Java -Exception handlingunit-iv
PPTX
Java Programming
PPTX
Features of java unit 1
PPTX
Introduction to Java -unit-1
PPTX
Risks in cc
PPTX
Dreamweaver
PPT
Working principles of internet
PPT
Coreldraw
PPT
C programming
PPT
OPERATING SYSTEM
PDF
Virtualization in cloud computing
PDF
Cloud computing technology
Computer graphics-CRT.pptx
Matrix representation- CG.pptx
Personality development.pptx
TRANSFORMATION-CG.pptx
dda algorithm-cg.pptx
line attributes.pptx
Java files and io streams
Java -Exception handlingunit-iv
Java Programming
Features of java unit 1
Introduction to Java -unit-1
Risks in cc
Dreamweaver
Working principles of internet
Coreldraw
C programming
OPERATING SYSTEM
Virtualization in cloud computing
Cloud computing technology

Recently uploaded (20)

PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Basic Mud Logging Guide for educational purpose
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Classroom Observation Tools for Teachers
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Pharma ospi slides which help in ospi learning
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
Lesson notes of climatology university.
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
master seminar digital applications in india
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Supply Chain Operations Speaking Notes -ICLT Program
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Basic Mud Logging Guide for educational purpose
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Classroom Observation Tools for Teachers
Complications of Minimal Access Surgery at WLH
Pharma ospi slides which help in ospi learning
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPH.pptx obstetrics and gynecology in nursing
2.FourierTransform-ShortQuestionswithAnswers.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Renaissance Architecture: A Journey from Faith to Humanism
Lesson notes of climatology university.
Abdominal Access Techniques with Prof. Dr. R K Mishra
master seminar digital applications in india
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf

Constructors in C++

  • 1. BY N.RUBA ASST.PROF/DEPT. OF CA, BONSECOURS COLLEGE FOR WOMEN, THANJAVUR.
  • 2.  Constructor is a special member function of a class that initializes the object of the class.  Constructor name is same as class name and it doesn’t have a return type.
  • 3. #include <iostream> using namespace std; class number { public: int num; char ch; number() { num = 100; ch = 'A'; } };
  • 4. int main() { Number obj;// create the object of class, cout<<"num: "<<obj.num<<endl;// access data //members using object cout<<"ch: "<<obj.ch; return 0; } Output: num: 100 ch: A
  • 5.  Constructor is different from member function of the class. 1) Constructor doesn’t have a return type. Member function has a return type. 2) Constructor is automatically called when we create the object of the class.  Member function needs to be called explicitly using object of class. 3) When we do not create any constructor in our class, C++ compiler generates a default constructor and insert it into our code. The same does not apply to member functions.
  • 6. This is how a compiler generated default constructor looks: class XYZ { .... XYZ() { ---- } };
  • 7. There are two types of constructor in C++. 1) Default constructor 2) Parameterized constructor 1) Default Constructor A default constructor doesn’t have any arguments (or parameters) #include <iostream> using namespace std; class Website{ public: //Default constructor Website() { cout<<"Welcome to Bonsecours"<<endl; } };
  • 8. int main(void){ Website obj1;// creating two objects of class Website. Website obj2;// constructor should have been invoked twice. return 0; } Output: Welcome to Bonsecourss Welcome to Bonsecours When you don’t specify any constructor in the class, a default constructor with no code (empty body) would be inserted into your code by compiler.
  • 9.  Constructors with parameters are known as Parameterized constructors. These type of constructor allows us to pass arguments while object creation. Parameterized Constructor: XYZ(int a, int b) { } ... XYZ obj(10, 20);
  • 10. Example: #include <iostream> using namespace std; class Add { public: Add(int num1, int num2) //Parameterized constructor { cout<<(num1+num2)<<endl; } };
  • 11. int main(void) { Add obj1(10, 25);//method 1:for creating object(implicit) Add obj2 = Add(50, 40);// method 2:for creating object(explicit) return 0; } Output: 35 90