Unit 2. Constructors It initializes an object when it is created. It has same as its class and syntactically similar to a method. Constructor have no.

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
Constructors: Access Considerations DerivedClass::DerivedClass( int iR, float fVar) : BaseClass(fVar) { m_uiRating = uiR; } Alternatively DerivedClass::DerivedClass(
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
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.
Methods. int month; int year class Month Defining Classes A class contains data declarations (static and instance variables) and method declarations (behaviors)
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
C# vs. C++ What's Different & What's New. An example C# public sometype myfn { get; set; } C++ public: sometype myfn { sometype get (); void set (sometype.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Internet Software Development Classes and Inheritance Paul J Krause.
C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
ILM Proprietary and Confidential -
CIS 3301 C# Lesson 7 Introduction to Classes. CIS 3302 Objectives Implement Constructors. Know the difference between instance and static members. Understand.
More about Class 靜宜大學資工系 蔡奇偉副教授 ©2011. 大綱 Instance Class Members Class members can be associated with an instance of the class or with the class as a.
ECE122 Feb. 22, Any question on Vehicle sample code?
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
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.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
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.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
 Constructor  Finalize() method  this keyword  Method Overloading  Constructor Overloading  Object As an Argument  Returning Objects.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Lecture 06 Java and OOP Jaeki Song. Outlines Java and OOP –Class and Object – Inheritance – Polymorphism.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
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.
Introduction to Object-Oriented Programming Lesson 2.
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
From C++ to C# Part 5. Enums Similar to C++ Similar to C++ Read up section 1.10 of Spec. Read up section 1.10 of Spec.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني 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.
Classes and Objects Introduction Life Cycle Creation Example.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Modern Programming Tools And Techniques-I
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.
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 & Polymorphism
Chapter 5 Classes.
Constructor & Destructor
Classes & Objects: Examples
Learning Objectives Classes Constructors Principles of OOP
Unit-2 Objects and Classes
Inheritance CT1513.
CIS 199 Final Review.
Computer Science II for Majors
Presentation transcript:

Unit 2

Constructors It initializes an object when it is created. It has same as its class and syntactically similar to a method. Constructor have no explicit return type. Syntax: Access class _name() { }

Simple constructor class rectangle { Public int length; Public int width; Public rectangle(int x,int y) // defining Constuctor { length = x; Width=y; } Public int RectArea() { Return(length*width) } Class RectangleArea { Public static void main() { rectangle rect1 =new Rectangle(15,10); // calling constructor int area1 =rect1.RectArea(); Console.WriteLine(“Area1 = “+area1); }

Overloaded constructor To create overloaded constructor method provide several different constructor definition with different parameter class Room { Public double length; Public double breadth; Public Room(double x,double y) // defining Constuctor 1 { length = x; breadth=y; } Public Room(double x) // defining Constuctor 2 { length=breadth=x; } { return(length*width) }

Types Static constructors Private constructors Copy constructors

Static constructor A static constructor is called before any object of the class is created. It is usually to assign initial values to static data members. Ex: Class Abc { Static Abc() // no parameter { ……………….// set values for static member here } Here no access modifier.

Copy constructor It creates an object by copying variables from another object. Ex: Public item (Item item) { Code=item.code; Price=item.price; } Copy constructor is invoked by instantiating an object of type Item and passing it the object to be copied. Ex: Item item2=new Item (item 1); Item2 is copy of item1

Private constructor C# does not have a global variables or constants. This constructor must be contained in a class only.

Destructors A destructor is opposite to a constructor. It is a method called when an object is no more required. Name as same in class name. Ex: Class fun { …….. ~Fun()// No aruguments { ……. }

Finalizers Finalizers are used to clean up any resources used by a class object when the object is destroyed.

garbage collector the finalizer will be called when the garbage collector decides that the object instance is no longer needed

INHERITANCE New class is derived from the existing class. Types: 1.Simple inheritance 2.Hierarchical inheritance 3.Multiple inheritance 4.Mutilevel inheritance

Polymorphism It is ability to take more than one form. Types: 1.Inclusion Polymorphism 2.operation Polymorphism

1.Inclusion Polymorphism

2.operation Polymorphism

indexers Indexers are location indicators and are used to access class objects. The indexer takes an index argument and looks like an array. The indexer is declared using the name this Ex: Public double this[int idx] { get { //return desired data } Set { //set desired data }

Delegates One person acting for another person. Method acting for another method. Steps: Delegate declaration Delegate methods definition Delegate instantiation Delagate invocation

Delegate declaration defines a class using the class system. delegate as a base class. Delegate methods are any functions whose signature matches the delegate signature exactly. Syntax: Modifier delegate return-type delegate name(parameter) Modifiers are New Public Protected Internal private

Examples Delegate void Simple Delegate(); Delegate int MathOperation(int x,int y); Public Delegate int CompareItems(obj o1,obj o2);