Classes Structured Programming 256 Chapter 8 Classes - Part I OOP & Class Object Terminology File Management.

Slides:



Advertisements
Similar presentations
Introduction to C++ An object-oriented language Unit - 01.
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.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
1 A pointer variable is a variable whose value is the address of a location in memory int x; x = 5; int* ptr1; ptr1 = &x; int* ptr2; ptr2 = ptr1; *ptr1.
1 Abstract Data Types Chapter 1. 2 Objectives You will be able to: 1. Say what an abstract data type is. 2. Implement a simple abstract data type in C++
Inheritance and Composition (aka containment) Textbook Chapter –
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
1 Lecture 29 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
1 Abstract Data Type (ADT) a data type whose properties (domain and operations) are specified (what) independently of any particular implementation (how)
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
Classes in C++ Bryce Boe 2012/08/15 CS32, Summer 2012 B.
1 Chapter 14-1 Object- Oriented Software Development Dale/Weems.
1 Chapter 14-2 Object- Oriented Software Development Dale/Weems.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
1 Structured Types, Data Abstraction and Classes.
1 Chapter 14 Object- Oriented Software Development Dale/Weems.
1 Chapter 14 Object-Oriented Software Development Dale/Weems/Headington.
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
CHAPTER 13 CLASSES AND DATA ABSTRACTION. In this chapter, you will:  Learn about classes  Learn about private, protected, and public members of a class.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
1 Chapter 14 Object-Oriented Software Development Dale/Weems.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
1 Chapter Structured Types, Data Abstraction and Classes Dale/Weems.
Design.ppt1 Top-down designs: 1. Define the Problem IPO 2. Identify tasks, Modularize 3. Use structure chart 4. Pseudocode for Mainline 5. Construct pseudocode.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
1 Structural Types, Data Abstractions and Classes.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
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.
1 Final Exam Tues 3/16/10 (2-3:50) (Same classroom) Old Textbook - Chapters 11-16, 18 Focus is on 15, 16 and 18 Final Exam Tues 3/16/10 (2-3:50) (Same.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
(1) ICS 313: Programming Language Theory Chapter 11: Abstract Data Types (Data Abstraction)
Chapter More on Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
1 Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch Read Style Guide (see course webpage)
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Chapter 12 Classes and Abstraction
TK1924 Program Design & Problem Solving Session 2011/2012
Classes (Part 1) Lecture 3
Dale/Weems/Headington
COMPUTER 2430 Object Oriented Programming and Data Structures I
Polymorphism.
Review: Two Programming Paradigms
About the Presentations
Chapter Structured Types, Data Abstraction and Classes
Introduction to Structured Data Types and Classes
CS212: Object Oriented Analysis and Design
CS148 Introduction to Programming II
Introduction to Classes
Classes and Data Abstraction
CPS120: Introduction to Computer Science
COP 3330 Object-oriented Programming in C++
Introduction to Classes and Objects
Chapter 14 Object-Oriented Software Development
CPS120: Introduction to Computer Science
Lecture 8 Object Oriented Programming (OOP)
Presentation transcript:

Classes Structured Programming 256 Chapter 8

Classes - Part I OOP & Class Object Terminology File Management

Object Oriented Programming  OOP  industry standard  C++ is language of choice

Object Oriented Programming With OOP and C++  well organized  easy to read to design to modify  reuse of code facilitated

Object Oriented Programming  data and their procedures as a single object Key features  Encapsulation  Data hiding  Inheritance  Polymorphism * *

Object Oriented Programming What is radius? Radius is 5. Circle Object Radius = 5 User Get radius?

Abstract Data Type ADT = a programmer defined data type whose properties (domain [values] and operations) are specified independently of any particular implementation. * It has a how and a what.

Classes A class is a programmer-defined data type. It consists of data structure and functions which operate on that data.

Example of a Class Declaration class Student { public: void PrintGrade(); void addGrade(double grade); double getGPA(); void chngAddr (char addr[ ]); void chngAddr (char addr [ ], char city [ ]); }; * private: double GPA; double GPA; string address1, address2; string address1, address2; int num_of_grades; int num_of_grades; string fullName; string fullName;

Class Declaration Syntax class Name // usually capitalized { public: public members;// usually functions private: private members;// usually variables }; Note *

A Few Terms - class - class member - class object (class instance) - client

Objects An object is an instance of a class. Similarity: int row; This creates an instance of int called “row”. *

Objects If the class is Student, the objects may be the individual student names, the student GPAs, the student courses, etc. Alfred E. Newman is an instance of the class Student. CSE100 could be an array of 130 students and would be another instance of Student. *

Objects jane.addGrade(93.4); yoko.PrintGrade(); elvira.getGPA(); * * * CSE100[47].chngAddr(“Dakoda #4”); maribell.chngAddr(“123 Gnu”, “Chili, NY”);

Class Declarations * public:  private: double GPA; string address1, address2; int num_of_grades; string fullName; cout << iago.GPA; cout << elvira. num_of_grades;

Class Declarations class Student { public:  int ID; private:  }; * cout << iago.ID; cout << elvira.ID; cout << CSE100[37].ID;

hall Objects and Members prIvate public PrintGradefunction code addGradefunction code getGPAfunction code chngAddrfunction code GPA2.81 address13 Ayn address215 Zola num_of_grades131 fullNameAl Hallsand PrintGradefunction code addGradefunction code getGPAfunction code chngAddrfunction code GPA3.45 address117 Emil address29 Gault num_of_grades93 fullNameJo Sand *

What can you do with a class? - declare as many objects as you like - pass class objects as parameters in a function and return them as function values - may be automatic

- use most built-in operataions  add two objects hall + rand  compare for equality hall == rand What you cannot do? *

What you can do? - work with individual members of a class hall.GPA + sand.GPA * hall.GPA == sand.GPA hall.GPA == sand.GPA hall.GPA = sand.GPA hall.GPA = sand.GPA hall = sand// member-by-member // assignment hall = sand// member-by-member // assignment

What you can do? - valid operators member selection. hall.getGPA member selection. hall.getGPA assignment = hall.GPA = sand.GPA assignment = hall.GPA = sand.GPA

Part of Some Client Code: Student hall; // declaration of an object of type Student Student sand; // declaration of an object of type Student double inGrade; // declaration of an object of type double hall.addGrade(84.3); cout > inGrade; sand.addGrade(inGrade); sand = hall;// member-by- member assigning sand.PrintGrade();// the grade will be 84.3 * * *

Class Scope declaration function call Student presley;presley.getGPA(); SomeClass someObjsomeObj.getGPA(); double xyz;getGPA();

Data Hiding data public class members

Files ª specification ª implementation ª client

Files client.cppimplementation.cpp #include “specification.h” specification.h void main()

Specification File class Name // usually capitalized { public: public members;// usually functions private: private members;// usually variables };

Sepcification File(s) #include “bool.h” class TimeType {public: void Set(int hours, int minutes, int seconds); void Increment(); void write() const; Boolean Equal(TimeType otherTime) Boolean LessThan(TimeType otherTime) private: int hrs; int mins; int secs; }; This is timetype.h

Implementation File(s) #include "timetype.h" #include void TimeType::Set( /* in */ int hours, /* in */ int minutes, /* in */ int seconds ) /* Precondition:Postcondition: 0 <= hours <=23 hrs == hours && 0 <= minutes <=59 && mins == minutes && 0 <= seconds <=59 && secs == seconds NOTE: This function MUST be called prior to any of the other member functions */ {hrs = hours; mins = minutes; secs = seconds; } This is timetype.cpp

Class Member Functions Syntax type Class_name::function_name(argument list) { function body } Example void TimeType::Set(int hour, int minu, int secon) { hrs = hour; mins = minu; secs = secon; } *

Client File(s) #include “timetype.h” #include void main() { TimeType appointment; appointment.Set(15, 30, 0); appointment.Write(); etc. } This is diary.cpp

Preprocessor Directives “filename” “path\filename”

Files diary.cpptimetype.cpp #include “timetype.h” timetype.h void main()

Multifile Program diary.exe linker timetype.h diary.cpptimetype.cpp diary.objtimetype.obj compiler

Multifile Program diary.exe linker timetype.h diary.cpptimetype.cpp diary.objtimetype.obj compiler datetype.obj compiler datetype.cpp

Managing Projects That Use Classes  Place class declaration into an interface header file, classname.h.  Place the implementation of the functions of this class into a separate implementation file, classname.cpp.  Place the client file(s) in separate.cpp files, each of which should have #include”classname.h”.  All client files and classname.cpp should be inserted as part of the project. * *