SlideShare a Scribd company logo
1 of 25
INDIAN INSTITUTE OF INFORMATION
TECHNOLOGY KOTA
#include <iostream>
using namespace std;
class GFG {
public:
void guest(bool op)
{
if (op == true) {
cout << "Yes, I want
fruit!“<<endl;
}
else {
cout << "No, Thanks!“<<endl;
}
}
};
string offering (bool a)
{
if (a)
{
return "Apple.";
}
else {
return "Chocolate.";
}
}
Example 1
int main()
{
bool n = true;
cout << "Will you eat fruit? ";
GFG obj;
obj.guest(n);
if (n == true) {
cout << "Give an " + offering(n);
}
else {
cout << "Give a " + offering(n);
}
return 0;
}
Example 1
Output
Will you eat fruit? Yes, I want fruit!
Give an Apple.
• Constructor is used to create the object in object oriented programming
language.
• The constructor is a function whose name is same as the object with no
return type.
• Whenever an object is created, the special member function, that is,
constructor will be executed automatically.
Constructor
• Constructors are used to allocate the memory for the newly created object
and they can be overloaded so that different form of initialization can be
accommodated.
• If a class has constructor, each object of that class will be initialized.
• It is called constructor because it constructs the value of data members of
the class.
Constructor
Constructor
Constructor
Constructor
• Here in the above program, one default constructor student has been
created which is similar to the class name student.
• When objects of the student class are created, then default constructor is
automatically executed, and three times it displays the output “Object is
initialized”.
• Since here, three objects, x, y and z have been defined for every execution
student constructor.
Constructor
• A constructor for a class is needed so that the compiler automatically
initializes an object as soon as it is created.
• A class constructor if defined is called whenever a program creates an
object of that class.
• The constructor functions have some special characteristics which are as
follows:
Characteristics of Constructors
• The constructor functions have some special characteristics which are as
follows:
They should be declared in the public section.
They are invoked directly when an object is created.
They don’t have return type, not even void and hence can’t return any
values.
They can’t be inherited; through a derived class, can call the base class
constructor.
Like other C++ functions, they can have default arguments.
Constructors can’t be virtual.
Constructor can be inside the class definition or outside the class
definition.
Characteristics of Constructors
• The constructor functions have some special characteristics which are as
follows:
Constructor can’t be friend function.
They can’t be used in union.
They make implicit calls to the operators new and delete when memory
allocation is required.
Characteristics of Constructors
• C++ constructors have the following limitations:
No return type
Naming
Compile time bound
There is no virtual constructor
Limitations of Constructor:
• A member function with the same name as its class is called constructor
and it is used to initialize the objects of that class type with an initial value.
• Objects generally need to initialize variables or assign dynamic memory
during their process of creation to become operative and to avoid returning
unexpected values during their execution.
• In order to avoid that, a class can include a special function called
constructor, which is automatically called whenever a new object of this
class is created.
Declaration of Constructors
Example 1
Example 2
• A constructor in a class is used to initialize the value of the object.
• It prepares the new object for use by initializing its legal value.
• Constructors are classified into three main types.
Default Constructor
Parameterized Constructor
Copy Constructor
Types of Constructors
• A default constructor is a constructor that either has no parameters, or if
it has parameters, all the parameters have default values.
• Having a default constructor simply means that an application can declare
instances of the class.
• The compiler first implicitly defines the implicitly declared constructors of
the base classes and non-static data members of a class A before defining
the implicitly declared constructor of A.
Default Constructor
Constructor
• A constructor of a class A is trivial if all the following statements are true:
It is implicitly defined
A has no virtual functions and no virtual base classes
All the direct base classes of A have trivial constructors
The classes of all the non-static data members of A have trivial
constructors
The default constructor provided by the compiler does not do anything
specific.
It simply allocates memory to data members of the object.
Default Constructor
• A constructor in C++ which can accept parameters for its invocation.
• Such constructor that can take the arguments are called parameterized
constructor.
Parameterized Constructor
Constructor
Constructor
25
THANK YOU

More Related Content

Similar to CST 203 Lecture 7.pptx

Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructorSaharsh Anand
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of ConstructorsDhrumil Panchal
 
C++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptxC++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptxsasukeman
 
ConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTIONConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTIONShweta Shah
 
Introductions to Constructors.pptx
Introductions to Constructors.pptxIntroductions to Constructors.pptx
Introductions to Constructors.pptxSouravKrishnaBaul
 
Constructors in C++.pptx
Constructors in C++.pptxConstructors in C++.pptx
Constructors in C++.pptxRassjb
 
Constructors in Java (2).pdf
Constructors in Java (2).pdfConstructors in Java (2).pdf
Constructors in Java (2).pdfkumari36
 
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its Types[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its TypesMuhammad Hammad Waseem
 
Example for Virtual and Pure Virtual function.pdf
Example for Virtual and Pure Virtual function.pdfExample for Virtual and Pure Virtual function.pdf
Example for Virtual and Pure Virtual function.pdfrajaratna4
 
chapter-9-constructors.pdf
chapter-9-constructors.pdfchapter-9-constructors.pdf
chapter-9-constructors.pdfstudy material
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCEVenugopalavarma Raja
 
Constructors.16
Constructors.16Constructors.16
Constructors.16myrajendra
 

Similar to CST 203 Lecture 7.pptx (20)

Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructor
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
 
C++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptxC++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptx
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Oops
OopsOops
Oops
 
ConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTIONConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTION
 
Introductions to Constructors.pptx
Introductions to Constructors.pptxIntroductions to Constructors.pptx
Introductions to Constructors.pptx
 
Constructors in C++.pptx
Constructors in C++.pptxConstructors in C++.pptx
Constructors in C++.pptx
 
Constructors in Java (2).pdf
Constructors in Java (2).pdfConstructors in Java (2).pdf
Constructors in Java (2).pdf
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its Types[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
 
Example for Virtual and Pure Virtual function.pdf
Example for Virtual and Pure Virtual function.pdfExample for Virtual and Pure Virtual function.pdf
Example for Virtual and Pure Virtual function.pdf
 
chapter-9-constructors.pdf
chapter-9-constructors.pdfchapter-9-constructors.pdf
chapter-9-constructors.pdf
 
OOP's Part 1
OOP's Part 1OOP's Part 1
OOP's Part 1
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
 
5 Constructors and Destructors
5 Constructors and Destructors5 Constructors and Destructors
5 Constructors and Destructors
 
Constructors.16
Constructors.16Constructors.16
Constructors.16
 
Constructor and destructor in C++
Constructor and destructor in C++Constructor and destructor in C++
Constructor and destructor in C++
 
Constructors and destructors in C++ part 2
Constructors and destructors in C++ part 2Constructors and destructors in C++ part 2
Constructors and destructors in C++ part 2
 

Recently uploaded

MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 

Recently uploaded (20)

MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 

CST 203 Lecture 7.pptx

  • 1. INDIAN INSTITUTE OF INFORMATION TECHNOLOGY KOTA
  • 2. #include <iostream> using namespace std; class GFG { public: void guest(bool op) { if (op == true) { cout << "Yes, I want fruit!“<<endl; } else { cout << "No, Thanks!“<<endl; } } }; string offering (bool a) { if (a) { return "Apple."; } else { return "Chocolate."; } } Example 1
  • 3. int main() { bool n = true; cout << "Will you eat fruit? "; GFG obj; obj.guest(n); if (n == true) { cout << "Give an " + offering(n); } else { cout << "Give a " + offering(n); } return 0; } Example 1
  • 4. Output Will you eat fruit? Yes, I want fruit! Give an Apple.
  • 5. • Constructor is used to create the object in object oriented programming language. • The constructor is a function whose name is same as the object with no return type. • Whenever an object is created, the special member function, that is, constructor will be executed automatically. Constructor
  • 6. • Constructors are used to allocate the memory for the newly created object and they can be overloaded so that different form of initialization can be accommodated. • If a class has constructor, each object of that class will be initialized. • It is called constructor because it constructs the value of data members of the class. Constructor
  • 10. • Here in the above program, one default constructor student has been created which is similar to the class name student. • When objects of the student class are created, then default constructor is automatically executed, and three times it displays the output “Object is initialized”. • Since here, three objects, x, y and z have been defined for every execution student constructor. Constructor
  • 11. • A constructor for a class is needed so that the compiler automatically initializes an object as soon as it is created. • A class constructor if defined is called whenever a program creates an object of that class. • The constructor functions have some special characteristics which are as follows: Characteristics of Constructors
  • 12. • The constructor functions have some special characteristics which are as follows: They should be declared in the public section. They are invoked directly when an object is created. They don’t have return type, not even void and hence can’t return any values. They can’t be inherited; through a derived class, can call the base class constructor. Like other C++ functions, they can have default arguments. Constructors can’t be virtual. Constructor can be inside the class definition or outside the class definition. Characteristics of Constructors
  • 13. • The constructor functions have some special characteristics which are as follows: Constructor can’t be friend function. They can’t be used in union. They make implicit calls to the operators new and delete when memory allocation is required. Characteristics of Constructors
  • 14. • C++ constructors have the following limitations: No return type Naming Compile time bound There is no virtual constructor Limitations of Constructor:
  • 15. • A member function with the same name as its class is called constructor and it is used to initialize the objects of that class type with an initial value. • Objects generally need to initialize variables or assign dynamic memory during their process of creation to become operative and to avoid returning unexpected values during their execution. • In order to avoid that, a class can include a special function called constructor, which is automatically called whenever a new object of this class is created. Declaration of Constructors
  • 18. • A constructor in a class is used to initialize the value of the object. • It prepares the new object for use by initializing its legal value. • Constructors are classified into three main types. Default Constructor Parameterized Constructor Copy Constructor Types of Constructors
  • 19. • A default constructor is a constructor that either has no parameters, or if it has parameters, all the parameters have default values. • Having a default constructor simply means that an application can declare instances of the class. • The compiler first implicitly defines the implicitly declared constructors of the base classes and non-static data members of a class A before defining the implicitly declared constructor of A. Default Constructor
  • 21. • A constructor of a class A is trivial if all the following statements are true: It is implicitly defined A has no virtual functions and no virtual base classes All the direct base classes of A have trivial constructors The classes of all the non-static data members of A have trivial constructors The default constructor provided by the compiler does not do anything specific. It simply allocates memory to data members of the object. Default Constructor
  • 22. • A constructor in C++ which can accept parameters for its invocation. • Such constructor that can take the arguments are called parameterized constructor. Parameterized Constructor