Data types. CISC 1600/1610 Computer Science I. Class dog. Introducing: classes. Class syntax declaration. Class syntax function definitions 12/2/2015

Similar documents
Data types. CISC 1600/1610 Computer Science I. Class dog. Introducing: classes. Class syntax declaration. Class syntax function definitions 4/19/2015

Encapsulation. Mason Vail Boise State University Computer Science

public class Account { private int id; private static int nextaccountid = 0; private String name; private double balance;

Data types. CISC 1600/1610 Computer Science I. Array syntax. Memory allocation. Zero-indexing 4/4/2016. Arrays

Object oriented programming

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

CSE115 Introduction to Computer Science I Coding Exercise #7 Retrospective Fall 2017

a data type is Types

CLASSES AND OBJECT CHAPTER 04 CLASS XII

C++ Review. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Lecture 7. Log into Linux New documents posted to course webpage

Exercises 1. class member { int membernum = 25; float memberpay; public void Input(cin >> membernum >> memberpay); void Output; }

Dot and Scope Resolution Operator

6.096 Introduction to C++

Overview CSE 142. Naming Revisited Declarations. Defining Parts of Objects

Introduction to Inheritance

Problem Solving With C++ Ninth Edition

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

CS 1302 Chapter 9 (Review) Object & Classes

ECE 122. Engineering Problem Solving with Java

Anatomy of a Class Encapsulation Anatomy of a Method

Storing Data in Objects

Computer Programming

Purpose of Review. Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

What is an algorithm?

Constants, References

What is an algorithm?

CSE 143. "Class Relationships" Class Relationships and Inheritance. A Different Relationship. Has-a vs. Is-a. Hierarchies of Organization

Structured Data. CIS 15 : Spring 2007

Using the Xcode Debugger

Programming Abstractions

CMSC 202 Midterm Exam 1 Fall 2015

1st Semester MTCE 601A COMPUTER SYSTEM SOFTWARE

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

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Imperative Languages!

Inheritance in Ruby. You are familiar with the idea of inheritance and how to use this in programming.

COMS W3101 Programming Language: C++ (Fall 2016) Ramana Isukapalli

Software Design and Analysis for Engineers

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

Question 1 Consider the following structure used to keep employee records:

CMSC 341 Lecture 7 Lists

Lab 12 Object Oriented Programming Dr. John Abraham

Exercise: Inventing Language

Chapter 6 Structures and Classes. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

Classes and Methods: Classes

Understanding class definitions

Abstract Data Types. Different Views of Data:

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations.

University of Swaziland Department Of Computer Science Supplementary Examination JULY 2012

Outline CSE 142. Specification vs Implementation - Review. CSE142 Wi03 F-1. Instance Variables

CPS122 Lecture: Defining a Class

STUDENT LESSON A5 Designing and Using Classes

More Shared Memory Programming

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Defining Classes. Chap 1 introduced many concepts informally, in this chapter we will be more formal in defining

Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING

Abstract Data Types (ADT) and C++ Classes

Chapter 10 Defining Classes

CSCI 1370 APRIL 26, 2017

Linear execution of statements. CISC 1600/1610 Computer Science I. Alternatives to linear execution. Linear execution of statements

Handout 8 Classes and Objects Continued: Static Variables and Constants.

Fast Introduction to Object Oriented Programming and C++

Classes, Objects, and OOP in Java. June 16, 2017

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

Anatomy of a Method. HW3 is due Today. September 15, Midterm 1. Quick review of last lecture. Encapsulation. Encapsulation

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

Object Oriented Design

A brief intro to pointers for the purposes of passing reference parameters

Practice problem on defining and using Class types. Part 4.

Defining Classes and Methods

Classes and Objects. CGS 3416 Spring 2018

CS31: Introduction to Computer Science I Spring 2011

Short Notes of CS201

COMP 2355 Introduction to Systems Programming

Object-Oriented Programming (OOP) Basics. CSCI 161 Introduction to Programming I

C++ Classes & Object Oriented Programming

CS201 - Introduction to Programming Glossary By

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

Ticket Machine Project(s)

22316 Course Title : Object Oriented Programming using C++ Max. Marks : 70 Time: 3 Hrs.

Introduc)on to Classes and Objects. Dr. Bowen Hui Computer Science University of Bri)sh Columbia Okanagan

class declaration Designing Classes part 2 Getting to know classes so far Review Next: 3/18/13 Driver classes:

C C C C++ 2 ( ) C C++ 4 C C

Introduction to Classes

CS1020 Data Structures and Algorithms I Lecture Note #8. Exceptions Handling exceptional events

PIC 10A. Final Review: Part I

Object-Oriented Programming

Topics. Constructor. Constructors

Today s Agenda. Quick Review

void Add() { cin >> trainnumber; gets(trainname); } void display() { cout<<trainnumber <<":"<<TrainName<<end;

COMP-202 Unit 8: Defining Your Own Classes. CONTENTS: Class Definitions Attributes Methods and Constructors Access Modifiers and Encapsulation

Classes. C++ Object Oriented Programming Pei-yih Ting NTOU CS

CSCS 261 Programming Concepts Exam 2 Fall EXAM 2 VERSION 1 Fall Points. Absolutely no electronic devices may be used during this exam.

C++ : Object Oriented Features. What makes C++ Object Oriented

EECS 211 Lab 6. Classes and Abstractation Winter Getting the code. Encapsulation and Abstraction. Classes

Encapsulation in C++

Vectors. CIS 15 : Spring 2007

G52CPP C++ Programming Lecture 9

Transcription:

CISC 1600/1610 Computer Science I Classes Professor Daniel Leeds dleeds@fordham.edu JMH 328A Data types Single pieces of information one integer int one symbol char one truth value bool Multiple pieces of information group of symbols string group of anything array group of multiple things struct, class 2 Introducing: classes Class dog A class defines a new data type Each instance of a class is an object Each object can contain Actions to perform (functions) Information about the object (variables) Example information Size Weight Location Example actions Eat Walk Bark 3 4 class Dog ; Class syntax declaration void Bark(); void Eat(float foodquantity); 5 Class syntax function definitions void Dog::Bark() cout << "Woof woof!\n"; void Dog::Eat(float foodquantity) weight+=foodquantity/2; size+=foodquantity/10; 6 1

Class syntax create and use an object Dog fido; fido.weight=40.5; fido.size=10; fido.eat(20); cout << fido.weight << " " << fido.size << endl; 7 class Dog ; Typical program layout Dog fluffy; fluffy.bark(); void Dog::Bark() Declaration Usage Definitions 8 The Dot Operator. Used for functions and data of individual objects fido.bark() The Scope Resolution Operator :: Used for functions of a class Dog::Bark() Note: a function inside a class is called a member function 9 Multiple instances of a class Dog fido, spot; fido.weight=40.5; fido.size=10; spot.weight=30; spot.size=7.5; fido.eat(20); fido.eat(2); cout << fido.weight << " " << spot.weight << endl; 10 Time to walk the dog Dog rufus; rufus.weight=35; rufus.size=7.2; rufus.location=5; rufus.walk(3.4); cout << "New location for Rufus: " << rufus.location << endl; // Will output location 8.4 11 Exercises Write the Walk function Modify the Walk function so the dog loses 0.2 pounds for every foot he walks Let s say dogs hide a bone at each location where they have stopped. Add an array hiddenbones that records each location where the dog object has hidden bones and modify Walk again to leave a record in hiddenbones 12 2

Class syntax declaration public vs. private class Dog Member variables void Bark(); void Eat(float foodquantity); ; Member functions 13 any function can see and use private: only visible to member functions Good style: make all member variables private use public functions to access and mutate variables 14 Class declaration, take 2 class Cat void set(float inweight, float insize, float inloc); float getsize(); float getweight(); float getlocation(); private: ; 15 Two types of functions Mutator change internal values void set(float inweight, float insize, float inloc); Accessor access internal values, no changing float getsize(); float getweight(); float getlocation(); 16 Function definitions take 2 void Cat::set(float inweight, float insize, float inloc) weight=inweight; size=insize; location=inloc; 17 Class usage take 2 Cat feline1; feline1.set(5.5,20.1,2); cout << feline1.location; // Error cout << feline1.getlocation() << endl; 18 3

Bank account Variables Name Current balance History of cash in (and out) Functions Deposit: Add entry to history Update balance class Account void open(string inname); void deposit(float money); float getbalance(); private: string name; float balance; ; 19 22 void Account::open(string inname) name=inname; balance=0; void Account::deposit(float money) if(money>=0) balance = balance+money; else cout << "Error! " << "Negative deposit!\n"; 23 What does this do? Account acc1; acc1.open("tina"); cout << acc1.getbalance() << endl; acc1.deposit(250); acc1.deposit(20.25); cout << acc1.getbalance() << endl; 24 Withdrawal function? How can we write withdraw function to reduce the money in our account? How can we prevent over-drawing? Member variables name Balance Member functions open deposit getbalance withdraw Account review New accessor function string getname() 25 26 4

Declaring/initializing Constructor functions We can declare and then initialize a variable int a; a=1; Account acc1; acc1.open("tiana"); Or we can declare and initialize together int b=1; Account...? Declaring and initializing object simultaneously Account acc1("tiana",200.20); Constructor function(s) defined to initialize object 27 28 Constructor definition Constructor has same name as class Constructor has no return type Account::Account(string inname, float indollars) name=inname; balance=indollars; 29 5