Specification and Implementation Separating the specification from implementation makes it easier to modify programs. Changes in the class’s implementation.

Slides:



Advertisements
Similar presentations
Introduction to C++ An object-oriented language Unit - 01.
Advertisements

Chapter 11 Separate Compilation and Namespaces. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Separate Compilation.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 12 Separate Compilation and Namespaces. Abstract Data Type (ADT) ADT: A data type consisting of data and their behavior. The abstraction is that.
CSE 303 Lecture 16 Multi-file (larger) programs
Lecture 18: 4/11/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
C++ Lecture 6 Object Life-times Creating objects using new Using pointers to objects Aggregation (Containment –UML speak) Other C++ class features.
Inheritance CS 308 – Data Structures “the mechanism by which one class acquires the properties of another class” the properties of another class”
1 Lab Session-1 CSIT221 Fall 2002 b Refresher slides b Practice Problem b Lab Exercise (Demo Required)
Programming Assignment 2 CS308 Fall Goals Improve your skills with using templates. Learn how to compile your code when using templates. Learn more.
Chapter 11 Separate Compilation and Namespaces Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12A Separate Compilation and Namespaces For classes this time.
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.
Stacks CS 308 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
 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.
1 Abstract Data Type (ADT) a data type whose properties (domain and operations) are specified (what) independently of any particular implementation (how)
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
You gotta be cool. Access Functions and Utility Functions Preprocessor Wrapper Looking Ahead to Composition and Inheritance Object Size Class Scope and.
Data Structures Lecture-12 : STL Azhar Maqsood NUST Institute of Information Technology (NIIT)
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Multiple Files. Monolithic vs Modular  one file before  system includes  main driver function  prototypes  function.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Inheritance CS 302 – Data Structures Section 2.4 (pp ) and Section 6.7.
Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.
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 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
Separate Compilation Bryce Boe 2013/10/09 CS24, Fall 2013.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
Computer Science and Software Engineering University of Wisconsin - Platteville 5. Template Yan Shi CS/SE 2630 Lecture Notes.
Chapter 9 Separate Compilation and Namespaces. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Separate Compilation (9.1)
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation and Namespaces.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Separate Compilation and Namespaces.
Separating Class Specification tMyn1 Separating Class Specification from Implementation Usually class declarations are stored in their own header files.
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Classes Classes are a major feature of C++. They support – – Abstraction – Data hiding – Encapsulation – Modularity – Re-use through inheritance.
CLASSES AND OBJECTS Chapter 3 : constructor, Separate files, validating data.
Object Access m1.write(44); m2.write(m2.read() +1); std::cout
TK1924 Program Design & Problem Solving Session 2011/2012
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Classes C++ representation of an object
CSE687 – Object Oriented Design
Introduction to Classes
Chapter Structured Types, Data Abstraction and Classes
C++ Classes C++ Interlude 1.
Separate Compilation and Namespaces
Introduction to Classes
2011/11/10: Lecture 21 CMSC 104, Section 4 Richard Chang
Chapter 9 Classes: A Deeper Look, Part 1
Separating Definition & Implementation
Introduction to Classes and Objects
Code Organization CSCE 121 J. Michael Moore.
Classes.
C++ Compilation Model C++ is a compiled language
Yan Shi CS/SE 2630 Lecture Notes
Classes C++ representation of an object
Separating Interface from Implementation
Code Organization Classes
More C++ Classes Systems Programming.
Classes and Objects Systems Programming.
Presentation transcript:

Specification and Implementation Separating the specification from implementation makes it easier to modify programs. Changes in the class’s implementation should not affect the client (as long as the class’s interface has not been changed). Follow these two principles: –Place the class declaration in a header file to be included by any client that wants to use the class (e.g., StackType.h). –Place the definition of the class member functions in a source file (e.g., StackType.cpp)

Multiple definitions When splitting a program into multiple files, the header file needs to be included in each file in which the class is used. To avoid multiple definitions of a class (or even functions), the class specification need to be enclosed in the following preprocessor code: #ifndef NAME #define NAME #endif

Multiple definitions (cont’d) #ifndef STACKTYPE_H #define STACKTYPE_H template class StackType { public: …….. void Push(ItemType); void Pop(ItemType&); private: int top; ItemType *items; }; #endif

Rules when using templates When working with templates, we change the ground rules regarding which file(s) we put the source code into. Previously (i.e., no templates) StackType.cpp could be compiled into object code independently of any client code. Using templates, the compiler cannot instantiate a function template unless it knows the argument to the template This information is found in the client code !!

Rules using templates (cont’d) Two possible solutions –Place the class definition and member function definitions into the same file (e.g., StatckType.h) –Or, give the include directive for the implementation file at the end of the header file

Rules when using templates (cont’d) template class StackType { public: …….. void Push(ItemType); void Pop(ItemType&); private: int top; ItemType *items; }; #include StatckType.cpp