Object-Oriented Design AND CLASS PROPERTIES

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Advertisements

Road Map Introduction to object oriented programming. Classes
C++ Classes in Depth. Topics Designing Your Own Classes Attributes and Behaviors Writing Classes in C++ Creating and Using Objects.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
Constructors A constructor is a method that creates an object in Java. It does not return anything and it is not void. It’s only purpose is to create an.
Agenda Object Oriented Programming Reading: Chapter 14.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
11 Introduction to Object Oriented Programming (Continued) Cats.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and 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?
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
1 Introduction to Object Oriented Programming Chapter 10.
Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default.
Comp1004: Building Better Objects II Encapsulation and Constructors.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Programming Abstractions
Andrew(amwallis) Classes!
Objects as a programming concept
Classes (Part 1) Lecture 3
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?
Static data members Constructors and Destructors
Review: Two Programming Paradigms
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?
CS1201: Programming Language 2
Creating Your OwnClasses
Lecture 14 Writing Classes part 2 Richard Gesick.
Advanced Programming in Java
CS 302 Week 11 Jim Williams, PhD.
Advanced Programming in Java
Lecture 11 C Parameters Richard Gesick.
Corresponds with Chapter 7
Encapsulation and Constructors
Chapter 9 Objects and Classes
Today’s topics UML Diagramming review of terms
Classes.
Namespaces, Scopes, Access privileges
Introduction to Objects & Classes
Object-Oriented Programming
Object-Oriented Programming and class Design
CS1201: Programming Language 2
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
Object-Oriented Programming and class Design
CPS120: Introduction to Computer Science
OO Programming Concepts
Object-Oriented Programming and class Design
Object-Oriented Design AND CLASS PROPERTIES
Visibilities and Static-ness
Parameters, Overloading Methods, and Random Garbage
Object-Oriented Programming and class Design
Object-Oriented Programming and class Design
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Object-Oriented Design AND CLASS PROPERTIES
Chapter 7 Objects and Classes
CSG2H3 Object Oriented Programming
Object-Oriented Programming and class Design
Presentation transcript:

Object-Oriented Design AND CLASS PROPERTIES Ps Module 6 – Part II Object-Oriented Design AND CLASS PROPERTIES 12/18/2019 CSE 1321 Module 6

Topics Default Constructors Multiple Constructors (overloading) Constructor “chaining” Control of Properties “Getters” or “Accessors” “Setters” or “Modifiers” 12/18/2019 CSE 1321 Module 6

Default Constructors In many languages, there’s a default constructor If you don’t create one, it’s created for you AND IT’S INVISIBLE! AND It takes no parameters AND It sets variables/attributes: To zero (0) for numbers To FALSE for booleans To NULL (empty) for objects like strings 12/18/2019 CSE 1321 Module 4

What it would look like (if you could see it) CLASS Dog BEGIN CREATE rabid CREATE weight CREATE name // It’s INVISIBLE! CONSTRUCTOR Dog ( ) rabid ← false weight ← 0 name ← NULL END CONSTRUCTOR END CLASS Ps

Multiple Constructors (a.k.a. overloading) We can have more than one constructor Remember, constructors are used to initialize our objects We can use parameters with the constructor to customize the initialization Sometimes we have more or less data to use in the customization We’d like to be able to pass in only what we know 12/18/2019 CSE 1321 Module 6 5

Implementation Overloading the constructor involves using the same method/function name Vary the number of parameters; AND/OR Vary the type of parameters 12/18/2019 CSE 1321 Module 6 6

Another Example: Class Circle 12/18/2019 7 CSE 1321 Module 6

Class Circle Pseudocode BEGIN CREATE Radius ← 1.0 CONSTRUCTOR Circle //called default constructor BEGIN END CONSTRUCTOR CONSTRUCTOR Circle (NewRadius) //called constructor Radius ← NewRadius END CONSTRUCTOR METHOD getArea () //compute and return circle area RETURN (Radius * Radius * 3.14159) END METHOD END CLASS Ps 4/26/2018 CSE 1321 Module 6 5

Class Circle public class Circle { double radius = 1.0; Circle() //default constructor { } Circle(double newRadius) //another constructor { radius = newRadius; } public double getArea() //calculates area { return (radius*radius*3.14159); } } 4/26/2018 CSE 1321 Module 6 6

// Header class Circle { public: double radius; Circle(); Circle(double newRadius); double getArea(); }; // The implementation file – Circle.cpp Circle::Circle() { radius = 1.0; } Circle::Circle(double newRadius) { radius = newRadius; double Circle::getArea() { return (radius*radius*3.14159); 12/18/2019 CSE 1321 Module 4

Ps Driver in Pseudocode CLASS TestCircle BEGIN CREATE circle1, circle2 AS Circle // create two variables circle1 ← NEW circle() // create circle1 object circle2 ← NEW circle(25.0) // create circle2 object PRINT ("Circle 1 area = " + circle1.getArea()) PRINT ("Circle 2 area = " + circle2.getArea()) END TestCircle Outputs: Circle 1 area = 3.14159 Circle 2 area = 1963.4937499999999 Ps 4/26/2018 CSE 1321 Module 6 5

Driver in Java class Test_Circle { public static void main (String args[]) { Circle c1 = new Circle(); System.out.println("The area of the c1 is " + c1.getArea()); Circle c2 = new Circle(25); System.out.println("The area of c2 is " + c2.getArea()); } } 4/26/2018 CSE 1321 Module 6 6

Driver in C# class Test_Circle { public static void Main (string args[]) { Circle c1 = new Circle(); Console.WriteLine("The area of the c1 is " + c1.getArea()); Circle c2 = new Circle(25); Console.WriteLine("The area of c2 is " + c2.getArea()); } } 4/26/2018 CSE 1321 Module 6 6

Driver in C++ int main() { Circle c1; cout << "The area of c1 is " << c1.getArea() << endl; // The advanced "pointer" way Circle* c2; c2 = new Circle(5); cout << "The area of c2 is " << c2->getArea() << endl; } 12/18/2019 CSE 1321 Module 4

Visibility of Class Content Problem: Imagine class “Person” with an “age” attribute Then, a “bad guy” directly sets the age to -3! We need protection/security We need a way of selectively “publishing” parts of a class and “hiding” other parts of the class Use public & private keywords 12/18/2019 CSE 1321 Module 6 15

Sloppy Definitions public: anyone (or anything) can see it from anywhere! That is, anything outside the class. private: can only be seen/called inside the class. Note, variables, methods and even classes can be marked either public or private 12/18/2019 CSE 1321 Module 4

Implications of public and private (this is a bit hard to understand…) 12/18/2019 CSE 1321 Module 6 17

General Rules Variables/attributes are normally marked private public variables can be tampered with and abused! Methods are most often marked public However, some methods should be marked as private 12/18/2019 CSE 1321 Module 4

Wait! How do we change private variables? Let’s review, shall we? 12/18/2019 CSE 1321 Module 4

Public Doggies (with private lives) PUBLIC CLASS Dog BEGIN PRIVATE CREATE rabid // change this? PRIVATE CREATE weight // and this? PUBLIC CREATE name // Boring constructor PUBLIC CONSTRUCTOR Dog ( ) rabid ← false weight ← 0 name ← NULL END CONSTRUCTOR END CLASS Ps

Main idea Use methods to access and change the variables/attributes. Instead of direct access 12/18/2019 CSE 1321 Module 6 21

Accessors/Modifiers These are public methods that enable change to attributes Also called “getters” and “setters” Why is this cool? Can put in protections to prevent bad data PUBLIC setAge (parameter: newAge) BEGIN IF (newAge < 110) THEN age = newAge ELSE PRINT (“That’s too old!”) ENDIF END 12/18/2019 CSE 1321 Module 4

Example class Dog { private int weight; private boolean isRabid; public String name; public Dog(int w, String n) { weight = w; name = n; isRabid = false; } public void setWeight (int w) { if (w > 0) {weight = w;} public void setRabid (String n) {name = n;} public int getWeight () {return weight;} public boolean getRabid() {return isRabid;} } 4/26/2018 CSE 1321 Module 6 6

Example class Dog { private int weight; private bool isRabid; public string name; public Dog(int w, string n) { weight = w; name = n; isRabid = false; } public int Weight { // Access Weight, not weight get {return weight;} set {weight = value;} public bool IsRabid { // IsRabid, not isRabid get {return isRabid;} set {isRabid = value;} } 4/26/2018 CSE 1321 Module 6 7

Example class Dog { private: bool isRabid; float weight; public: string name; Dog(float w, string n); bool getRabid(); float getWeight(); void setRabid (bool r); void setWeight (float w); }; Dog::Dog(float w, string n) { isRabid = false; weight = w; name = n; } bool Dog::getRabid() {return isRabid;} float Dog::getWeight() {return weight;} void Dog::setRabid(bool r) {isRabid = r;} void Dog::setWeight(float w) {weight = w;} 12/18/2019 CSE 1321 Module 4

A common point of confusion We see a lot of keywords like this and self What is that? A reference to something inside the class we’re coding in Commonly used to resolve ambiguity of variables: 12/18/2019 CSE 1321 Module 4

A point of confusion Ps PUBLIC CLASS Dog BEGIN PRIVATE CREATE rabid PRIVATE CREATE weight PUBLIC CREATE name PUBLIC CONSTRUCTOR Dog (parameter: rabid, weight, name) rabid ← rabid // what? weight ← weight // huh? name ← name // wuh? END CONSTRUCTOR END CLASS 12/18/2019 CSE 1321 Module 4

Better Ps PUBLIC CLASS Dog BEGIN PRIVATE CREATE rabid PRIVATE CREATE weight PUBLIC CREATE name PUBLIC CONSTRUCTOR Dog (parameter: rabid, weight, name) this.rabid ← rabid // assign attributes this.weight ← weight // the parameters this.name ← name END CONSTRUCTOR END CLASS // Note – in C++, you’ll use the arrow (e.g. this->name) 12/18/2019 CSE 1321 Module 4

Last thing: Constructor Chaining It’s possible for one constructor to leverages/calls another You can do this as many times as you want Let’s see an example 12/18/2019 CSE 1321 Module 4

PUBLIC CLASS Dog BEGIN PRIVATE CREATE rabid PRIVATE CREATE weight PUBLIC CREATE name PUBLIC CONSTRUCTOR Dog ( ) // Default constructor // Call the constructor below this (FALSE, 4, “Fluffy”) END CONSTRUCTOR PUBLIC CONSTRUCTOR Dog (parameter: rabid, weight, name) this.rabid ← rabid this.weight ← weight this.name ← name END CLASS Ps 12/18/2019 CSE 1321 Module 4

Summary A class is made of variables and methods. Variables represent the state of the class objects; Variables should (almost) always be declared private Method provide controlled access to variables Methods intended for class client should be public. Other methods for local use should be private. Controlled access to the variables promotes encapsulation. 12/18/2019 CSE 1321 Module 6