The Lifecycle of an Object

Slides:



Advertisements
Similar presentations
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.
Advertisements

F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Methods. int month; int year class Month Defining Classes A class contains data declarations (static and instance variables) and method declarations (behaviors)
Road Map Introduction to object oriented programming. Classes
©2004 Brooks/Cole Chapter 6 Methods. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Using Methods We've already seen examples of using methods.
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Review of C++ Programming Part II Sheng-Fang Huang.
Dale Roberts Object Oriented Programming using Java - Class Constructors Dale Roberts, Lecturer Computer Science, IUPUI Department.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
C# D1 CSC 298 Elements of C# code (part 2). C# D2 Writing a class (or a struct)  Similarly to Java or C++  Fields: to hold the class data  Methods:
ECE122 Feb. 22, Any question on Vehicle sample code?
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
 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.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Objects and Classes Mostafa Abdallah
Programming Fundamentals1 Chapter 8 OBJECT MANIPULATION - INHERITANCE.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
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?
Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,
Classes Methods and Properties. Introduction to Classes and Objects In object-oriented programming terminology, a class is defined as a kind of programmer-defined.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Topics Instance variables, set and get methods Encapsulation
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Intro to C# for Java people
Operator Overloading.
Functions + Overloading + Scope
Topic: Classes and Objects
Lecture 3: Introduction to Object and Classes
C# for C++ Programmers 1.
Learning Objectives Pointers as dada members
Creating Your Own Classes
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?
3 Introduction to Classes and Objects.
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.
Introduction to C++ Systems Programming.
Yanal Alahmad Java Workshop Yanal Alahmad
A First C++ Class – a Circle
CS410 – Software Engineering Lecture #11: C++ Basics IV
Chapter 3: Using Methods, Classes, and Objects
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?
This technique is Called “Divide and Conquer”.
Computing with C# and the .NET Framework
Chapter 4 Procedural Methods.
More on Classes Classes may have constructors which are called when objects are created and destructors which are called when objects are destroyed. Classes.
Pass by Reference, const, readonly, struct
Chapter 3 Introduction to Classes, Objects Methods and Strings
Learning Objectives Classes Constructors Principles of OOP
Objects with Functions and Arrays
Chapter 7 Procedural Methods.
Method of Classes Chapter 7, page 155 Lecture /4/6.
CIS 199 Final Review.
Class.
Java Programming Language
Recitation Course 0603 Speaker: Liu Yu-Jiun.
A Deeper Look at Classes
OO Programming Concepts
Classes and Objects Object Creation
Chapter 7 Objects and Classes
Chengyu Sun California State University, Los Angeles
Presentation transcript:

The Lifecycle of an Object

The lifecycle includes two important stages: Construction and Destruction. When an object is first instantiated it needs to be initialized. This initialization is known as construction and is carried out by a constructor function.

Basic initialization of an object is automatic. Table . Primitive types and their default values Default value Type numeric (int, long, etc.) false bool '\0' (null) char enum null reference

EX: Time object1=new Time (); All objects have a default constructor, which is a parameterless method with the same name as the class itself. EX: Time object1=new Time (); In addition, a class definition might include several constructor methods with parameters, known as nondefault constructors, that uses a parameter at instantiation: Time object1=new Time(CurrentTime);

Construction EX1: using System; class xxx { public int x; // int x; or private int x; public int y; public xxx(int p1, int p2) x = p1; y = p2; } static void Main() xxx mC = new xxx(1, 2); Console.WriteLine("x = {0}, y = {1}", mC.x, mC.y);

EX2: using System; public class Test { class xxx public int x; // int x public int y; public xxx(int p1, int p2) x = p1; y = p2; } static void Main() xxx mC = new xxx(11, 22); Console.WriteLine("x = {0}, y = {1}", mC.x, mC.y);

EX3 using System; class Circle { const double PI = 3.141592; public Circle() // default constructor radius = 5; } public double Area() // return 3.141592 * radius * radius; return PI * radius * radius; private double radius; static void Main() Circle c; c = new Circle(); double areaOfCircle = c.Area(); Console.WriteLine(areaOfCircle);

Methods The syntax of a method is as follows: access-modifiers returnType methodName ( parameterList )  {      // method body statements   }

EX.1: public void DisplayCurrentTime( ) { System.Console.WriteLine( "{0}/{1}/{2} {3}:{4}:{5}", Month, Date, Year, Hour, Minute, Second ); } EX.2: double Area()      {         return 3.141592 * radius * radius;      }

The syntax of a method call is as follows: objectname.methodName ( argumentList ) EX.1: Time t = new Time( ); t.DisplayCurrentTime( );

Calling the method Using amethode name by itself to call method of the same class. Using avariable that contains arefrence to an object followed by (.) & method name to call non-static method. Using the class name followed by (.) & method name to call static method.

Passing Parameters Parameter Types C# allows four types of parameters in a parameter list: • Input parameters • Output parameters • Reference parameters • Parameter arrays

Input parameters are passed by value into methods Input parameters are passed by value into methods. It is not allowed to change the value supplied by the caller. Output parameters are parameters whose values are not set when the method is called. The out keyword must precede the parameter's type in the parameter list. Reference parameters are passed by reference into methods. The ref keyword must precede the parameter's type in the parameter list. Parameter arrays enable you to specify that your method accept a variable number of arguments. using the C# keyword params,

ref class RefExample { void Method(ref int i) i = 44; } static void Main() RefExample ob=new RefExample(); int val = 0; ob.Method(ref val); Console.WriteLine(val); // val is now 44

out class OutExample { void Method(out int i) i = 44; } static void Main() OutExample ob=new OutExample(); int value; ob.Method(out value); // value is now 44

class CS0663_Example { // compiler error CS0663: "cannot define overloaded // methods that differ only on ref and out" public void SampleMethod(ref int i) { } public void SampleMethod(out int i) { } }

Example: method to return multiple values class OutReturnExample { void Method(out int i, out string s1, out string s2) i = 44; s1 = "I've been returned"; s2 = null; } static void Main() { OutReturnExample ob= new OutReturnExample(); int value; string str1, str2; ob.Method(out value, out str1, out str2); // value is now 44 // str1 is now "I've been returned" // str2 is (still) null;