Agenda Object Oriented Programming Reading: Chapter 14.

Slides:



Advertisements
Similar presentations
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Advertisements

CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Inheritance Inheritance Reserved word protected Reserved word super
Road Map Introduction to object oriented programming. Classes
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
Enhancing classes Visibility modifiers and encapsulation revisited
Terms and Rules Professor Evan Korth New York University (All rights reserved)
CS31: Introduction to Computer Science I Discussion 1A 5/28/2010 Sungwon Yang
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Classes in C++ Bryce Boe 2012/08/15 CS32, Summer 2012 B.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Java Unit 9: Arrays Declaring and Processing Arrays.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Neal Stublen Key Concepts  Classes  Objects  Inheritance  Polymorphism  Encapsulation.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
CS 106 Introduction to Computer Science I 03 / 19 / 2007 Instructor: Michael Eckmann.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Classes CS 21a: Introduction to Computing I First Semester,
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
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.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Applications Development
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.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Side effects A side effect is anything that happens in a method other than computing and/or returning a value. Example: public class hello { public int.
Welcome to AP Computer Science A We use the Java language, but this class is much more rigorous than Intro to Java More programming, but also more theory.
Classes, Interfaces and Packages
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
Classes - Intermediate
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Classes (Part 1) Lecture 3
A basic tutorial for fundamental concepts
Yanal Alahmad Java Workshop Yanal Alahmad
Agenda Warmup AP Exam Review: Litvin A2
Chapter 3: Using Methods, Classes, and Objects
Object-Oriented Programming & Design Lecture 14 Martin van Bommel
Wrapper Classes ints, doubles, and chars are known as primitive types, or built-in types. There are no methods associated with these types of variables.
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Static and non-Static Chapter 5.
Lesson 2: Building Blocks of Programming
Defining Classes and Methods
Classes, Encapsulation, Methods and Constructors (Continued)
Chapter 4 Writing Classes.
CMSC202 Computer Science II for Majors Lecture 07 – Classes and Objects (Continued) Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Classes CS 21a: Introduction to Computing I
CS 240 – Advanced Programming Concepts
Presentation transcript:

Agenda Object Oriented Programming Reading: Chapter 14

Object Oriented Programming C++ is an Object Oriented Programming language. So are Java, C#, VB, Smalltalk, and Objective-C (Mac/Iphone development). What does this mean? Now, instead of only working with simple data types like integers, doubles, characters, etc...we can make objects to represent anything we like. Do you want to keep a list of students and track their school progress? Make a Student class. Do you want to do math with X,Y coordinates? Make a Point class.

Object Oriented Programming So far we have seen the following data types: Integer (int) A whole number Double (double) A number with decimals Character (char) A single character String (string) A series of characters Boolean (bool) True or False

Object Oriented Programming There are many more types. There are also special, more abstract types, that are defined by classes or structures. C++ String An example of a class is a C++ string. A variable of data type string is really an object of type string. A class is a special data type. A variable declared with that data type is called an object of that data type.

Object Oriented Programming student Let's say you have a class called student. It may have the following properties: string firstName; string lastName; date birthDate; int id; schedule classSchedule; *date and schedule may be other classes

Classes, Objects, Structures, etc student could also have the following functions: void study (double hours); double getGrade(int courseNum); void register(int courseNum); To call any of these functions, we would declare a student variable and then type variable name dot function name, including any function parameters. student myStudent; myStudent.study(2.0);

Object Oriented Programming Classes use constructors to create the initial version of an object. For our student class, we might define a constructor (which is a function) that will set the id, first name, and last name of our student. If we had a constructor, we would use it like this: student myStudent (1,”John”, “Doe”); The code in the constructor function would assign the values passed in as parameters to their corresponding properties in the object.

Object Oriented Programming student is just an example of the many, many possibilities with classes. You can also make a student structure, which is slightly different from a class, but the same concept applies – you make your own “type” with its own properties.

Note about the book PLEASE NOTE: Your textbook is written for the standardized AP Computer Science exam. As a result, it utilizes some classes written specifically for the AP exam that are NOT available to you unless you include them. All of the code in the slides, and anything you find by looking at online documentation, will omit those classes. Please use what we use in class. For example, if you see apstring in the book, use string instead.

Object Oriented Programming Another example. We could make up a card class with the following properties and functions: Card char suit; char rank; char color; string getSuitName();

Private and Public Variables For our class, it could be useful to have some variables that code outside the class cannot see. These would be variables that we don't want to be changed manually. For example...if the suit of our card is changed, the color has to change, so it would be better to write a function to do that and not allow the programmer to change it from their code. Actually, most commonly, programmers make all class variables private and use functions to read or change their value. Variables that cannot be seen or modified from outside the class are called private variables. Variables that can be seen or modified from outside the class are called public variables.

Private and Public Functions Similarly, it may be useful to have functions that can only be used from within our class. Usually private functions are created when the function is only needed by another function in the class and really wouldn't serve a purpose to a programmer using our class. Example: we could write a function to synchronize the suit and color variables: void syncColor() { if (suit == 'D' || suit == 'H') { color = 'R'; } else { color = 'B'; }

Header Files While it is possible to approach this in many ways, it is good practice to have multiple files to separate your class code and program code. Example: PlayCards.cpp – The program you are writing that may use your class(es) Card.h – The code for the Card class definition: what private and public variables it has, the definitions of the private and public functions it has Card.cpp – The bodies (implementations) of the functions belonging to your Card class.

Constructors All classes should have at least one function that is used for initializing an object of that class. This function must have the same name as the class. It may have as few or as many parameters as you like. Note: Constructors do not return anything and should have no return type specified (not even void). Define a constructor like this: Card (); When we want to create a new object of our class, we would type (in our program's.cpp file): Card myCard(); If we wrote our constructor to accept parameters, we would include those between the parenthesis as with any function.

Constructors for our Card class Overloading Constructors You can overload a function by creating a second function with the same name that accepts different parameters. Here are two possible constructors for our card class. Card (); Card (char suit, char rank);

Class definition This is what a class definition would look like (in your.h file) class ClassName () { private: //List of private variables //List of private functions public: //Constructors //List of public functions }; Don't miss this semi-colon!

Accessor Functions - Getters and Setters Like I said, it is common practice to not make any class variables public. Instead, we write getter and setter functions for each. So our card class could have the following public functions: char getSuit(); char getRank(); void setSuit(char s); void setRank(char r);

Card Class Can you write the Card.h file? After that, can you write the Card.cpp file? After that, a program called DealCards.cpp that creates 7 card objects?