COMS 261 Computer Science I

Slides:



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

ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 10: Continuing with classes Constructors, using classes.
C++ Classes & Data Abstraction
Lecture 18: 4/11/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Introduction to C++. Overview C++? What are references Object orientation Classes Access specifiers Constructor/destructor Interface-implementation separation.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Abstract Data Types Using Classes Lecture-5. Abstract Data Types Using Classes Representing abstract data types using C++ We need the following C++ keywords.
1 COMS 261 Computer Science I Title: Classes Date: November 7, 2005 Lecture Number: 28.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Constructors and Encapsulation reading: self-checks: #10-17.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 24P. 1Winter Quarter C++ Lecture 24.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Introduction To Classes Chapter Procedural And Object Oriented Programming Procedural programming focuses on the process/actions that occur in a.
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.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Chapter 10 Introduction to Classes
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 02] Introduction to.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
The Class Construct Defining objects with attributes and behavior JPC and JWD © 2002 McGraw-Hill, Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
1 CSC241: Object Oriented Programming Lecture No 02.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 9: Continuing with classes.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
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?
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
1 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight – don’t be late! Notes and clarifications for Project.
1 COMS 261 Computer Science I Title: Classes Date: November 9, 2005 Lecture Number: 29.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
1 COMS 261 Computer Science I Title: Classes Date: November 4, 2005 Lecture Number: 27.
1 Review for Midterm 2 Aaron Bloomfield CS 101-E.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
11/07/11Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 8 An Introduction.
6/24/2016Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 8 An Introduction.
CSIS 123A Lecture 1 Intro To Classes Glenn Stevenson CSIS 113A MSJC.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Procedural and Object-Oriented Programming
Objects as a programming concept
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
A First C++ Class – a Circle
Defining objects with attributes and behavior
Chapter 3: Using Methods, Classes, and Objects
Introduction to 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?
Introduction to Classes
Corresponds with Chapter 7
Implementing Classes Chapter 3.
Defining Classes and Methods
PreAP Computer Science Quiz Key
CMSC202 Computer Science II for Majors Lecture 07 – Classes and Objects (Continued) Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
NAME 436.
Dr. R Z Khan Handout-3 Classes
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
C++ data types.
Lecture 8 Object Oriented Programming (OOP)
Classes and Objects Systems Programming.
(4 – 2) Introduction to Classes in C++
Presentation transcript:

COMS 261 Computer Science I Title: Classes Date: October 28, 2005 Lecture Number: 24

Announcements Project 2 Due 11/2/05 Exam 2 11/02/05

Review I/O manipulators

Outline Classes

Class Types Class construct Allows programmers to define new data types for representing information Class type objects can have both attribute components and behavior components Data members Member functions Provides the object-oriented programming in C++

Terminology Client Object behaviors Object attributes Program using a class Object behaviors Realized in C++ via member functions (methods) Object attributes Are known as data members in C++

Member Functions Provide an interface to data members, object access, and manipulation Create objects of the class Inspect, mutate, and manipulate object of the class Can be used to keep data members in a correct state setSize() setColor() draw()

Member Functions Constructors Inspectors or Accessors Member functions that initialize an object during its definition Inspectors or Accessors Member functions that act as a messenger that returns the value of an attribute Mutators or Accessors Changes the value of an attribute

Member Functions Facilitators Causes an object to perform some action or service

Vector Class Definition of a class to represent a 2D vector, (x, y) Definition, not an implementation!! Class definitions are stored in header files vec.h Contain Constructor Accessors Facilitators

Vec Class Definition Defines the characteristics of an object Defines the data members Defines the member functions public interface Sort of a mold Does not create an object Instantiation

Point Class Definition class VEC { Allows public access to what follows public: Allows access by members of the vec class to what follows private: };

Public access public Declares public members of the class Typically Member function declarations Interface of the class Allows access to the object’s public members (interface) outside the class

Private access private Declares private members of the class Typically Data members Some members function Allows access to an object’s private members inside the class Inside member functions

Access Specifiers Public access Private access All clients and class members have access to the public members Private access Only class members have access to the private members

vec Class Definition class VEC { public: VEC(); // default constructor (no arguments) private: };

Default constructor No parameter constructor VEC myVec; Function overloading allows us to define more than one constructor Only one is the default constructor Other constructors must have a different number and/or type parameters Used when creating an object of the class when no parameters are specified VEC myVec;

VEC Class Definition: goes in vec.h class VEC { public: VEC(); // default constructor (no arguments) private: float x; // x and y components of the vector float y; };

Default constructor Implementation of member functions are put in the vec.cpp file We say vec.h: definition file vec.cpp: implementation file Standard include statements Also need to include “vec.h” The definition of the class

Default constructor Implementation in the VEC.cpp file Member functions prefaced with the scope resolution operator VEC::VEC() { x = 0.0f; y = 0.0f; }

Application Now we have enough to write an application program that creates an object of the VEC class Instantiate a VEC object VEC v1; // defines an object of type VEC Of course the class is rather limited We can add some functionality Print member function

Implementation Member functions prefaced with the scope resolution operator void VEC::print () { cout << “X: “ << x << “ “; cout << “Y: “ << y << endl; }

Application Objects of the VEC class can now call the member function print VEC v1; v1.print(); Run CodeWarrior

Accessors Applications read and write the an objects data (private members) through accessor functions Read accessors getBlah(); Write accessors setBlah(val);

VEC Class Definition: goes in vec.h class VEC { public: VEC(); }; public: VEC(); // default constructor (no arguments) void print(); // output values in object float getX(); void setX(float val); private: float x; // x and y components of the vector float y;

Accessor Implementation float VEC::getX (){ return x; } void VEC::setX (float val) { x = val; Run CodeWarrior

VEC Class It may be useful to add a constructor that takes two parameters An initial value for x and y Allows us to construct VEC objects as: VEC v1(1.2, 3.4); Called and initial value constructor

VEC(float xval, float yval); VEC Class Definition: goes in vec.h class VEC { }; public: VEC(); VEC(float xval, float yval); void print(); float getX(); float getY(); void setX(float val); void setY(float val); private: float x; // x and y components of the vector float y;

Implementation Definition: goes in vec.cpp VEC(float xval, float yval) { x = xval; y = yval; } Run CodeWarrior

VEC Class It may be useful to add a constructor that takes one parameter An initial value for x Allows us to construct VEC objects as: VEC v1(1.2); Also called and initial value constructor

VEC(float xval, float yval); VEC Class Definition: goes in vec.h class VEC { }; public: VEC(); VEC(float xval, float yval); VEC(float xval); void print(); float getX(); float getY(); void setX(float val); void setY(float val); private: float x; // x and y components of the vector float y;

Implementation Definition: goes in vec.cpp VEC(float xval) { x = xval; y = 0.0; } Run CodeWarrior

VEC Class It may be more useful to add a constructor that takes one parameter and optionally takes another parameter Default parameter An initial value for x Optional parameter for y Allows us to construct VEC objects as: VEC v1(1.2); Also called and initial value constructor

VEC Class Definition: goes in vec.h class VEC { public: }; public: VEC(); VEC(float xval, float yval); VEC(float xval, float yval = 0.0f); void print(); float getX(); float getY(); void setX(float val); void setY(float val); private: float x; // x and y components of the vector float y;

Implementation Definition: goes in vec.cpp VEC(float xval, float yval) { x = xval; y = yval; } Run CodeWarrior