Constructor, Destructor Mehroz Sadiq CS Faculty, UMT Department of CS&E, UET.

Slides:



Advertisements
Similar presentations
Objects and Classes Part II
Advertisements

Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
1 CSC241: Object Oriented Programming Lecture No 02.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
Class and Structure. 2 Structure Declare using the keyword struct purpose is to group data Default visibility mode is public A struct doesn't have a constructor.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Polymorphism CMPS Poly-morphism Means “many-forms” Means different things in biology, chemistry, computer science Means different things to functional.
1 CSC241: Object Oriented Programming Lecture No 05.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
Classes, Interfaces and Packages
1 CSC241: Object Oriented Programming Lecture No 03.
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Destructors The destructor fulfills the opposite functionality. It is automatically called when an object.
CIS162AD Constructors Part 2 08_constructors.ppt.
1 Introduction to Object Oriented Programming Chapter 10.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
CONSTRUCTOR AND DESTRUCTOR. Topics to be discussed……………….. 1. Introduction Introduction 2. FeaturesFeatures 3. Types of ConstructorTypes of Constructor.
Learners Support Publications Constructors and Destructors.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
Memory Management.
Constructors and Destructors
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Learning Objectives Pointers as dada members
Overloaded Constructors and Multiple Classes
Static data members Constructors and Destructors
By Muhammad Waris Zargar
CSC241: Object Oriented Programming
Default Constructors A default constructor is a constructor that takes no arguments. If you write a class with no constructor at all, C++ will write a.
Inheritance and Polymorphism
Constructor & Destructor
Class: Special Topics Copy Constructors Static members Friends this
Templates.
group work #hifiTeam
Lecture Constructors and Destructors
Object Oriented Analysis and Design
Object Oriented Analysis and Design
OOP’S Concepts in C#.Net
Constructor & Destructor
Inheritance Basics Programming with Inheritance
CS212: Object Oriented Analysis and Design
Contents Introduction to Constructor Characteristics of Constructor
Constructors and destructors
Constructors and Destructors
Computer Programming with JAVA
Object Oriented Programming Using C++
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
C++ Programming ㅎㅎ String OOP Class Constructor & Destructor.
Chapter 10: Method Overriding and method Overloading
Method Overriding and method Overloading
Eighth step for Learning C++ Programming
A Deeper Look at Classes
C++ Programming CLASS This pointer Static Class Friend Class
CS1201: Programming Language 2
Class: Special Topics 2 For classes using memory allocation
Types of Computer Languages
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Objects as Function Arguments
Constructors & Destructors
Presentation transcript:

Constructor, Destructor Mehroz Sadiq CS Faculty, UMT Department of CS&E, UET.

Pathway Constructor Constructor Examples Overloading Constructor (with arguments) Function Overloading vs. Function overriding Copy Constructor Destructor Constructor vs. Destructor

Constructor Constructor is special function that is called automatically when you create an object of the class. Constructor has the same name as class name It does not have any return type. There is no way that we call constructor manually Constructor are normally use to initialize the properties (attributes) of the class

Count Example Return 0; }

Another Example Class Human { Private: string name; int age; public: Human() { name= “null”; age=0; } void getdata() { cout<<“enter name”<<endl; getline(cin, name); cout<<“enter age”<<endl; cin>>age; } void showdata() { cout<<“name is”<<name<<“age is ”<<age<<endl; } };

Int main() { Human h1, h2; h1.getdata(); h1.showdata(); h2.getdata(); h2.showdata(); return 0; }

Overloading Class Constructor It’s convenient to be able to give variables of type Distance a value when they are first created. That is, we would like to use definitions like Distance width(5, 6.25); which defines an object, width, and simultaneously initializes it to a value of 5 for feet and 6.25 for inches. To do this we write a constructor like this: Distance(int ft, float in) : feet(ft), inches(in) { }

Example Class Distance { private: int feet; float inches; public: Distance() { feet=0; inches= 0;} Distance(int ft, float in) { feet=ft; inches=in; } void getdata() { cout<<“enter feet”<<endl; cin>>feet; cout<<“enter inches”<<endl; cin>>inches; } void showdata() { cout<<“the distance is: ”<<feet, inches<<endl; } };

Int main() { Distance d1; Distance d2(11, 6.5); d1.getdata(); d1.showdata(); d2.showdata(); }

Example:

Function Overloading Function overloading is a feature that allows us to have same function more than once in a program. Overloaded functions have same name but their signature must be different. Here we have the same function sum declared four times with different signatures. Based on the parameters we pass, while calling function sum, decides which method is to be called. This happens during compilation, which is why it is also known as compile time polymorphism. – float sum(int, int); – float sum(float, float); – float sum(int, float); – float sum(float, int);

Function Overriding Function overriding is a feature of OOPs Programming that allows us to override a function of parent class in child class. Function overridingOOPs Programming Function Overriding is happens in the child class when child class overrides parent class function. In function overriding the signature of both the functions (overriding function and overridden function) should be same. overriding happens at run time which is why it is known as run time polymorphism.

#include using namespace std; //Parent class or super class or base class class A { public: void disp() { cout<<"Parent Class disp() function"<<endl; } }; Class B : public A { public: void disp() { // definition can be different } };

Copy Constructor Two types of constructors have already discussed: 1.Constructor with no arguments and data members are initialized with constant values. 2.Constructor with arguments where data members are initializes to values passed as arguments. There’s another type of constructor which is called Copy Constructor.  Special type of constructor, where the values of objects are initialized with another object of the same type. (data members of one objects are initialized with the data members of same type of another objects). This constructor is called copy constructor. Copy Constructor is one argument constructor, where the argument is another object of same type whose values are going to be copied

Destructor We’ve seen that a special member function—the constructor—is called automatically when an object is first created. You might guess that another function is called automatically when an object is destroyed. This is indeed the case. Such a function is called a destructor. A destructor has the same name as the constructor (which is the same as the class name) but is preceded by a tilde: class Foo { private: int data; public: Foo() : data(0) //constructor (same name as class) { } ~Foo() //destructor (same name with tilde) { } };

Destructor It is the same name as constructor but with tild sign (~) Destructor has no arguments Destructor has no return value The major purpose of destructor to de-allocate the memory which was allocated for the objects by the constructor