Object Access m1.write(44); m2.write(m2.read() +1); std::cout << m1.read() << " " << m2.read() << std::endl; m3 = new IntCell; std::cout read() << std::endl;

Slides:



Advertisements
Similar presentations
 2003 Prentice Hall, Inc. All rights reserved. ECE 2552 Dr. S. Kozaitis Summer Summary of Topics Related to Classes Class definition Defining member.
Advertisements

1 Classes Object-oriented programming: Model the problem as a collection of objects that have certain attributes and interact with one another and/or the.
OOP Using Classes - I. Structures vs. Classes C-style structures –No “interface” If implementation changes, all programs using that struct must change.
1 Lab Session-XII CSIT121 Fall 2000 b Namespaces b Will This Program Compile ? b Master of Deceit b Lab Exercise 12-A b First Taste of Classes b Lab Exercise.
1 Lab Session-XIV CSIT121 Spring 2002 b Namespaces b First Class Travel b Lab Exercise 14 (Demo) b Lab Exercise b Practice Problem.
 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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 16: Classes and Data Abstraction Outline 16.1Introduction 16.2Implementing a Time Abstract Data.
More C++ Classes Systems Programming. Systems Programming: C++ Classes 2 Systems Programming: 2 C++ Classes  Preprocessor Wrapper  Time Class Case Study.
CS-2303 System Programming Concepts
Classi - Esempi1 // SalesPerson class definition // Member functions defined in salesp.cpp #ifndef SALESP_H #define SALESP_H class SalesPerson { public:
 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.
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.
Copyright © 2012 Pearson Education, Inc. 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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
Mobility Research Lab mobility.ceng.metu.edu.tr Applied Innovative Interdisciplinary (AI2) Research Lab Short Course on Programming in C/C++
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
1 Classes and Data Abstraction 2 Objectives Understand encapsulation and data hiding Understand data abstraction and Abstract Data Types (ADTs) Create.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 6: Classes and Data Abstraction Outline 6.1Introduction 6.2Structure Definitions 6.3Accessing.
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.
1 Chapter 1 C++ Basics Review Reading: Sections 1.4 and 1.5.
CS Introduction to Data Structures Spring Term 2004 Franz Hiergeist.
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.
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?
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
Intoduction to C++ CENG 213 Data Structures1. 2 Programming in C++ C++ –Improves on many of C's features –Has object-oriented capabilities Increases software.
 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.
Introduction to C++ CENG 213 Data Structures 1.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
1 Chapter 1 C++ Templates Readings: Sections 1.6 and 1.7.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
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.
 2003 Prentice Hall, Inc. All rights reserved. ECE 2552 Dr. Këpuska Summer 2004 from Dr. S. Kozaitis Spring 2003 slides 1 Summary of Chapter 6: Classes.
Intoduction to C++ CENG 213 Data Structures1. 2 Programming in C++ C++ –Improves on many of C's features –Has object-oriented capabilities Increases software.
Chapter 16: Classes and Data Abstraction
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Classes C++ representation of an object
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?
Chapter 16: Classes and Data Abstraction
Chapter 1 C++ Basics Review
Introduction to C++ and Algorithm Analysis
Classes Object-oriented programming: Example: Bank transactions
Chapter Structured Types, Data Abstraction and Classes
Chapter 2 Objects and Classes
Classes: A Deeper Look Outline
Chapter 9 Classes: A Deeper Look, Part 1
Classes.
9-10 Classes: A Deeper Look.
Recitation Course 0520 Speaker: Liu Yu-Jiun.
CMSC 341 C++ and OOP.
Classes: A Deeper Look, Part 1
Classes C++ representation of an object
Review: C++ class represents an ADT
CMSC 341 C++ and OOP.
Separating Interface from Implementation
CMSC 341 C++ and OOP.
CMSC 341 C++ and OOP.
CMSC 341 C++ and OOP.
More C++ Classes Systems Programming.
9-10 Classes: A Deeper Look.
Presentation transcript:

Object Access m1.write(44); m2.write(m2.read() +1); std::cout << m1.read() << " " << m2.read() << std::endl; m3 = new IntCell; std::cout read() << std::endl; CENG 213 Data Structures1

2 Example: Class Time class Time { public: Time( int hr = 0, int mn = 0, int sc = 0 ) : hour(hr), minute(mn), second(sc) {} //default //constructor void setTime( int, int, int ); //set hr, min,sec void printMilitary() const; // print am/pm format void printStandard() const; // print standard format private: int hour; int minute; int second; };

CENG 213 Data Structures3 Declaring Time Objects int main() { Time t1, // all arguments defaulted t2(2), // min. and sec. defaulted t3(21, 34), // second defaulted t4(12, 25, 42); // all values specified... }

CENG 213 Data Structures4 Destructors Member function of class Performs termination housekeeping before the system reclaims the object’s memory Complement of the constructor Name is tilde (~) followed by the class name E.g. ~IntCell( ); ~ Time( ); Receives no parameters, returns no value One destructor per class

CENG 213 Data Structures5 When are Constructors and Destructors Called Global scope objects Constructors called before any other function (including main) Destructors called when main terminates (or exit function called) Automatic local objects Constructors called when objects defined Destructors called when objects leave scope (when the block in which they are defined is exited) static local objects Constructors called when execution reaches the point where the objects are defined Destructors called when main terminates or the exit function is called

CENG 213 Data Structures6 Class Interface and Implementation In C++, separating the class interface from its implementation is common. –The interface remains the same for a long time. –The implementations can be modified independently. –The writers of other classes and modules have to know the interfaces of classes only. The interface lists the class and its members (data and function prototypes) and describes what can be done to an object. The implementation is the C++ code for the member functions.

CENG 213 Data Structures7 Separation of Interface and Implementation It is a good programming practice for large-scale projects to put the interface and implementation of classes in different files. For small amount of coding it may not matter. Header File: contains the interface of a class. Usually ends with.h (an include file) Source-code file: contains the implementation of a class. Usually ends with.cpp (. cc or.C ).cpp file includes the.h file with the preprocessor command #include. »Example: #include ”myclass.h”

CENG 213 Data Structures8 Separation of Interface and Implementation A big complicated project will have files that contain other files. –There is a danger that an include file (.h file) might be read more than once during the compilation process. It should be read only once to let the compiler learn the definition of the classes. To prevent a.h file to be read multiple times, we use preprocessor commands #ifndef and #define in the following way.

CENG 213 Data Structures9 Class Interface #ifndef _IntCell_H_ #define _IntCell_H_ class IntCell { public: IntCell( int initialValue = 0 ); int read( ) const; void write( int x ); private: int storedValue; }; #endif IntCell class Interface in the file IntCell.h

CENG 213 Data Structures10 Class Implementation #include #include “IntCell.h” using std::cout; //Construct the IntCell with initialValue IntCell::IntCell( int initialValue) : storedValue( initialValue) {} //Return the stored value. int IntCell::read( ) const { return storedValue; } //Store x. void IntCell::write( int x ) { storedValue = x; } IntCell c lass implementation in file IntCell.cpp Scope operator: ClassName :: member

CENG 213 Data Structures11 A driver program #include #include “IntCell.h” using std::cout; using std::endl; int main() { IntCell m; // or IntCell m(0); m.write (5); cout << “Cell content : “ << m.read() << endl; return 0; } A program that uses IntCell in file TestIntCell.cpp