OOP Using Classes - I. Structures vs. Classes C-style structures –No “interface” If implementation changes, all programs using that struct must change.

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

 2003 Prentice Hall, Inc. All rights reserved. ECE 2552 Dr. S. Kozaitis Summer Summary of Topics Related to Classes Class definition Defining member.
Class and Objects.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 16: Classes and Data Abstraction Outline 16.1Introduction.
Classes: A Deeper Look Systems Programming.
Introduction to Classes and Data Abstraction
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
More C++ Classes Systems Programming. Systems Programming: C++ Classes 2 Systems Programming: 2 C++ Classes  Preprocessor Wrapper  Time Class Case Study.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
C++ Classes & Object Oriented Programming. Object Oriented Programming  Programmer thinks about and defines the attributes and behavior of objects. 
IT PUTS THE ++ IN C++ Object Oriented Programming.
Review of C++ Programming Part II Sheng-Fang Huang.
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
You gotta be cool. Access Functions and Utility Functions Preprocessor Wrapper Looking Ahead to Composition and Inheritance Object Size Class Scope and.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
More C++ Classes Systems Programming. C++ Classes  Preprocessor Wrapper  Time Class Case Study –Two versions (old and new)  Class Scope and Assessing.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
More about Class 靜宜大學資工系 蔡奇偉副教授 ©2011. 大綱 Instance Class Members Class members can be associated with an instance of the class or with the class as a.
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.
Chapter 10 Introduction to Classes
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
1 Classes and Data Abstraction Part I Introduction Object-oriented programming (OOP)  Encapsulates data (attributes) and functions (behavior)
1 Classes and Data Abstraction 2 Objectives Understand encapsulation and data hiding Understand data abstraction and Abstract Data Types (ADTs) Create.
CLASSES : A DEEPER LOOK Chapter 9 Part I 1. 2 OBJECTIVES In this chapter you will learn: How to use a preprocessor wrapper to prevent multiple definition.
Chapter 9 Classes: A Deeper Look, Part I Part II.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
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.
Object Oriented Programming
Class 4 (L34) u Constructors u Default Constructor u Example of Default Constructors u Destructors u Constructors Are Called in Global Scope u Constructors.
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.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
C++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of 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?
1 Lecture 6 Classes and Data Abstraction: Part II.
Classes Methods and Properties. Introduction to Classes and Objects In object-oriented programming terminology, a class is defined as a kind of programmer-defined.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
Friend Function. 2 Any data which is declared private inside a class is not accessible from outside the class. A non-member function cannot have an access.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Chapter 9 Classes: A Deeper Look, Part 1 Seventh Edition C++ How to Program © by Pearson Education, Inc. All Rights Reserved.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
1 Example Original Array Array After 2 nd Pass Array After 1 st Pass Array After 3 rd Pass.
Object Access m1.write(44); m2.write(m2.read() +1); std::cout
Constructors and Destructors
Chapter 16: Classes and Data Abstraction
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
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?
C++ Classes & Object Oriented Programming
Classes: A Deeper Look Outline
CS212: Object Oriented Analysis and Design
Chapter 9 Classes: A Deeper Look, Part 1
Constructors and Destructors
9-10 Classes: A Deeper Look.
Recitation Course 0520 Speaker: Liu Yu-Jiun.
Classes: A Deeper Look, Part 1
Constructors & Destructors
More C++ Classes Systems Programming.
9-10 Classes: A Deeper Look.
Presentation transcript:

OOP Using Classes - I

Structures vs. Classes C-style structures –No “interface” If implementation changes, all programs using that struct must change accordingly –Cannot print as unit Must print/format member by member –Cannot compare in entirety Must compare member by member

Structures vs. Classes (cont.) Classes –Model objects Attributes (data members) Behaviors (member functions) –Defined using keyword class –Member functions Methods Invoked in response to messages

Class Example 1 class Time { 2 3 public: 4 Time(); // constructor 5 void setTime( int, int, int ); // set hour, minute, second 6 void printUniversal(); // print universal-time format 7 void printStandard(); // print standard-time format 8 9 private: 10 int hour; // (24-hour clock format) 11 int minute; // int second; // }; // end class Time

Access Control Class member access –Default private –Explicitly set to private, public, protected struct member access –Default public –Explicitly set to private, public, protected Access to class’s private data –Controlled with access functions (accessor methods) Get function –Read private data Set function –Modify private data

Static Data Members class Date { public: Date(); //constructor void setDate(int year, int month, int day); void getDate(int & year, int & month, int & day); static int numInstances(); void getDate2Str(); //forms string representation of the date and stores it ~Date(); //destructor private: int yearAttrib; int monthAttrib; int dayAttrib; char* Date_Str; static int countOfInstances; };

Static Data Members - Issues Class Wide Have to be accessed via Class Name Initialization –int Date::countOfInstances = 0; //don’t use the word static in front You cannot define nor initialize a static member attribute at present in the class declaration.

Member Functions Symbols defined within a class (even if declared public) exist within their own namespace. Inline Definitions static int numInstances(){ return countOfInstances; } –“Member functions defined within in the declaration are by default ‘inline’, even though you don’t have to declare them inline”.

Member Functions (cont.) Scope Resolution and Outside Definition void Date::setDate(int year, int month, int day) { yearAttrib = year; monthAttrib = month; dayAttrib = day; } Global Scope Definition –Assume that we have a non-member function getDate() that returns system date. –If the implementation of Date::getDate() just invoked the getDate() function by name alone, the compiler could interpret this as a recursive call to itself –To refer to a scope outside the class, use the scope resolution operator with no left parameter.

Constructors (Simplified) Constructors –Initialize data members Or can set later –Same name as class –No return type –Implicitly Called –Can be explicitly called –Compiler provides default version (better not to live with this!!) –It is likely that you will declare most constructors ‘public’ so that client module programmers can use them.

Example Date() { yearAttrib = 1900; //constructor! monthAttrib = 1; dayAttrib = 1; countOfInstances++; };

Default Constructor Constructors –Can specify default arguments –Default constructors Defaults all arguments OR Explicitly requires no arguments Can be invoked with no arguments Only one per class

Destructors –Special member function –Same name as class Preceded with tilde ( ~ ) –No arguments –No return value –Cannot be overloaded –Performs “termination housekeeping” Before system reclaims object’s memory –Reuse memory for new objects –No explicit destructor Compiler creates “empty” destructor”

When Constructors and Destructors Are Called –Global scope objects Constructors –Before any other function (including main ) Destructors –When main terminates (or exit function called) –Not called if program terminates with abort –Automatic local objects Constructors –When objects defined »Each time execution enters scope Destructors –When objects leave scope »Execution exits block in which object defined –Not called if program ends with exit or abort

When Constructors and Destructors Are Called (cont.) –static local objects Constructors –Exactly once –When execution reaches point where object defined Destructors –When main terminates or exit function called –Not called if program ends with abort