Brent A. Perdue. July 15, 2009

Similar documents
An Incomplete C++ Primer. University of Wyoming MA 5310

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Glossary of Object Oriented Terms

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

Ch 7-1. Object-Oriented Programming and Classes

Java Application Developer Certificate Program Competencies

Introduction to C++ Introduction to C++ Week 7 Dr Alex Martin 2013 Slide 1

El Dorado Union High School District Educational Services

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

CISC 181 Project 3 Designing Classes for Bank Accounts

How to Write a Simple Makefile

C++ Programming Language

[PRAKTISCHE ASPEKTE DER INFORMATIK SS 2014]

Chapter 1: Key Concepts of Programming and Software Engineering

16 Collection Classes

Polymorphism. Problems with switch statement. Solution - use virtual functions (polymorphism) Polymorphism

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

Comp151. Definitions & Declarations

C++ INTERVIEW QUESTIONS

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

PetaLinux SDK User Guide. Application Development Guide

The C Programming Language course syllabus associate level

Moving from CS 61A Scheme to CS 61B Java

Netscape Internet Service Broker for C++ Programmer's Guide. Contents

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Copyright 2001, Bill Trudell. Permission is granted to copy for the PLoP 2001 conference. All other rights reserved.

Building Software Systems. Multi-module C Programs. Example Multi-module C Program. Example Multi-module C Program

C Programming Review & Productivity Tools

Object Oriented Software Design II

C++FA 5.1 PRACTICE MID-TERM EXAM

History OOP languages Year Language 1967 Simula Smalltalk

CS 111 Classes I 1. Software Organization View to this point:

An API for Reading the MySQL Binary Log

Course Name: ADVANCE COURSE IN SOFTWARE DEVELOPMENT (Specialization:.Net Technologies)

Advanced Data Structures

Java CPD (I) Frans Coenen Department of Computer Science

Facebook Twitter YouTube Google Plus Website

5 CLASSES CHAPTER. 5.1 Object-Oriented and Procedural Programming. 5.2 Classes and Objects 5.3 Sample Application: A Clock Class

Chapter 1 Fundamentals of Java Programming

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

3 Pillars of Object-oriented Programming. Industrial Programming Systems Programming & Scripting. Extending the Example.

6.088 Intro to C/C++ Day 4: Object-oriented programming in C++ Eunsuk Kang and Jean Yang

Curriculum Map. Discipline: Computer Science Course: C++

13 Classes & Objects with Constructors/Destructors

CPSC 226 Lab Nine Fall 2015

Introduction to Scientific Computing Part II: C and C++ C. David Sherrill School of Chemistry and Biochemistry Georgia Institute of Technology

Fast Arithmetic Coding (FastAC) Implementations

Fundamentals of Java Programming

Sources: On the Web: Slides will be available on:

Computer Systems II. Unix system calls. fork( ) wait( ) exit( ) How To Create New Processes? Creating and Executing Processes

Java (12 Weeks) Introduction to Java Programming Language

Software documentation systems

Introduction. dnotify

AP Computer Science Java Subset

CSC230 Getting Starting in C. Tyler Bletsch

CORBA Programming with TAOX11. The C++11 CORBA Implementation

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

SE 360 Advances in Software Development Object Oriented Development in Java. Polymorphism. Dr. Senem Kumova Metin

Surface and Volumetric Data Rendering and Visualisation

Chapter 13 Storage classes

Automation of Library (Codes) Development for Content Management System (CMS)

TECHNOLOGY Computer Programming II Grade: 9-12 Standard 2: Technology and Society Interaction

SQLITE C/C++ TUTORIAL

Table of Contents. Java CGI HOWTO

3.5. cmsg Developer s Guide. Data Acquisition Group JEFFERSON LAB. Version

Web development... the server side (of the force)

Getting off the ground when creating an RVM test-bench

Variable Base Interface

EP241 Computer Programming

Tutorial: Getting Started

C++ Crash Kurs. C++ Object-Oriented Programming

CMSC Fundamentals of Computer Programming II (C++)

Characteristics of Java (Optional) Y. Daniel Liang Supplement for Introduction to Java Programming

CS 241 Data Organization Coding Standards

/* File: blkcopy.c. size_t n

CS 103 Lab Linux and Virtual Machines

Algorithms, Flowcharts & Program Design. ComPro

IVI Configuration Store

CMPT 373 Software Development Methods. Building Software. Nick Sumner Some materials from Shlomi Fish & Kitware

Course Title: Software Development

Constructor, Destructor, Accessibility and Virtual Functions

Introduction to Object-Oriented Programming


Computing Concepts with Java Essentials

Running your first Linux Program

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Transcription:

Title Page Object-Oriented Programming, Writing Classes, and Creating Libraries and Applications Brent A. Perdue ROOT @ TUNL July 15, 2009 B. A. Perdue (TUNL) OOP, Classes, Libraries, Applications July 15, 2009 1 / 37

Outline of the Talk Outline 1 Example of a Real Object 2 Pros and Cons of Writing Classes 3 Components of a C++ Class 4 Inheritance 5 Shared Object Libraries 6 Stand-alone Applications B. A. Perdue (TUNL) OOP, Classes, Libraries, Applications July 15, 2009 2 / 37

Example of a Real Object class Amplifier : public NIM class Amplifier : public NIM Parent Class: NIM Attributes: width, height, length, rear connector specs Interface Points: rear connector power pins Child Class: Amplifier Attributes: power voltage, fine gain, coarse gain, shaping time, input polarity, internal ICs Interface Points: fine/coarse gain knobs, shaping time knob input polarity switch, input connector, unipolar/bipolar output connectors B. A. Perdue (TUNL) OOP, Classes, Libraries, Applications July 15, 2009 3 / 37

Example of a Real Object Why is this Amplifier object useful? class Amplifier : public NIM 1 Easy to move from one NIM bin to another Amplifier inherits NIM attributes 2 Easy to change operational variables (e.g. fine gain) Amplifier provides methods of changing variables via knobs, switches, and pots 3 Easy to pass information in and out Amplifier provides methods of signal passing via input and output connectors 4 Easy to achieve desired shaping and amplification Amplifier conceals the internal details from the user (encapsulation) 5 Easy to use inside a more complicated circuit Amplifier allows one to think at higher level of abstraction B. A. Perdue (TUNL) OOP, Classes, Libraries, Applications July 15, 2009 4 / 37

Pros and Cons of Writing Classes Pros and Cons of Writing Classes Pros modular programming encapsulation abstraction inheritance reuse of code Cons more time to design and develop more lines of code more complexity C++ software packages that use the OOP framework ROOT Geant4 B. A. Perdue (TUNL) OOP, Classes, Libraries, Applications July 15, 2009 5 / 37

Components of a C++ Class Components of a C++ Class Defining a Class Definition: Class - A user-defined type. It defines the characteristics of an object, including the its attributes (data members) and behaviors (methods or member functions). It can be thought of as a blueprint that describes the nature of an object. B. A. Perdue (TUNL) OOP, Classes, Libraries, Applications July 15, 2009 6 / 37

Components of a C++ Class The Class Specification The Class Specification // Using preprocessor directives to avoid multiple inclusion // of the header files in a program. #ifndef ClassName_h #define ClassName_h 1 // Class definition class ClassName public: // Prototypes of member methods that are public and can be // used outside the class defintion and implementation. private: // Data member declarations that are private and can be used // only inside the class definition and implementation. }; #endif Figure: ClassName-1.h B. A. Perdue (TUNL) OOP, Classes, Libraries, Applications July 15, 2009 7 / 37

Constructors Components of a C++ Class The Class Specification class ClassName public: // Demonstrates polymorphism ClassName(); // Default constructor ClassName(Int_t i); // Alternate constructor ClassName(Double_t d); // Alternate constructor private: // Private data members and methods }; Figure: ClassName-2.h B. A. Perdue (TUNL) OOP, Classes, Libraries, Applications July 15, 2009 8 / 37

Destructors Components of a C++ Class The Class Specification class ClassName public: ClassName(); // Default constructor ~ClassName(); // Destructor // Destructors are used to clean up and release resources. The most // common use of destructor is to release memory acquired in a // constructor. Destructors do not need to be called explicitly. private: // Private data members and methods }; Figure: ClassName-3.h B. A. Perdue (TUNL) OOP, Classes, Libraries, Applications July 15, 2009 9 / 37

Components of a C++ Class Example From ROOT: TH1D The Class Specification public: TH1D(); TH1D(const TVectorD &v); TH1D(const TH1D &h1d); TH1D(const char *name,const char *title,int_t nbinsx,const Float_t *xbins); TH1D(const char *name,const char *title,int_t nbinsx,const Double_t *xbins); TH1D(const char *name,const char *title,int_t nbinsx,double_t xlow,double_t xup); virtual ~TH1D(); Figure: ROOTExample-1.h B. A. Perdue (TUNL) OOP, Classes, Libraries, Applications July 15, 2009 10 / 37

Members of a Class Components of a C++ Class The Class Specification #include <ctime> #include "TRandom3.h" #ifndef Coin_h #define Coin_h 1 class Coin public: Coin(); ~Coin(); // Public member methods void Flip(void); Int_t GetFace(void); // Public "get" method void SetFace(Int_t face); // Public "set" method static Int_t HEADS(void) return 0; }; // static = only one copy for all Coin objects static Int_t TAILS(void) return 1; }; private: // Private data members Int_t fface; TRandom3 frandgen; Int_t ZeroOrOne(void); // Private "helper" method }; #endif Figure: Coin.h B. A. Perdue (TUNL) OOP, Classes, Libraries, Applications July 15, 2009 11 / 37

Components of a C++ Class The Class Implementation The Class Implementation #include "Coin.h" Coin::Coin() frandgen.setseed(time(0)); Flip(); // Initializes the state of the coin. }//END of constructor Coin::~Coin()} // Destructor does nothing in this case. void Coin::Flip(void) SetFace( ZeroOrOne() ); // Set method calls helper method. }//END of Flip() Int_t Coin::ZeroOrOne(void) return (Int_t)(fRandGen.Rndm() * 2); }//END of ZeroOrOne() void Coin::SetFace(Int_t face) fface = face; }//END of SetFace() Int_t Coin::GetFace(void) return fface; }//END of GetFace() Figure: Coin.cxx B. A. Perdue (TUNL) OOP, Classes, Libraries, Applications July 15, 2009 12 / 37

Using an Object Components of a C++ Class An Instance of a Class #include "Riostream.h" #include "Coin.cxx" void countflips(void) Int_t numheads = 0, numtails = 0; Coin acoin; // Create an instance of the Coin class for(int_t i=0; i<1000; i++) acoin.flip(); if( acoin.getface() == Coin::HEADS() ) numheads++; else numtails++; }//END of i loop cout << "The number of flips: " << numheads + numtails << endl; cout << "The number of heads: " << numheads << endl; cout << "The number of tails: " << numtails << endl; }//END of countflips() Figure: countflips.c B. A. Perdue (TUNL) OOP, Classes, Libraries, Applications July 15, 2009 13 / 37

Running the Program Components of a C++ Class An Instance of a Class B. A. Perdue (TUNL) OOP, Classes, Libraries, Applications July 15, 2009 14 / 37

Components of a C++ Class Summary: Components of a Class Summary: Components of a Class Class Specification public: the interface to the world constructors, destructors: how the objects are created and destroyed set and get methods: how to pass information to and from the objects - Flip(), SetFace(), GetFace() in Coin - knobs, switches, and connectors on Amplifier private: the internal structure (encapsulation and information hiding) data members and helper methods - fface, frandgen, ZeroOrOne() in Coin - fine gain, shaping time, internal ICs, etc in Amplifier Class Implementation definitions of constructors, destructors, and methods, and how they interact - Flip() calls SetFace(), and SetFace() calls ZeroOrOne() - wires and solder in Amplifier B. A. Perdue (TUNL) OOP, Classes, Libraries, Applications July 15, 2009 15 / 37

Inheritance Defining Inheritance Child class Subclass Derived class Definition: Inheritance - A consequence of deriving a new class from an existing one. The new class automatically inherits some or all of the data members and methods defined in the original class. Inheritance is a defining characteristic of object-oriented programming. B. A. Perdue (TUNL) OOP, Classes, Libraries, Applications July 15, 2009 16 / 37

Purposes of Inheritance Inheritance Purposes of Inheritance Reuse existing software - derived classes can be created via inheritance faster and easier than by writing them from scratch. Classification hierarchies can be developed that exploit natural commonalities between objects. B. A. Perdue (TUNL) OOP, Classes, Libraries, Applications July 15, 2009 17 / 37

Inheritance Descendants of TObject in ROOT Purposes of Inheritance B. A. Perdue (TUNL) OOP, Classes, Libraries, Applications July 15, 2009 18 / 37

Inheritance Building on the Coin Class Building on the Coin Class class Coin public: // Constructor and destructor // Public member methods // Public data members }; private: // Private data members protected: // Private inside Coin, but is inherited by derived (child) classes. // It also remains private inside the derived classes. void SetSeed(UInt_t seed); Figure: Coin.h B. A. Perdue (TUNL) OOP, Classes, Libraries, Applications July 15, 2009 19 / 37

Creating a Child Class Inheritance Building on the Coin Class #include <ctime> #include "Coin.h" #ifndef Nickel_h #define Nickel_h 1 class Nickel : public Coin public: Nickel() SetSeed(time(0)); }; ~Nickel();}; Int_t GetValue(void) return VALUE; }; private: static const Int_t VALUE = 5; }; #endif Figure: Nickel.h B. A. Perdue (TUNL) OOP, Classes, Libraries, Applications July 15, 2009 20 / 37

Creating a Child Class Inheritance Building on the Coin Class #include <ctime> #include "Coin.h" #ifndef Quarter_h #define Quarter_h 1 class Quarter : public Coin public: Quarter() SetSeed(time(0)+12345); }; ~Quarter();}; Int_t GetValue(void) return VALUE; }; private: static const Int_t VALUE = 25; }; #endif Figure: Quarter.h B. A. Perdue (TUNL) OOP, Classes, Libraries, Applications July 15, 2009 21 / 37

Using the Child Classes Inheritance Building on the Coin Class #include "Riostream.h" #include "Nickel.h" #include "Quarter.h" #include "Coin.cxx" void getrich(void) Nickel anickel; Quarter aquarter; Int_t numnickelheads = 0, numquarterheads = 0; for(int_t i=0; i<10; i++) anickel.flip(); aquarter.flip(); if( anickel.getface() == Coin::HEADS() ) numnickelheads++; if( aquarter.getface() == Coin::HEADS() ) numquarterheads++; }//END of i loop Int_t cents = anickel.getvalue()*numnickelheads + aquarter.getvalue()*numquarterheads; printf("you can keep $%i.%i\n",cents/100,cents%100); }//END of getrich() Figure: getrich.c B. A. Perdue (TUNL) OOP, Classes, Libraries, Applications July 15, 2009 22 / 37

Summary: Inheritance Inheritance Summary: Inheritance How do you create a child class from an existing class? class ChildName : public ParentName class Nickel : public Coin What gets inherited? all public members of the class - Flip(), GetFace(), SetFace(), HEADS(), TAILS() all protected members of the class - SetSeed() What doesn t get inherited? constructors and destructors - Coin(), Coin() any private members of the class - fface, frandgen, ZeroOrOne() B. A. Perdue (TUNL) OOP, Classes, Libraries, Applications July 15, 2009 23 / 37

Shared Object Libraries ACLiC ACLiC - Automatic Compiler of Libraries in Cint ACLiC will build a CINT dictionary and a shared library from your C++ script using the compiler and the compiler options that were used to compile the ROOT executable. You do not have to write a makefile remembering the correct compiler options, and you do not have to exit ROOT. - ROOT User Guide, Chapter 7 Commands in Cint to compile the Coin, Nickel, and Quarter classes: root [0].L Coin.cxx+ root [1].L Nickel.h+ root [2].L Quarter.h+ Resulting shared object libraries: Coin cxx.so, Nickel h.so, Quarter h.so B. A. Perdue (TUNL) OOP, Classes, Libraries, Applications July 15, 2009 24 / 37

Shared Object Libraries Compiling a Shared Object via Shell Script Compiling a Shared Object Library Using a Shell Script #!/bin/sh # (1) Creating dictionary code from header files using rootcint echo "Creating dictionary" rootcint -f dictcoins.cxx -c Coin.h Nickel.h Quarter.h # (2) Compiling source files echo "Compiling dictionary" gcc -c -o dictcoins.o dictcoins.cxx root-config --cflags echo "Compiling source code" gcc -c -o Coin.o Coin.cxx root-config --cflags # (3) Creating shared object library echo "Creating shared object library" gcc -shared -o libcoins.so dictcoins.o Coin.o echo "Finished" Figure: shared.sh B. A. Perdue (TUNL) OOP, Classes, Libraries, Applications July 15, 2009 25 / 37

Shared Object Libraries Breakdown of the Shell Script Compiling a Shared Object via Shell Script 1 Creating dictionary: rootcint -f dictcoins.cxx -c Coin.h Nickel.h Quarter.h The program rootcint generates the CINT dictionaries needed in order to get access to ones classes via the interpreter. -f = force overwrite of the output dictionary file (dictcoins.cxx) -c = write interpreter method interface stubs to the output file 2 Compiling source: gcc -c -o Object.o Source.cxx root-config cflags gcc = GNU C and C++ compiler -c = compile, but do not link into an executable -o = specify the output file (Object.o in this case) root-config cflags = returns compiler flags needed for code containing ROOT includes 3 Creating shared object: gcc -shared -o libcoins.so dictcoins.o Coin.o -shared = Produce a shared object which can then be linked with other objects to form an executable -o = specify the output file (libcoins.so in this case) B. A. Perdue (TUNL) OOP, Classes, Libraries, Applications July 15, 2009 26 / 37

Shared Object Libraries Using the Shared Object Library in ROOT Using the Shared Object Library in ROOT B. A. Perdue (TUNL) OOP, Classes, Libraries, Applications July 15, 2009 27 / 37

Stand-alone Applications The main Function Stand-alone Application: The main Function The main function is the entry point for every C++ program. It is required to build a stand-alone application. Signature - Int t main(int t argc, Char t *argv[]) returns an Int t argc = the number of command-line arguments argv[] = an array of the command-line arguments - argv[0] is usually the name of the program main() returns 0 at the end B. A. Perdue (TUNL) OOP, Classes, Libraries, Applications July 15, 2009 28 / 37

Stand-alone Applications The Structure of main() The main Function #include "Riostream.h" #include "Nickel.h" #include "Quarter.h" #include "Coin.h" Int_t main(int_t argc, Char_t *argv[]) // The body of main is identical to getrich.c Nickel anickel; Quarter aquarter; Int_t numnickelheads = 0, numquarterheads = 0; for(int_t i=0; i<10; i++) anickel.flip(); aquarter.flip(); if( anickel.getface() == Coin::HEADS() ) numnickelheads++; if( aquarter.getface() == Coin::HEADS() ) numquarterheads++; }//END of i loop Int_t cents = anickel.getvalue()*numnickelheads + aquarter.getvalue()*numquarterheads; printf("you can keep $%i.%i\n",cents/100,cents%100); return 0; }//END of main() Figure: main.cxx B. A. Perdue (TUNL) OOP, Classes, Libraries, Applications July 15, 2009 29 / 37

Stand-alone Applications Compiling an Application via Shell Script Compiling a Stand-alone Application Using a Shell Script #!/bin/sh # (1) Creating dictionary code from header files using rootcint echo "Creating dictionary" rootcint -f dictcoins.cxx -c Coin.h Nickel.h Quarter.h # (2) Compiling source files echo "Compiling dictionary" gcc -c -o dictcoins.o dictcoins.cxx root-config --cflags echo "Compiling source code" gcc -c -o Coin.o Coin.cxx root-config --cflags gcc -c -o main.o main.cxx root-config --cflags # (3) Creating shared object library echo "Creating shared object library" gcc -shared -o libcoins.so dictcoins.o Coin.o # (4) Linking object files into an application gcc -o getrich main.o libcoins.so root-config --glibs echo "Finished" Figure: compile.sh B. A. Perdue (TUNL) OOP, Classes, Libraries, Applications July 15, 2009 30 / 37

Stand-alone Applications Compiling an Application via Shell Script Compiling and Running the Program Using a Shell Script B. A. Perdue (TUNL) OOP, Classes, Libraries, Applications July 15, 2009 31 / 37

Making Decisions Stand-alone Applications Defining Makefile Definition: Makefile - Text files formatted in a way that tell the make utility how to compile one or multiple source files. The Makefile is essentially a list of rules for automating the compilation process. The advantage of using a Makefile over a shell script is that the Makefile sets up dependencies for when a file should be recompiled. B. A. Perdue (TUNL) OOP, Classes, Libraries, Applications July 15, 2009 32 / 37

Stand-alone Applications Components of a Makefile Components of a Makefile A Makefile rule: target: dependencies [tab] system command - Rule execution is based on timestamps. If any dependency is newer than the target, then the command is executed. An example rule: Coin.o: Coin.cxx Coin.h @gcc -c -o Coin.o Coin.cxx root-config cflags The Makefile executes the same commands as the shell script compile.sh, but it only executes the commands if they are needed. B. A. Perdue (TUNL) OOP, Classes, Libraries, Applications July 15, 2009 33 / 37

Stand-alone Applications Compiling an Application via Makefile Compiling and Running the Program Using a Makefile B. A. Perdue (TUNL) OOP, Classes, Libraries, Applications July 15, 2009 34 / 37

Stand-alone Applications Advantage of Using a Makefile Compiling an Application via Makefile B. A. Perdue (TUNL) OOP, Classes, Libraries, Applications July 15, 2009 35 / 37

Stand-alone Applications Displaying ROOT Graphics in Applications Displaying ROOT Graphics in Applications To be able to see any ROOT graphics windows created in an application an instance of the class TApplication must be created. TApplication - creates the ROOT Application Environment that interfaces to the windowing system event loop and event handlers. This class must be instantiated exactly once in any given application. - http://root.cern.ch/root/html/tapplication.html #include "TApplication.h" Int_t main(int_t argc, Char_t *argv[]) TApplication theapp("theapp", &argc, argv); // The body of main goes here. theapp.run(); return 0; }//END of main() Figure: tapplication.cxx B. A. Perdue (TUNL) OOP, Classes, Libraries, Applications July 15, 2009 36 / 37

Recap Recap of the Topics Covered 1 Example of a Real Object 2 Pros and Cons of Writing Classes 3 Components of a C++ Class 4 Inheritance 5 Shared Object Libraries 6 Stand-alone Applications B. A. Perdue (TUNL) OOP, Classes, Libraries, Applications July 15, 2009 37 / 37