0% found this document useful (0 votes)
10 views19 pages

Chapter 5 Constructor

Chapter 5 of the Object Oriented Programming Language course covers constructors and destructors in C++. It explains the types of constructors, including default and parameterized constructors, as well as the concept of constructor overloading. Additionally, it describes destructors, their syntax, and their role in memory management.

Uploaded by

shabir.ahmad1317
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)
10 views19 pages

Chapter 5 Constructor

Chapter 5 of the Object Oriented Programming Language course covers constructors and destructors in C++. It explains the types of constructors, including default and parameterized constructors, as well as the concept of constructor overloading. Additionally, it describes destructors, their syntax, and their role in memory management.

Uploaded by

shabir.ahmad1317
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/ 19

Faculty of Computer Science

Object Oriented Programming Language

Lecturer: Lutfullah Haqnesar


OOP Chapter 5

Chapter 5

Constructor and Destructor


OOP Chapter 5

Learning outcomes:
 Constructor
 Types of Constructor
 Default Constructor
 Parameterized Constructor
 Constructor overloading
 Destructor
Constructors Chapter 5
What is Security?
What is constructor?
 Constructor is a member function of class, whose name is same as the
class name.
 A constructor is a special type of function of a class which initializes
objects of a class.
 In C++, Constructor is automatically called when object of class is
created.
 Constructor does not have a return value, hence they do not have a return
type.
Constructors Chapter 5
What is Security?
Syntax for creating constructor:
1. Syntax for defining the constructor within the class:
<class-name> (list-of-parameters)
{
// constructor definition
}
2. Syntax for defining the constructor outside the class:
<class-name>: :<class-name> (list-of-parameters)
{
// constructor definition
}
Default Constructor Example Chapter 5
What is Security?
#include <iostream>
using namespace std;
class Emp {
public:
Emp() {
cout<<"Constructor”;
}
};
int main() {
Emp e1; //creating an object of Emp
}
Difference between constructor and normal functions
What is Security?
Constructor Function

 Constructor is automatically called  Function is not automatically called


when we create the object class. after creating object of the class.
 Constructor has the same name as  Function should have a different
class name. name than class name.
 Constructor has no return type not
 Function requires a valid return type.
even void.
Types of Constructors Chapter 5
What is Security?
There can be two types of constructors in C++.

1. Default constructor

2. Parameterized constructor
Types of Constructors Chapter 5
What is Security?
1. Default Constructor
• A constructor which has no argument is known as default constructor.
• Default constructor is the constructor which doesn’t take any argument.
• It is invoked at the time of creating object.
Default Constructor Example Chapter 5
What is Security?
#include <iostream>
using namespace std;
class Employee {
public:
Employee() {
cout<<"Default Constructor Invoked"<<endl;
}
};
int main() {
Employee e1; //creating an object of Employee
}
Types of Constructors Chapter 5
What is Security?
2. Parameterized Constructor
• A constructor which has parameters is called parameterized constructor.
• To create a parameterized constructor, simply add parameters to it, the
way you would do it to any other function.
• When you define the constructor’s body, use the parameters to initialize
the object.
• It is used to provide different values to distinct objects.
Parameterized Constructor Example Chapter 5
What is Security?
#include <iostream> void display()
using namespace std; {
class Employee { cout<<id<<" "<<name<<endl;
public: }
int id; };
string name; int main()
Employee(int i, string n) {
{ Employee e1 =Employee(101, "Sonoo");
id = i; e1.display();
name = n; }
}
Constructors Example 3 Chapter 5
What is Security?
#include<iostream> cout<<"Enter the Name:";
using namespace std; cin>>name;
class student{ }
int rno; void student :: display()
char name[50]; {
double fee; cout << rno << "\t“ << name;
public: }
student(); int main()
void display(); {
}; student s;
student::student() { s.display();
cout<<"Enter the RollNo:"; }
cin>>rno;
Constructor Overloading Chapter 5
What is Security?
• We can have more than one constructor in a class with same name, but
different list of arguments. This concept is known as Constructor
Overloading.
• Overloaded constructors have the same name (name of the class) but the
different number of arguments.
• Depending upon the number and type of arguments passed, the
corresponding constructor is called.
Default Constructor Example Chapter 5
What is Security?
#include <iostream> construct (int a, int b) {
using namespace std; area = a * b;
class construct { cout<< area<< endl;
public: }
float area; };
construct() { int main() {
area = 0; construct ob;
cout << area<< endl; construct ob2( 10, 20);
} }
Destructor Chapter 5
What is Security?
• Destructor is also a special member function and destructor destroys the
class objects created by constructor, and release memory space occupied
by the objects.
• Destructor has the same name as their class name preceded by a tiled (~)
symbol.
• It is not possible to define more than one destructor hence it can-not be
overloaded.
• Destructor neither requires any argument nor returns any value.
Destructor Chapter 5
What is Security?
Syntax for defining the destructor within the class:
~ <class-name>()
{

Syntax for defining the destructor outside the class:


<class-name>: : ~ <class-name>()
{

}
Destructor Chapter 5
What is Security?
#include <iostream> ~Employee()
using namespace std; {
class Employee cout<<"Destructor called"<<endl;
{ }
public: };
Employee() int main(void)
{ {
cout<<"Constructor called"; Employee e1;
} }
What is Security?

End of Chapter

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy