Constructors/Destructors Functions Revisited

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

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.
A C LOSER L OOK AT C LASSES 1. A SSIGNING O BJECTS One object can be assigned to another provided that both objects are of the same type. It is not sufficient.
C++ Classes & Data Abstraction
OOP Using Classes - I. Structures vs. Classes C-style structures –No “interface” If implementation changes, all programs using that struct must change.
Welcome to Constructors and Destructors Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
ECE122 Feb. 22, Any question on Vehicle sample code?
1 Inside the Vector Class: with additional needed methods CPS212CPS212 Gordon College.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 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.
CONSTRUCTORS AND DESTRUCTORS Chapter 5 By Mrs. Suman Verma PGT (Comp.Sc)
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA, Dc. Sc. & Engg.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
 A constructor is a special member function whose task is to initialize the objects of its class.  It is special because its name is same as the class.
Constructor in Inheritance. 2 Constructors are used to initialized object. In inheritance the base class contains default constructor then, the base class.
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.
CONSTRUCTOR AND DESTRUCTORS
CSC241 Object-Oriented Programming (OOP) Lecture No. 5.
OBJECT ORIENTED PROGRAMMING LECTURE 7 Instructor: Rashi Garg Coordinator: Gaurav Saxena.
1 CSC241: Object Oriented Programming Lecture No 03.
1 Review: C++ class 2 kinds of class members: data members and function members Class members are private by default Data members are generally private.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Destructors The destructor fulfills the opposite functionality. It is automatically called when an object.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
CONSTRUCTOR AND DESTRUCTOR. Topics to be discussed……………….. 1. Introduction Introduction 2. FeaturesFeatures 3. Types of ConstructorTypes of Constructor.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Learners Support Publications Constructors and Destructors.
Welcome to Constructors and Destructors Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.
Object Lifetimes and Dynamic Objects Lecture-7. Object Lifetimes and Dynamic Objects External (Global) Objects Persistent (in existence) throughout the.
INTRODUCTION TO CLASSES & OBJECTS CREATING CLASSES USING C#
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Constructors and Destructors
Pointer to an Object Can define a pointer to an object:
Learning Objectives Pointers as dada members
Class and Objects UNIT II.
Static data members Constructors and Destructors
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?
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.
CONSTRUCTORS & DESTRUCTORS
Constructors & Destructors.
Object Lifetime and Dynamic Objects
Constructor & Destructor
Class: Special Topics Copy Constructors Static members Friends this
group work #hifiTeam
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Lecture Constructors and Destructors
Prepared by Puttalakshmi H.R PGT(CS) KV Minambakkam Chennai-27.
Constructor & Destructor
More on Classes Classes may have constructors which are called when objects are created and destructors which are called when objects are destroyed. Classes.
Inheritance Using work from:
Chapter 9 Classes: A Deeper Look, Part 1
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.
Contents Introduction to Constructor Characteristics of Constructor
Object Oriented Programming (OOP) Lecture No. 9
Classes & Objects: Examples
CONSTRUCTORS AND DESRUCTORS
Constructors and destructors
Constructors and Destructors
Introduction of Programming
Lecture 16 Oct 30, 02.
Constructor, Destructor Mehroz Sadiq CS Faculty, UMT Department of CS&E, UET.
Class: Special Topics 2 For classes using memory allocation
Types of Computer Languages
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
CS148 Introduction to Programming II
Chapter 11 Classes.
Constructors & Destructors
Presentation transcript:

Constructors/Destructors Functions Revisited Powerpoint 7 Constructors/Destructors Functions Revisited

Constructors C++ Has no return value May have parameters Called automatically when a class object is declared C++ has default constructors that are called whenever a class is declared even if no function with the same name exists Should be public (or protected)

C++ Does not have a return type Cannot have parameters Called automatically when the class object is destroyed Deactivates storage allocated to a class Declared with a tilde (~)

Example of Constructor class sum { public: sum(); private: int sum1,sum2; };

C++ void main () { sum obj1; //constructor is called at this //time cout<<“end of main”<<endl; return; }

C++ Functions sum::sum () { sum1=0; sum2=10; cout<<sum1<<“ “<<sum2<<endl; }

C++ Sample Functions What is the output??

the answer 0 10 end of main

Example of Constructor withArguments class sum { public: sum(int,int); private: int sum1,sum2; };

C++ void main () { sum obj1 (10,20); //constructor is // called at this time cout<<“end of main”<<endl; return; }

C++ Functions sum::sum (int x,int y)) { sum1=x; sum2=y; cout<<sum1<<“ sum1”; cout<<sum2<<“ sum2”<<endl; }

C++ Sample Functions What is the output?? 10 sum1 20 sum2 end main

Destructor: Also a function Identified by a ~ preceding function name Called automatically when a class object is destroyed May not have arguments Should be public (or protected)

public: ~sum (); sum::~sum ( ) { close (infile); close (outfile); }