Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE

Similar documents
CS313D: ADVANCED PROGRAMMING LANGUAGE

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

CS313D: ADVANCED PROGRAMMING LANGUAGE

Constructors for classes

CS111: PROGRAMMING LANGUAGE II

Lecture 18 Tao Wang 1

Advanced Programming Using Visual Basic 2008

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS313D: ADVANCED PROGRAMMING LANGUAGE

Object Oriented Programming in C#

Chapter 10 Introduction to Classes

And Even More and More C++ Fundamentals of Computer Science

Chapter 12 Object-Oriented Programming. Starting Out with Games & Graphics in C++ Tony Gaddis

Computer Programming with C++ (21)


Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.

Introduction to Programming session 24

Chapter 12. OOP: Creating Object- Oriented Programs. McGraw-Hill. Copyright 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved.

CS313D: ADVANCED PROGRAMMING LANGUAGE

Inheritance and Polymorphism

Chapter 4 Defining Classes I

CIS Intro to Programming in C#

Object Oriented Design

CMPT 117: Tutorial 1. Craig Thompson. 12 January 2009

CS111: PROGRAMMING LANGUAGE II

Computer Science II. OO Programming Classes Scott C Johnson Rochester Institute of Technology

Unit3: Java in the large. Prepared by: Dr. Abdallah Mohamed, AOU-KW

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 12: Classes and Data Abstraction

C++ (Non for C Programmer) (BT307) 40 Hours

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Microsoft Visual Basic 2005: Reloaded

Object Oriented Programming

by Pearson Education, Inc. All Rights Reserved. 2

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES IV

Classes: A Deeper Look

Introduction to Classes

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Object-Oriented Programming

CS313D: ADVANCED PROGRAMMING LANGUAGE

A A B U n i v e r s i t y

7. C++ Class and Object

15CS45 : OBJECT ORIENTED CONCEPTS

2/3/2018 CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II. Lecture Contents. C# basics. Methods Arrays. Dr. Amal Khalifa, Spr17

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

CS111: PROGRAMMING LANGUAGE II

Object oriented programming. Encapsulation. Polymorphism. Inheritance OOP

EEE-425 Programming Languages (2013) 1

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

Programming I. Course 9 Introduction to programming

EEE-425 Programming Languages (2013) 1

Software Development With Java CSCI

Lecture 5. Function Pointers

Fundamental Concepts and Definitions

CLASSES AND OBJECTS IN JAVA

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #1 Examination 12:30 noon, Tuesday, February 14, 2012

Chapter 12. OOP: Creating Object-Oriented Programs The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill

Introduction to Programming Using Java (98-388)

JAVA GUI PROGRAMMING REVISION TOUR III

C++ Inheritance and Encapsulation

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

Classes and Objects: A Deeper Look

CS360 Lecture 5 Object-Oriented Concepts

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

Objects and Classes. Amirishetty Anjan Kumar. November 27, Computer Science and Engineering Indian Institue of Technology Bombay

Data Structures using OOP C++ Lecture 3

Introduction To C#.NET

Learning Objectives. C++ For Artists 2003 Rick Miller All Rights Reserved xli

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

CS304 Object Oriented Programming Final Term

BBM 102 Introduction to Programming II Spring Inheritance

CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II

Java Primer 1: Types, Classes and Operators

Lecture #1. Introduction to Classes and Objects

Chapter 6 Introduction to Defining Classes

MaanavaN.Com CS1203 OBJECT ORIENTED PROGRAMMING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

CMSC 132: Object-Oriented Programming II

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

Topics. Topics (Continued) 7.1 Abstract Data Types. Abstraction and Data Types. 7.2 Object-Oriented Programming

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

C++ Inheritance and Encapsulation

Ch02. True/False Indicate whether the statement is true or false.

Abstraction in Software Development

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

Cpt S 122 Data Structures. Introduction to C++ Part II

Object Oriented Programming. Solved MCQs - Part 2

Data Structures (list, dictionary, tuples, sets, strings)

Transcription:

CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science department Lecture 4 : Classes & Objects Lecture Contents What is a class? Class definition: Data Methods Constructors Properties (set/get) objects Object initializers Static members Readonly members Composition 1

What is a class? Class: like a struct in C++, defines a data type (blueprint) Combines data and operations in one place: data / fields/ state variables Operations/ behavior / methods that modify state Encapsulation The bundling of data and procedures(methods) into a single unit(called class). Class : various data elements and member functions are wrapped up together. main feature of object oriented programming 2

Objects & Classes variables of class type are objects Object: an instance / realization of a class same as relationship between a datatype and its variables Blueprint of a house Design build use Attributes?? Methods?? 3

the Unified Modeling Language (UML) can be defined as a modeling language to capture the architectural, behavioral and structural aspects of a system. The UML has an important role in object oriented analysis and design. Objects are the key to this object oriented world The basic requirement of is to identify the object efficiently. Example Mark Visibility type + Public # Protected - Dr. Amal Khalifa, Private spr17 4

Access modifiers Public items (usually member functions) are "user-accessible Outside of class definition, cannot change (or even access) private data Initializing data with Constructors A constructor can be used to initialize an object of a class when the object is created. Every class must have at least one constructor. If you do not provide any constructors in a class s declaration the compiler creates a default constructor that takes no arguments when it is invoked. if you declare any constructors no default constructor will be created for that class With the default constructor, its instance variables are initialized to their default values. 5

Be careful!! Overloaded Constructors Overloaded constructors enable objects of a class to be initialized in different ways. multiple constructor declarations with different signatures. the compiler differentiates signatures by : the number of parameters, the types of the parameters and the order of the parameter types in each signature. 6

How to define a Class?? Class type definition Object declaration Methods call Time Class Case Study Class Time2 represents the time of day in universal-time format (24-hour clock format). instance variables hour, minute and second methods: Property set/get five overloaded Constructors settime ToUniversalString ( hh:mm:ss) ToString (hh:mm:ss AM/PM) 7

The Time2 Class Private data Overloaded constructors Constructor initializers Using this operator The Time2 Class get/set provide controlled ways to get (i.e., retrieve) the value in an instance variable and set (i.e., modify) the value in an instance variable. 8

The Time2 Class get/set By convention, we name each property with the capitalized name of the instance variable that it manipulates The Time2 Class datavalidation Convert to string representation 9

The Time2 Class Convert to string representation Test!! 10

Test Test 11

Object Initializers allow you to create an object and initialize its public properties (and public instance variables, if any) in the same statement. Can be useful when a class does not provide an appropriate constructor, but does provide properties on the class s data. 12

The destructor Object destructor: Same as the class name, preceded by a tilde (~), with no access modifier in its header. called by the Common Language Runtime (CLR) garbage collector to perform termination housekeeping on an before its memory is reclaimed. not guaranteed to execute at a specified time. Rarely used static Class Members static field: called a class variable represents class-wide information only one copy is be shared by all objects of a class. The declaration begins with the keyword static. public static members can be accessed through a reference to any object of the class, or by qualifying the member name with the class name and a dot (.). A class s private static class members can be accessed only through the methods and properties of the class. To access a private static member from outside its class, a public static method or property can be provided 13

Tip!! readonly Instance Variables C# provides keyword readonly to specify that an instance variable of an object is not modifiable and that any attempt to modify it after the object is constructed is an error. Like constants, readonly variables are declared with all capital letters by convention readonly instance variables can be initialized when they are declared, but this is not required. Members that are declared as const must be assigned values at compile time, whereas members declared with keyword readonly, can be initialized at execution time. If a class provides multiple constructors, every constructor should initialize a readonly variable. 14

Composition A class can have references to objects of other classes as members. This is called composition and is sometimes referred to as a has-a relationship. Examples: An AlarmClock object needs two references to Time objects to know the current time and the time when it s supposed to sound its alarm. A Robot Object has-a MechanicalArm A car Object has-a Wheel A student Object has-a BirthDate You name it!!! Case study Read-only fields 15

16

17

That s all for today Chapter 4 Chapter 10 Case Study 18