Working with Objects. Overview. This chapter covers. ! Overview! Properties and Fields! Initialization! Constructors! Assignment

Size: px
Start display at page:

Download "Working with Objects. Overview. This chapter covers. ! Overview! Properties and Fields! Initialization! Constructors! Assignment"

Transcription

1 4 Working with Objects 41 This chapter covers! Overview! Properties and Fields! Initialization! Constructors! Assignment Overview When you look around yourself, in your office; your city; or even the world, there are objects. Each of these object have specific properties and behaviors and some of these object would have common properties and behaviors. For example, cats have behaviors such as meowing, scratching, and sleeping where as dogs bark, chewing, and fetching a ball. Properties include ears, teeth, and tails. But, when you think about it, they both share properties and behaviors; they are both animals and make sounds. The foundation to learning object-oriented programming is understanding classes and objects. We have already learned that C# has types for numbers and strings. Classes allow you to define new types - complex types where these types have fields, properties, methods and constructors and we will talk in depth about all of these.

2 42 Properties and Fields Let's create a new program and take a look at the creation of classes and objects; as usual, we ll create a new Windows C# Console Application and name it ClassProgram. The first thing to understand is that classes should be created in their own file a class file. To do this, you need to right-click over top of the project name ClassProgram as shown below: You should be shown a large list of options that can be performed on a project but we are wanted to add a new item to the project a new class. Hover over top of the Add option and when presented with yet another list of items, select Class. You ll be presented with the Add Item dialog screen with the Class item selected in the list of items. At the bottom of the dialog, enter the name Employee.cs and click on the Add button. Once done, your new file should appear in the list of files in your project as shown on the right. Now this class might be used in any number of programs to represent employees in a company. There are many fields and properties that a typical employee might have, but let's talk about fields and properties for a moment and get that distinction clear, and then talk about how we use properties in modern C#. A Class Field A field is just a member and it can be private or public. Private means that the field can only be seen by methods of the class itself; in our case the Employee Class. When we declare a field as Public, it can be seen by any method of any class. Later we'll take a look at what the C# Fundamentals

3 43 difference is but for now let's start simple public field of type integer - the employee's age and for the purpose of learning we will just add this one field. Now, click on the Program.cs file and lets make an instance of Employee - or an Employee object. Instance and object are really the same thing they are synonymous. Let s review the following example: In the example above, notice how we create a new instance of Employee. This is quite similar to creating a new string variable; we start with typing the Name of the Class (Employee) followed by the name of the variable in our case allan. The main difference is that we new to make it equal (or assign it) to a new Employee(). This is basically saying Create a spot in memory that will hold an Employee object. Once we have an instance of the Employee Object, we can access the fields that are defined in this new object. The next line is doing just that; we are assigning the age field and assigning it the integer number 27. Lastly, to test that our new object now has this new age value, we ll output it to the console, as shown on the right. That all works just fine, but age is part of the internal state of the employee, and one of the key concepts in object-oriented programming, encapsulation and data hiding, indicate that we should not have this data be public. It should not be available to the function main, which is not a function of the employee class. A Property Best practices in object-orientated programming state that this field should be private to the class employee, and access to that should be mediated through a property. The reason for that, among others, is that you may change the way you hold or store the employee age. You may decide at a later time to get that from a database or to compute that from the employee's birthdate and the current date. So, the more common way, and preferred way, to handle this is to make age private. Then, what we'd like is something that looks like a field to this client, but acts like a method to this employee object, and that's where properties come in. Let s examine the following: With a property, what we can do is come along and say public int Age, and it's common in C# to have the private backing variable be lowercase and the public variable be initial uppercase.

4 44 Remember C# is case sensitive, so those are different. Public int Age. And then within the braces, we create a getter and a setter. And the getter will just return the private variable age, and the setter will just set the private variable age to value. And what's value? Value is a hidden parameter that is passed into set. Now, let s add another property to our Employee Class Name, as shown on the right: Just like our Age property, we have now added a Name property; a backing variable named name with a property defined as Name with, again, a getter and a setter. They way these properties are used back in our main program is like this. The way the program using an instance of this Employee class is when it wants to set the age we say allan.age = 35. Notice that to the main program this looks like you're working with a field. You simply set the Age equal to 35. That calls the setter automatically and the 35 is passed in as the value automatically, and the backing variable age is then set. We can then get the age by saying allan.age and the getter is called automatically and the backing field is retrieved and returned.. So, a lot of that work is done for you. And now you have a private member age, which you can change later to be computed or whatever you want, and a public getter and setter that the user can use as if they were accessing a field directly, and this works just as well as the previous example. Similarly, the same happens for the Name property; setting the Name = Allan (for example) passes the string Allan as the value in the setter and when you access the Name property for retrieving the name the getter is called, as shown below: A Better way to create Properties But it gets even better. This is such a common repetitive coding task; to have a backing variable called age and another public variable called Age (with a capital) and a getter that just returns it and a setter that just sets it. Rather than having to type that again and again as we're doing here with the name field and property; rather than doing that and having many of these all using the same format; there's an incredibly convenient shortcut as shown below: C# Fundamentals

5 45 The new Employee class with its two properties Name and Age is the equivalent to all of the code we had before. In essence, the compiler sees these two lines of code and creates what we had before; a backing variable, the public property, and the properties getters and setters remember, we hate typing and these types of short cuts helps us focus more on the application than typing repetitive code over and over. I should note that at any time, you can expand one of these properties and put more into the getter or more into the setter, but if you need just the standard get and set and want to use properties instead of fields, this new short syntax is tremendously useful. And notice that I didn t need to make any changes to the body of my program inside main. We still use allan.age to set and get the value, and, in fact, when we run it we get the same results. Initialization and Constructors At this point we have an Employee Class that has two properties. Let's give it a few more: As shown above, I've added a salary, a starting date, and a phone number. When we instantiated our employee allan, we did this using the new Employee(). There are three ways to get data into that instance of allan and we have already used one of them by simply assigning the properties to the new object one at a time (allan.age = 27 and allan.name= Allan ). A second way of assigning data to the object is through a process called initialization. With initialization, at the time we create the employee, we can also initialize one or more of the properties. Let s examine the following piece of code:

6 46 This may look a little confusing at first glance and there are a few different new things to explain but once to work with this type of initialization a few times it will become second nature. Initialization First, after the new Employee() we are replacing the semi-colon with an open and closing squiggly brace a block. Inside of this we have access to all of the public properties and can assign them at the time of creating the new instance of Employee. In many ways, this is quite similar to how we were assigning the properties before but now we can save a bunch of typing by not having to have the allan. object reference in front of the property names. Also in the example above I am introducing a couple new shortcuts; the first is assigning a date to the DateTime object defined in the employee class. DateTime is a date and time class in the.net Framework and offers a complete list of functionality when dealing with dates and Time. Similar to how we create a new Employee class we are creating a new DateTime object and accessing one of its constructors (more on this later) that requires a year, month and then a day; this returns a new constructed object with the date October 28, The other new bit of code I am using is how I m using the Console.WriteLine. Instead of having five separate calls to WriteLine for each of the properties in our employee object, I am inserting a \n between the properties which just instructs the Console.WriteLine to place a new line after each property. The output is as shown on the right. The other, and third, way of initializing the fields in the new object is through a special method called a Constructor. Now, we've only alluded to methods, but methods are simply functions that are members of the class. And most methods are very normal in that they have a return type or void, they take 0 or more parameters, and there's a body containing statements - to the method. So, for example, we might give the employee class a method called AssignBonus. So, let's look at this method that we've added to our employee class called AssignBonus. Reviewing our new Employee Class we can see that AssignBonus does not return a value, so we have it return void, it takes one argument of type double called increasepercentage, and what it does is it does is it adds to the salary, Salary * increasepercentage. C# Fundamentals

7 47 In our running program that we've been building, let s give allan a bonus by calling allan.assignbonus and passing in 5 percent as shown below: allan.assignbonus(0.05); And, when we run the application we see that allan is now making $78,750 from his previous salary of $75,000 a 5% increase. So, that's a standard method of a class, but we we're talking about constructors. I just wanted to create a standard method so that you d see how Constructors are different. Constructors Constructors are special methods of the class, and they are used to construct an instance. What's interesting about constructors is that they don't have a return type at all, not even void, and they must have the name of the class itself, in our case, Employee. However, they do typically take parameters, and what is very common is for them to have a parameter for each of the properties of the class. So, our constructor might have an int for age and a string for name and so on for each of the various properties. Then within the body of the body of the constructor, you can assign these passed in parameters to the property. And so you can say Age = age and Name = name and so forth; as shown here in the Constructor shown below: Now, you have a way of constructing the object by passing parameters into the constructor at the time you create the object. Notice in our current code we are creating an instance of Employee and with nothing in the brackets and now, in fact, has the squiggly line below it indicating that we have an error. Any constructor that has no parameters is called a default constructor, and, in fact, if you don't create a constructor, you are given a default constructor by default. That default constructor, which takes no parameters, does nothing. This is why we had to do the initialization. However, now that I've created a constructor, I no longer get my free default constructor in the

8 48 Employee Class need to supply my own Constructor that contains no parameters. Fortunately, we can do something called method overloading where we have two methods with the same name, but different parameters, either different types or a different number of parameters. Let s add a another Constructor using method overloading. Now, we can instantiate an Employee without parameters as we have thus far or we can instantiate an employee by passing in values, where these values would then be passed into the constructor with parameters. Let s give this a try; we ll create a new employee, Mary = new Employee but this time we're going to use our second constructor. You should notice that IntelliSense will now show us that we have two constructors now and if I het the down arrow it will display the new constructor with all of the parameters we defined. Also, as you enter the values to be passed to our new constructor IntelliSense makes it very easy to fill them in because it takes you through them on-by-one. So, the first one is Mary's Name, we'll make her 39, and let's give her a salary of dollars. Her start date, which will be a new DateTime of February 17, Hit the comma, and it says you still need the phone number as a string, let s enter shown below: Now, we have created an employee, not by initializing it, by passing in values to the constructor, and yet, we can once again add a Console.WriteLine. And now let's run the program, and we've got Mary's age is 39, she started on February 17 th, and makes Key here is we were able to set all of the values in the employee object through the constructor that took those arguments. We could, of course, have a constructor that takes some arguments and not others, and we can even use initialization when we have a constructor. So, you can mix and match these various ways of putting data into your object. C# Fundamentals

9 49 Summary In this chapter, we talked about classes and objects, and we saw how classes define a type and objects are instances of that type. Classes can have fields, which are the data for the class, and they can have properties, which are the way that other methods of other classes interact with the data of the class. Classes can also have methods, which represent the behavior of the objects in that class. An instance of a class is called an object, and classes exist to instantiate objects, each of which has its own data. The constructor can be used to create an instance of a class and to populate the data of that class. A constructor that takes no arguments is called a default constructor, and if you don't create any constructors for your class, you will receive a default constructor by default. We also looked at initialization in which you can set the values of the members of the class at the time you create it.

10 50 C# Fundamentals

Chapter 1 Getting Started

Chapter 1 Getting Started Chapter 1 Getting Started The C# class Just like all object oriented programming languages, C# supports the concept of a class. A class is a little like a data structure in that it aggregates different

More information

MITOCW watch?v=kz7jjltq9r4

MITOCW watch?v=kz7jjltq9r4 MITOCW watch?v=kz7jjltq9r4 PROFESSOR: We're going to look at the most fundamental of all mathematical data types, namely sets, and let's begin with the definitions. So informally, a set is a collection

More information

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet. Mr G s Java Jive #2: Yo! Our First Program With this handout you ll write your first program, which we ll call Yo. Programs, Classes, and Objects, Oh My! People regularly refer to Java as a language that

More information

printf( Please enter another number: ); scanf( %d, &num2);

printf( Please enter another number: ); scanf( %d, &num2); CIT 593 Intro to Computer Systems Lecture #13 (11/1/12) Now that we've looked at how an assembly language program runs on a computer, we're ready to move up a level and start working with more powerful

More information

Chapter 7 Classes & Objects, Part B

Chapter 7 Classes & Objects, Part B Chapter 7 Classes & Objects, Part B These note present Dog simulation example that shows how we go about OO modeling. A number of new things are introduced. They also present the Person > BirthDate example.

More information

IT 374 C# and Applications/ IT695 C# Data Structures

IT 374 C# and Applications/ IT695 C# Data Structures IT 374 C# and Applications/ IT695 C# Data Structures Module 2.1: Introduction to C# App Programming Xianrong (Shawn) Zheng Spring 2017 1 Outline Introduction Creating a Simple App String Interpolation

More information

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements Programming, Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science and Engineering Indian Institute of Technology, Madras Lecture 05 I/O statements Printf, Scanf Simple

More information

For this chapter, switch languages in DrRacket to Advanced Student Language.

For this chapter, switch languages in DrRacket to Advanced Student Language. Chapter 30 Mutation For this chapter, switch languages in DrRacket to Advanced Student Language. 30.1 Remembering changes Suppose you wanted to keep track of a grocery shopping list. You could easily define

More information

Your First Windows Form

Your First Windows Form Your First Windows Form From now on, we re going to be creating Windows Forms Applications, rather than Console Applications. Windows Forms Applications make use of something called a Form. The Form is

More information

Chapter 10 Recursion

Chapter 10 Recursion Chapter 10 Recursion Written by Dr. Mark Snyder [minor edits for this semester by Dr. Kinga Dobolyi] Recursion implies that something is defined in terms of itself. We will see in detail how code can be

More information

Chapter 11 Generics. Generics. Hello!

Chapter 11 Generics. Generics. Hello! Chapter 11 Generics Written by Dr. Mark Snyder [minor edits for this semester by Dr. Kinga Dobolyi] Hello! The topic for this chapter is generics. Collections rely heavily upon generics, and so they are

More information

************ THIS PROGRAM IS NOT ELIGIBLE FOR LATE SUBMISSION. ALL SUBMISSIONS MUST BE RECEIVED BY THE DUE DATE/TIME INDICATED ABOVE HERE

************ THIS PROGRAM IS NOT ELIGIBLE FOR LATE SUBMISSION. ALL SUBMISSIONS MUST BE RECEIVED BY THE DUE DATE/TIME INDICATED ABOVE HERE Program 10: 40 points: Due Tuesday, May 12, 2015 : 11:59 p.m. ************ THIS PROGRAM IS NOT ELIGIBLE FOR LATE SUBMISSION. ALL SUBMISSIONS MUST BE RECEIVED BY THE DUE DATE/TIME INDICATED ABOVE HERE *************

More information

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014 Lesson 10A OOP Fundamentals By John B. Owen All rights reserved 2011, revised 2014 Table of Contents Objectives Definition Pointers vs containers Object vs primitives Constructors Methods Object class

More information

6.001 Notes: Section 17.5

6.001 Notes: Section 17.5 6.001 Notes: Section 17.5 Slide 17.5.1 Now, let's look at one example in which changing the evaluation model allows us to explore a very different kind of computational problem. Our goal is to show how

More information

MITOCW MIT6_01SC_rec2_300k.mp4

MITOCW MIT6_01SC_rec2_300k.mp4 MITOCW MIT6_01SC_rec2_300k.mp4 KENDRA PUGH: Hi. I'd like to talk to you today about inheritance as a fundamental concept in object oriented programming, its use in Python, and also tips and tricks for

More information

Software Design and Analysis for Engineers

Software Design and Analysis for Engineers Software Design and Analysis for Engineers by Dr. Lesley Shannon Email: lshannon@ensc.sfu.ca Course Website: http://www.ensc.sfu.ca/~lshannon/courses/ensc251 Simon Fraser University Slide Set: 2 Date:

More information

Programming Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science Indian Institute of Technology, Madras

Programming Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science Indian Institute of Technology, Madras Programming Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science Indian Institute of Technology, Madras Module 12B Lecture - 41 Brief introduction to C++ Hello, welcome

More information

Lecture 21: The Many Hats of Scala: OOP 10:00 AM, Mar 14, 2018

Lecture 21: The Many Hats of Scala: OOP 10:00 AM, Mar 14, 2018 CS18 Integrated Introduction to Computer Science Fisler, Nelson Lecture 21: The Many Hats of Scala: OOP 10:00 AM, Mar 14, 2018 Contents 1 Mutation in the Doghouse 1 1.1 Aside: Access Modifiers..................................

More information

Thursday, February 16, More C++ and root

Thursday, February 16, More C++ and root More C++ and root Today s Lecture Series of topics from C++ Example of thinking though a problem Useful physics classes in ROOT Functions by this point you are used to the syntax of a function pass by

More information

Notes on Chapter Three

Notes on Chapter Three Notes on Chapter Three Methods 1. A Method is a named block of code that can be executed by using the method name. When the code in the method has completed it will return to the place it was called in

More information

Introduction To C#.NET

Introduction To C#.NET Introduction To C#.NET Microsoft.Net was formerly known as Next Generation Windows Services(NGWS).It is a completely new platform for developing the next generation of windows/web applications. However

More information

An Introduction to C++

An Introduction to C++ An Introduction to C++ Introduction to C++ C++ classes C++ class details To create a complex type in C In the.h file Define structs to store data Declare function prototypes The.h file serves as the interface

More information

MITOCW watch?v=flgjisf3l78

MITOCW watch?v=flgjisf3l78 MITOCW watch?v=flgjisf3l78 The following content is provided under a Creative Commons license. Your support will help MIT OpenCourseWare continue to offer high-quality educational resources for free. To

More information

Object oriented programming Concepts

Object oriented programming Concepts Object oriented programming Concepts Naresh Proddaturi 09/10/2012 Naresh Proddaturi 1 Problems with Procedural language Data is accessible to all functions It views a program as a series of steps to be

More information

MITOCW watch?v=4dj1oguwtem

MITOCW watch?v=4dj1oguwtem MITOCW watch?v=4dj1oguwtem PROFESSOR: So it's time to examine uncountable sets. And that's what we're going to do in this segment. So Cantor's question was, are all sets the same size? And he gives a definitive

More information

IDM 232. Scripting for Interactive Digital Media II. IDM 232: Scripting for IDM II 1

IDM 232. Scripting for Interactive Digital Media II. IDM 232: Scripting for IDM II 1 IDM 232 Scripting for Interactive Digital Media II IDM 232: Scripting for IDM II 1 PHP HTML-embedded scripting language IDM 232: Scripting for IDM II 2 Before we dive into code, it's important to understand

More information

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

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture - 43 Dynamic Binding (Polymorphism): Part III Welcome to Module

More information

Slide 1 CS 170 Java Programming 1

Slide 1 CS 170 Java Programming 1 CS 170 Java Programming 1 Objects and Methods Performing Actions and Using Object Methods Slide 1 CS 170 Java Programming 1 Objects and Methods Duration: 00:01:14 Hi Folks. This is the CS 170, Java Programming

More information

MITOCW watch?v=hverxup4cfg

MITOCW watch?v=hverxup4cfg MITOCW watch?v=hverxup4cfg PROFESSOR: We've briefly looked at graph isomorphism in the context of digraphs. And it comes up in even more fundamental way really for simple graphs where the definition is

More information

6.001 Notes: Section 15.1

6.001 Notes: Section 15.1 6.001 Notes: Section 15.1 Slide 15.1.1 Our goal over the next few lectures is to build an interpreter, which in a very basic sense is the ultimate in programming, since doing so will allow us to define

More information

static CS106L Spring 2009 Handout #21 May 12, 2009 Introduction

static CS106L Spring 2009 Handout #21 May 12, 2009 Introduction CS106L Spring 2009 Handout #21 May 12, 2009 static Introduction Most of the time, you'll design classes so that any two instances of that class are independent. That is, if you have two objects one and

More information

Programming Languages (Outsource: 2-2)

Programming Languages (Outsource: 2-2) Programming Languages (Outsource: 2-2) A computer program is a set of instructions that you write to tell a computer what to do. There are many languages used to write computer programs, each language

More information

1 Getting used to Python

1 Getting used to Python 1 Getting used to Python We assume you know how to program in some language, but are new to Python. We'll use Java as an informal running comparative example. Here are what we think are the most important

More information

COPYRIGHTED MATERIAL. Starting Strong with Visual C# 2005 Express Edition

COPYRIGHTED MATERIAL. Starting Strong with Visual C# 2005 Express Edition 1 Starting Strong with Visual C# 2005 Express Edition Okay, so the title of this chapter may be a little over the top. But to be honest, the Visual C# 2005 Express Edition, from now on referred to as C#

More information

Programming Using C# QUEEN S UNIVERSITY BELFAST. Practical Week 7

Programming Using C# QUEEN S UNIVERSITY BELFAST. Practical Week 7 Programming Using C# QUEEN S UNIVERSITY BELFAST Practical Week 7 Table of Contents PRACTICAL 7... 2 EXERCISE 1... 2 TASK 1: Zoo Park (Without Inheritance)... 2 TASK 2: Zoo Park with Inheritance... 5 TASK

More information

Basic Object-Oriented Concepts. 5-Oct-17

Basic Object-Oriented Concepts. 5-Oct-17 Basic Object-Oriented Concepts 5-Oct-17 Concept: An object has behaviors In old style programming, you had: data, which was completely passive functions, which could manipulate any data An object contains

More information

COMP 250 Fall inheritance Nov. 17, 2017

COMP 250 Fall inheritance Nov. 17, 2017 Inheritance In our daily lives, we classify the many things around us. The world has objects like dogs and cars and food and we are familiar with talking about these objects as classes Dogs are animals

More information

BEGINNER PHP Table of Contents

BEGINNER PHP Table of Contents Table of Contents 4 5 6 7 8 9 0 Introduction Getting Setup Your first PHP webpage Working with text Talking to the user Comparison & If statements If & Else Cleaning up the game Remembering values Finishing

More information

Using the API: Introductory Graphics Java Programming 1 Lesson 8

Using the API: Introductory Graphics Java Programming 1 Lesson 8 Using the API: Introductory Graphics Java Programming 1 Lesson 8 Using Java Provided Classes In this lesson we'll focus on using the Graphics class and its capabilities. This will serve two purposes: first

More information

Slide 1 CS 170 Java Programming 1 Multidimensional Arrays Duration: 00:00:39 Advance mode: Auto

Slide 1 CS 170 Java Programming 1 Multidimensional Arrays Duration: 00:00:39 Advance mode: Auto CS 170 Java Programming 1 Working with Rows and Columns Slide 1 CS 170 Java Programming 1 Duration: 00:00:39 Create a multidimensional array with multiple brackets int[ ] d1 = new int[5]; int[ ][ ] d2;

More information

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

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture - 31 Static Members Welcome to Module 16 of Programming in C++.

More information

Chapter 6 Classes and Objects

Chapter 6 Classes and Objects Chapter 6 Classes and Objects Hello! Today we will focus on creating classes and objects. Now that our practice problems will tend to generate multiple files, I strongly suggest you create a folder for

More information

Pointers, Arrays and Parameters

Pointers, Arrays and Parameters Pointers, Arrays and Parameters This exercise is different from our usual exercises. You don t have so much a problem to solve by creating a program but rather some things to understand about the programming

More information

Advanced Object-Oriented Programming. 11 Features. C# Programming: From Problem Analysis to Program Design. 4th Edition

Advanced Object-Oriented Programming. 11 Features. C# Programming: From Problem Analysis to Program Design. 4th Edition 11 Features Advanced Object-Oriented Programming C# Programming: From Problem Analysis to Program Design C# Programming: From Problem Analysis to Program Design 1 4th Edition Chapter Objectives Learn the

More information

PROFESSOR: Last time, we took a look at an explicit control evaluator for Lisp, and that bridged the gap between

PROFESSOR: Last time, we took a look at an explicit control evaluator for Lisp, and that bridged the gap between MITOCW Lecture 10A [MUSIC PLAYING] PROFESSOR: Last time, we took a look at an explicit control evaluator for Lisp, and that bridged the gap between all these high-level languages like Lisp and the query

More information

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal COMSC-051 Java Programming Part 1 Part-Time Instructor: Joenil Mistal Chapter 4 4 Moving Toward Object- Oriented Programming This chapter provides a provides an overview of basic concepts of the object-oriented

More information

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n)

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n) Programming, Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science and Engineering Indian Institute of Technology, Madras Module 10A Lecture - 20 What is a function?

More information

CS100J, Fall 2003 Preparing for Prelim 1: Monday, 29 Sept., 7:30 9:00PM

CS100J, Fall 2003 Preparing for Prelim 1: Monday, 29 Sept., 7:30 9:00PM CS100J, Fall 2003 Preparing for Prelim 1: Monday, 29 Sept., 7:30 9:00PM This handout explains what you have to know for the first prelim. Terms and their meaning Below, we summarize the terms you should

More information

Lecture 34 SDLC Phases and UML Diagrams

Lecture 34 SDLC Phases and UML Diagrams That Object-Oriented Analysis and Design Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology-Kharagpur Lecture 34 SDLC Phases and UML Diagrams Welcome

More information

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

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved. Java How to Program, 10/e Education, Inc. All Rights Reserved. Each class you create becomes a new type that can be used to declare variables and create objects. You can declare new classes as needed;

More information

In our classes, we've had

In our classes, we've had In our classes, we've had Attributes that have a visibility of public to store state and identity for objects Methods for behaviour so far these have simply printed things to the console (possibly after

More information

G52CPP C++ Programming Lecture 9

G52CPP C++ Programming Lecture 9 G52CPP C++ Programming Lecture 9 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Last lecture const Constants, including pointers The C pre-processor And macros Compiling and linking And

More information

If Statements, For Loops, Functions

If Statements, For Loops, Functions Fundamentals of Programming If Statements, For Loops, Functions Table of Contents Hello World Types of Variables Integers and Floats String Boolean Relational Operators Lists Conditionals If and Else Statements

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

Introductory ios Development

Introductory ios Development Instructor s Introductory ios Development Unit 3 - Objective-C Classes Introductory ios Development 152-164 Unit 3 - Swift Classes Quick Links & Text References Structs vs. Classes Structs intended for

More information

(5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University

(5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University (5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University Key Concepts Function templates Defining classes with member functions The Rule

More information

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing.

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing. CISC-124 20180115 20180116 20180118 We continued our introductory exploration of Java and object-oriented programming by looking at a program that uses two classes. We created a Java file Dog.java and

More information

Chapter 15: Object Oriented Programming

Chapter 15: Object Oriented Programming Chapter 15: Object Oriented Programming Think Java: How to Think Like a Computer Scientist 5.1.2 by Allen B. Downey How do Software Developers use OOP? Defining classes to create objects UML diagrams to

More information

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

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity. OOPS Viva Questions 1. What is OOPS? OOPS is abbreviated as Object Oriented Programming system in which programs are considered as a collection of objects. Each object is nothing but an instance of a class.

More information

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch Problem Solving Creating a class from scratch 1 Recipe for Writing a Class 1. Write the class boilerplate stuff 2. Declare Fields 3. Write Creator(s) 4. Write accessor methods 5. Write mutator methods

More information

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes Based on Introduction to Java Programming, Y. Daniel Liang, Brief Version, 10/E 1 Creating Classes and Objects Classes give us a way of defining custom data types and associating data with operations on

More information

Hands-On Lab. Introduction to F# Lab version: Last updated: 12/10/2010. Page 1

Hands-On Lab. Introduction to F# Lab version: Last updated: 12/10/2010. Page 1 Hands-On Lab Introduction to Lab version: 1.0.0 Last updated: 12/10/2010 Page 1 CONTENTS OVERVIEW... 3 EXERCISE 1: TYPES IN... 4 Task 1 Observing Type Inference in... 4 Task 2 Working with Tuples... 6

More information

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics WIT COMP1000 Java Basics Java Origins Java was developed by James Gosling at Sun Microsystems in the early 1990s It was derived largely from the C++ programming language with several enhancements Java

More information

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University Day 4 COMP1006/1406 Summer 2016 M. Jason Hinek Carleton University today s agenda assignments questions about assignment 2 a quick look back constructors signatures and overloading encapsulation / information

More information

1. Write two major differences between Object-oriented programming and procedural programming?

1. Write two major differences between Object-oriented programming and procedural programming? 1. Write two major differences between Object-oriented programming and procedural programming? A procedural program is written as a list of instructions, telling the computer, step-by-step, what to do:

More information

Fundamental C# Programming

Fundamental C# Programming Part 1 Fundamental C# Programming In this section you will find: Chapter 1: Introduction to C# Chapter 2: Basic C# Programming Chapter 3: Expressions and Operators Chapter 4: Decisions, Loops, and Preprocessor

More information

Creating a Class Library You should have your favorite version of Visual Studio open. Richard Kidwell. CSE 4253 Programming in C# Worksheet #2

Creating a Class Library You should have your favorite version of Visual Studio open. Richard Kidwell. CSE 4253 Programming in C# Worksheet #2 Worksheet #2 Overview For this worksheet, we will create a class library and then use the resulting dynamic link library (DLL) in a console application. This worksheet is a start on your next programming

More information

Math Modeling in Java: An S-I Compartment Model

Math Modeling in Java: An S-I Compartment Model 1 Math Modeling in Java: An S-I Compartment Model Basic Concepts What is a compartment model? A compartment model is one in which a population is modeled by treating its members as if they are separated

More information

Intro. Classes Beginning Objected Oriented Programming. CIS 15 : Spring 2007

Intro. Classes Beginning Objected Oriented Programming. CIS 15 : Spring 2007 Intro. Classes Beginning Objected Oriented Programming CIS 15 : Spring 2007 Functionalia HW 4 Review. HW Out this week. Today: Linked Lists Overview Unions Introduction to Classes // Create a New Node

More information

Objective-C. Stanford CS193p Fall 2013

Objective-C. Stanford CS193p Fall 2013 New language to learn! Strict superset of C Adds syntax for classes, methods, etc. A few things to think differently about (e.g. properties, dynamic binding) Most important concept to understand today:

More information

(Refer Slide Time: 4:00)

(Refer Slide Time: 4:00) Principles of Programming Languages Dr. S. Arun Kumar Department of Computer Science & Engineering Indian Institute of Technology, Delhi Lecture - 38 Meanings Let us look at abstracts namely functional

More information

CS Problem Solving and Object-Oriented Programming

CS Problem Solving and Object-Oriented Programming CS 101 - Problem Solving and Object-Oriented Programming Lab 5 - Draw a Penguin Due: October 28/29 Pre-lab Preparation Before coming to lab, you are expected to have: Read Bruce chapters 1-3 Introduction

More information

Fortunately, you only need to know 10% of what's in the main page to get 90% of the benefit. This page will show you that 10%.

Fortunately, you only need to know 10% of what's in the main page to get 90% of the benefit. This page will show you that 10%. NAME DESCRIPTION perlreftut - Mark's very short tutorial about references One of the most important new features in Perl 5 was the capability to manage complicated data structures like multidimensional

More information

A function is a named piece of code that performs a specific task. Sometimes functions are called methods, procedures, or subroutines (like in LC-3).

A function is a named piece of code that performs a specific task. Sometimes functions are called methods, procedures, or subroutines (like in LC-3). CIT Intro to Computer Systems Lecture # (//) Functions As you probably know from your other programming courses, a key part of any modern programming language is the ability to create separate functions

More information

Software Design and Analysis for Engineers

Software Design and Analysis for Engineers Software Design and Analysis for Engineers by Dr. Lesley Shannon Email: lshannon@ensc.sfu.ca Course Website: http://www.ensc.sfu.ca/~lshannon/courses/ensc251 Simon Fraser University Slide Set: 1 Date:

More information

The Parts of a C++ Program

The Parts of a C++ Program HOUR 2 The Parts of a C++ Program What You ll Learn in This Hour:. Why you should choose C++. The specific parts of any C++ program. How the parts work together. What a function is and what it does Why

More information

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

A A B U n i v e r s i t y A A B U n i v e r s i t y Faculty of Computer Sciences O b j e c t O r i e n t e d P r o g r a m m i n g Week 4: Introduction to Classes and Objects Asst. Prof. Dr. M entor Hamiti mentor.hamiti@universitetiaab.com

More information

02 Features of C#, Part 1. Jerry Nixon Microsoft Developer Evangelist Daren May President & Co-founder, Crank211

02 Features of C#, Part 1. Jerry Nixon Microsoft Developer Evangelist Daren May President & Co-founder, Crank211 02 Features of C#, Part 1 Jerry Nixon Microsoft Developer Evangelist Daren May President & Co-founder, Crank211 Module Overview Constructing Complex Types Object Interfaces and Inheritance Generics Constructing

More information

Week - 01 Lecture - 04 Downloading and installing Python

Week - 01 Lecture - 04 Downloading and installing Python Programming, Data Structures and Algorithms in Python Prof. Madhavan Mukund Department of Computer Science and Engineering Indian Institute of Technology, Madras Week - 01 Lecture - 04 Downloading and

More information

IT150/IT152 Concepts Summary Sheet

IT150/IT152 Concepts Summary Sheet (Examples within this study guide/summary sheet are given in C#.) Variables All data in a computer program, whether calculated during runtime or entered by the user, must be stored somewhere in the memory

More information

Lesson 3: Accepting User Input and Using Different Methods for Output

Lesson 3: Accepting User Input and Using Different Methods for Output Lesson 3: Accepting User Input and Using Different Methods for Output Introduction So far, you have had an overview of the basics in Java. This document will discuss how to put some power in your program

More information

The Essence of Object Oriented Programming with Java and UML. Chapter 2. The Essence of Objects. What Is an Object-Oriented System?

The Essence of Object Oriented Programming with Java and UML. Chapter 2. The Essence of Objects. What Is an Object-Oriented System? Page 1 of 21 Page 2 of 21 and identity. Objects are members of a class, and the attributes and behavior of an object are defined by the class definition. The Essence of Object Oriented Programming with

More information

vi Primer Adapted from:

vi Primer Adapted from: Adapted from: http://courses.knox.edu/cs205/205tutorials/viprimer.html vi Primer This document is designed to introduce you to the standard UNIX screen editor, vi (short for "visual"). Vi can be used to

More information

Collections, Maps and Generics

Collections, Maps and Generics Collections API Collections, Maps and Generics You've already used ArrayList for exercises from the previous semester, but ArrayList is just one part of much larger Collections API that Java provides.

More information

Chapter 5 Object-Oriented Programming

Chapter 5 Object-Oriented Programming Chapter 5 Object-Oriented Programming Develop code that implements tight encapsulation, loose coupling, and high cohesion Develop code that demonstrates the use of polymorphism Develop code that declares

More information

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

G52CPP C++ Programming Lecture 7. Dr Jason Atkin G52CPP C++ Programming Lecture 7 Dr Jason Atkin 1 This lecture classes (and C++ structs) Member functions inline functions 2 Last lecture: predict the sizes 3 #pragma pack(1) #include struct A

More information

Chapter 1: Object-Oriented Programming Using C++

Chapter 1: Object-Oriented Programming Using C++ Chapter 1: Object-Oriented Programming Using C++ Objectives Looking ahead in this chapter, we ll consider: Abstract Data Types Encapsulation Inheritance Pointers Polymorphism Data Structures and Algorithms

More information

Introduction to Game Programming Lesson 4 Lecture Notes

Introduction to Game Programming Lesson 4 Lecture Notes Introduction to Game Programming Lesson 4 Lecture Notes Learning Objectives: Following this lecture, the student should be able to: Define frame rate List the factors that affect the amount of time a game

More information

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable? Peer Instruction 8 Classes and Objects How can multiple methods within a Java class read and write the same variable? A. Allow one method to reference a local variable of the other B. Declare a variable

More information

GEA 2017, Week 4. February 21, 2017

GEA 2017, Week 4. February 21, 2017 GEA 2017, Week 4 February 21, 2017 1. Problem 1 After debugging the program through GDB, we can see that an allocated memory buffer has been freed twice. At the time foo(...) gets called in the main function,

More information

CS 61B Discussion 4: Inheritance Fall 2018

CS 61B Discussion 4: Inheritance Fall 2018 CS 61B Discussion 4: Inheritance Fall 2018 1 Creating Cats Given the Animal class, fill in the definition of the Cat class so that it makes a "Meow!" noise when greet() is called. Assume this noise is

More information

Rules and syntax for inheritance. The boring stuff

Rules and syntax for inheritance. The boring stuff Rules and syntax for inheritance The boring stuff The compiler adds a call to super() Unless you explicitly call the constructor of the superclass, using super(), the compiler will add such a call for

More information

Project 1 Computer Science 2334 Spring 2016 This project is individual work. Each student must complete this assignment independently.

Project 1 Computer Science 2334 Spring 2016 This project is individual work. Each student must complete this assignment independently. Project 1 Computer Science 2334 Spring 2016 This project is individual work. Each student must complete this assignment independently. User Request: Create a simple movie data system. Milestones: 1. Use

More information

This chapter is intended to take you through the basic steps of using the Visual Basic

This chapter is intended to take you through the basic steps of using the Visual Basic CHAPTER 1 The Basics This chapter is intended to take you through the basic steps of using the Visual Basic Editor window and writing a simple piece of VBA code. It will show you how to use the Visual

More information

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

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture 10 Reference and Pointer Welcome to module 7 of programming in

More information

(Refer Slide Time: 00:23)

(Refer Slide Time: 00:23) In this session, we will learn about one more fundamental data type in C. So, far we have seen ints and floats. Ints are supposed to represent integers and floats are supposed to represent real numbers.

More information

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

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved. Assoc. Prof. Dr. Marenglen Biba Laboratory Session: Exercises on classes Analogy to help you understand classes and their contents. Suppose you want to drive a car and make it go faster by pressing down

More information

Super-Classes and sub-classes

Super-Classes and sub-classes Super-Classes and sub-classes Subclasses. Overriding Methods Subclass Constructors Inheritance Hierarchies Polymorphism Casting 1 Subclasses: Often you want to write a class that is a special case of an

More information

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

And Even More and More C++ Fundamentals of Computer Science And Even More and More C++ Fundamentals of Computer Science Outline C++ Classes Special Members Friendship Classes are an expanded version of data structures (structs) Like structs, the hold data members

More information

Inheritance & Polymorphism

Inheritance & Polymorphism Inheritance & Polymorphism Procedural vs. object oriented Designing for Inheritance Test your Design Inheritance syntax **Practical ** Polymorphism Overloading methods Our First Example There will be shapes

More information