Constructors and Deconstructor

Slides:



Advertisements
Similar presentations
Introduction to C++ An object-oriented language Unit - 01.
Advertisements

Objects and Classes Part II
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such.
EC-241 Object-Oriented Programming
C++ Classes & Data Abstraction
1 Classes Object-oriented programming: Model the problem as a collection of objects that have certain attributes and interact with one another and/or the.
Your First C++ Program Aug 27, /27/08 CS 150 Introduction to Computer Science I C++  Based on the C programming language  One of today’s most.
1 CSC241: Object Oriented Programming Lecture No 07.
1 CSC241: Object Oriented Programming Lecture No 13.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
1 CSC241: Object Oriented Programming Lecture No 02.
Chapter 8 Operator Overloading.  Operator overloading is considered one of the more useful techniques for improving readability and ease of programming.
1 Becoming More Effective with C++ … Day Two Stanley B. Lippman
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
1 CSC241: Object Oriented Programming Lecture No 03.
1 CSC241: Object Oriented Programming Lecture No 08.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
1 C++ Classes and Data Structures Course link…..
Introduction to Object-oriented Programming
Constructors and Destructors
Andrew(amwallis) Classes!
Pointers and Dynamic Arrays
Review 1.
Visit for more Learning Resources
Classes and Objects.
Chapter 13: Overloading and Templates
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Review What is an object? What is a class?
Java Primer 1: Types, Classes and Operators
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
Classes Object-oriented programming: Example: Bank transactions
Review: Two Programming Paradigms
Chapter 1. Introduction to Computers and Programming
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Chapter 5 Classes.
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Pointers and Pointer-Based Strings
Pointers Revisited What is variable address, name, value?
Introduction to Functions
Repetition Statements
FUNCTIONS& FUNCTIONS OVERLOADING
Name: Rubaisha Rajpoot
Object-Oriented Programming
Constructors and Destructors
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
Object-Oriented Programming
Operator Overloading.
Computing Fundamentals
Control Structures Part 1
UNIT I OBJECT ORIENTED PROGRAMMING FUNDAMENTALS
COP 3330 Object-oriented Programming in C++
Object-Oriented Programming
Pointers and Pointer-Based Strings
Chapter 9 Inheritance.
Repetition Statements (Loops) - 2
Predefined Functions Revisited
Functions Imran Rashid CTO at ManiWeber Technologies.
CS 144 Advanced C++ Programming January 31 Class Meeting
STATIC DATA MEMBER & MEMBER FUNCTIONS
Lab – 2018/04/12 implement getTotalCount to return how many ‘nMatrix’s are allocated(exist) (modify constructor and destructor, use static variable and.
Modifying Objects Assignment operation Assignment conversions
Objects as Function Arguments
More Loops Topics Counter-Controlled (Definite) Repetition
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Creating and Using Classes
Introduction to Classes and Objects
Presentation transcript:

Constructors and Deconstructor The ENGLOBJ example shows two ways that member functions can be used to give values to the initialize itself function. Automatic initialization is carried out using a special member function called a constructor. A constructor is a member function that is executed automatically whenever an object is created. -purpose programming element. A counter is a variable that counts things. Maybe it counts file accesses, or the number of times the user presses the Enter key, or the number of customers entering a bank. Each time such an event takes place, the counter is incremented counter can also be accessed to find the current count. is important in the program and must be accessed by many different functions. In procedural languages such as C, a counter would probably be implemented as a design and may be modified accidentally. This example, COUNTER, provides a counter variable that can be modified only through its member functions. // object represents a counter variable #include <iostream> class Counter { private: unsigned int count; //count public: { /*empty body*/ } void inc_count() //increment count { count++; } int get_count() //return count { return count; } };

Automatic Initialization Constructors and Deconstructor //////////////////////////////////////////////////////////////// int main() { \ \ \ again \ cout << endl; } The Counter class has one data member: count, of type unsigned int (since the count is always positive). It has three member functions: look the current value of count. Automatic Initialization When an object of type Coun After it with always set count created a Counter object. //every time we do this, //we must do this too This is mistake prone, because the programmer may forget to initialize the object after

Same Name as the Class Initializer List creating objects of a given Constructors and Deconstructor creating objects of a given ated. In the Counter class, the constructor Counter() does this. This function is called automatically whenever a new object of type Counter is created. Thus in main() the statement creates two objects of type Counter. As each is created, its constructor, Counter (), is executed. to not only Same Name as the Class There are some unusual aspects of constructor functions. First, it is no accident that they have exactly the same name (Counter in this example) as the class of which they are members. This is one way the compiler knows they are constructors. Second, no return type is used for constructors. Why not? Since the constructor is called Initializer List one of the most common tasks a constructor carries out is initializing data members. In the this count() { coun should initialize a data member: { } The initialization takes place following the member function declarator but before the function