CSIS 123A Lecture 1 Intro To Classes Glenn Stevenson CSIS 113A MSJC.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Wednesday, 10/2/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/2/02  QUESTIONS (on HW02 – due at 5 pm)??  Today:  Review of parameters  Introduction.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Classes and Objects Systems Programming.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
Classes and Objects April 6, Object Oriented Programming Creating functions helps to make code that we can reuse. When programs get large it becomes.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Structures  2 nd aggregate data type: struct  Recall:
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Procedural and Object-Oriented Programming 13.1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Classes Definition A class is a data type whose variables are objects Object – a variable that has member functions as well the ability to hold.
Structures and Classes Version 1.0. Topics Structures Classes Writing Structures & Classes Member Functions Class Diagrams.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
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?
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Lecture 19: Introduction to Classes Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
ENEE150 – 0102 ANDREW GOFFIN Abstract Data Types.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
100 學年度碩士班新生暑期課程 程式設計 Object-Oriented Programming: Part I The Basics of Classes.
Introduction to Classes and Objects CS-2303, C-Term C++ Program Structure Typical C++ Programs consist of:– main –A function main –One or more classes.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
CMSC202 Computer Science II for Majors Lecture 06 – Classes and Objects Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Structures and Classes
Procedural and Object-Oriented Programming
Classes C++ representation of an object
What header file contains C++ file I/O instructions?
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?
Abstract Data Types Programmer-created data types that specify
10.2 Classes Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
COMP 51 Week Twelve Classes.
Chapter 3: Using Methods, Classes, and Objects
About the Presentations
Classes & Objects.
Introduction to Classes
Chapter 7: Introduction to Classes and Objects
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
Introduction to Classes
Classes and Objects Encapsulation
Classes and Data Abstraction
Classes and Data Abstraction
Chapter 9 Objects and Classes
Learning Objectives Classes Constructors Principles of OOP
Introduction to Classes and Objects
Defining Classes and Methods
CMSC202 Computer Science II for Majors Lecture 07 – Classes and Objects (Continued) Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Defining Classes and Methods
Defining Classes and Methods
Classes C++ representation of an object
CMSC 202 Lesson 8 Classes II.
Lecture 8 Object Oriented Programming (OOP)
Classes and Objects Systems Programming.
Introduction to Classes and Objects
Presentation transcript:

CSIS 123A Lecture 1 Intro To Classes Glenn Stevenson CSIS 113A MSJC

Class Syntax Glenn Stevenson CSIS 113A MSJC

Class Definitions  Defined similar to structures  Example: class DayOfYear  name of new class type { public: void output();  member function! int month; int day; };  Notice only member function’s prototype  Function’s implementation is elsewhere Glenn Stevenson CSIS 113A MSJC

Classes are Blueprints Classes are blue prints for objects –Class is a definition –Instance of class is an Object –Class definition is typically put in a header file! Glenn Stevenson CSIS 113A MSJC

Declaring Objects  Declared same as all variables  Predefined types, structure types  Example: DayOfYear today, birthday;  Declares two objects of class type DayOfYear  Objects include:  Data  Members month, day  Operations (member functions)  output() Glenn Stevenson CSIS 113A MSJC

Member Access Like Structures  Members accessed same as structures  Example: today.month today.day  And to access member function: today.output();  Invokes member function Glenn Stevenson CSIS 113A MSJC

Class Member Functions  Must define or ‘implement’ class member functions  Like other function definitions  Can be after main() definition  Must specify class: void DayOfYear::output() {…}  :: is scope resolution operator  Instructs compiler ‘what class’ member is from  Item before :: called type qualifier Glenn Stevenson CSIS 113A MSJC

Class Member Functions Definition  Notice output() member function’s definition  Refers to member data of class  No qualifiers  Function used for all objects of the class  Will refer to ‘that object’s’ data when invoked  Example: today.output();  Displays ‘today’ object’s data Glenn Stevenson CSIS 113A MSJC

Rectangle Example Glenn Stevenson CSIS 113A MSJC // Rectangle.h class Rectangle { private: int length, width; public: void set_values (int Length, int Width); int area () ; };

Rectangle.cpp Glenn Stevenson CSIS 113A MSJC // Rectangle.cpp #include #include "Rectangle.h" void Rectangle::set_values(int Length, int Width) { length = Length; width = Width; } int Rectangle::area() { return length * width; }

Dot and Scope Resolution Operator  Used to specify ‘of what thing’ they are members  Dot operator:  Specifies member of particular object  Scope resolution operator:  Specifies what class the function definition comes from Glenn Stevenson CSIS 113A MSJC

A Class’s Place  Class is full-fledged type!  Just like data types int, double, etc.  Can have variables of a class type  We simply call them ‘objects’  Can have parameters of a class type  Pass-by-value  Pass-by-reference  Can use class type like any other type! Glenn Stevenson CSIS 113A MSJC

What’s this private, public stuff?

private Data That you want to be private gets defined after the colon of the private statement. –Nothing outside of the class can access it Let’s look at an example: Normally put variables and utility functions that make the class operate. –If a person doesn’t need access then don’t give it to them Glenn Stevenson CSIS 113A MSJC

class Rectangle { private: int length, width; public: void set_values (int Length, int Width); int area () ; }; void Rectangle::set_values(int Length, int Width) { length = Length; width = Width; } int Rectangle::area() { return length * width; // area is part of class so it has access } int main() { Rectanngle r; r.width = 10; // Can’t do this width is private }

Public interface Private variables should not be allowed to be modified directly. –Public interface to private data Allows you to control how data is stored –Accessor and mutator methods used for this Accessor gets private information Mutator modifies private information Glenn Stevenson CSIS 113A MSJC

Accessor and Mutator Functions  Object needs to ‘do something’ with it’s data  Call accessor member functions  Allow object to read data  Also called ‘get member functions’  Simple retrieval of member data  Mutator member functions  Allow object to change data  Manipulated based on application Glenn Stevenson CSIS 113A MSJC

Encapsulation  Any data type includes  Data (range of data)  Operations (that can be performed on data)  Example: int data type has: Data: +-32,767 Operations: +,-,*,/,%,logical,etc.  Same with classes  But WE specify data, and the operations to be allowed on our data! Glenn Stevenson CSIS 113A MSJC

Abstract Data Types  ‘Abstract’  Programmers don’t know details  Abbreviated ‘ADT’  Collection of data values together with set of basic operations defined for the values  ADT’s often ‘language-independent’  We implement ADT’s in C++ with classes  C++ class ‘defines’ the ADT  Other languages implement ADT’s as well Glenn Stevenson CSIS 113A MSJC

More Encapsulation  Encapsulation  Means ‘bringing together as one’  Declare a class  get an object  Object is ‘encapsulation’ of  Data values  Operations on the data (member functions) Glenn Stevenson CSIS 113A MSJC

Principles of OOP  Information Hiding  Details of how operations work not known to ‘user’ of class  Data Abstraction  Details of how data is manipulated within ADT/class not known to user  Encapsulation  Bring together data and operations, but keep ‘details’ hidden Glenn Stevenson CSIS 113A MSJC

Thinking Objects  Focus for programming changes  Before  algorithms center stage  OOP  data is focus  Algorithms still exist  They simply focus on their data  Are ‘made’ to ‘fit’ the data  Designing software solution  Define variety of objects and how they interact Glenn Stevenson CSIS 113A MSJC