ericsson l d-amps wireless office services page 1 training goals understand the c++ language...

61
Page 1 ericsson L D-AMPS Wireless Office Services Training Goals Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design Patterns Caveats in an Embedded Environment Introduction to C++ Style for DWOS

Upload: oscar-leonard

Post on 26-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 1 ericsson L

D-AMPS Wireless Office Services

Training GoalsTraining Goals

Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design Patterns Caveats in an Embedded Environment Introduction to C++ Style for DWOS

Page 2: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 2 ericsson L

D-AMPS Wireless Office Services

Legend & NotesLegend & Notes

Code samples appear in fixed font In code samples, C or C++ keywords are in italics Naming conventions

User defined types are mixed case starting with uppercaseclass TimeSwitch;

Variables are mixed case starting with lowercaseint firstCard;

Class data members are prefixed with the word “my”caddr_t myHWAddress;

Functions and methods are mixed case starting with lowercasevoid setHWAddr(caddr_t);

Page 3: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 3 ericsson L

D-AMPS Wireless Office Services

References Used to Compile this ClassReferences Used to Compile this Class

The C++ Programming Language, Bjarne Stroustrup Effective C++, Scott Meyers More Effective C++, Scott Meyers C++ Embedded Programming Techniques, Dan Saks from

Embedded Systems Conference East 1995 The C++ Programming Language, An Overview of C++, Douglas

Schmidt Design Patterns, Helms, et al. (Gang Of Four) Advanced C++, James Coplien

Page 4: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 4 ericsson L

D-AMPS Wireless Office Services

GlossaryGlossary

Attribute: a data-valued characteristic defined for a class. Attributes are used to maintain the state of instances of a class. Values can be connected to instances via the attributes of the class. Typically, the connected value is determined by an operation with a single parameter identifying the object. Attributes implement the properties of a type (synonyms: field, data member, instance variable, slot).

Data Member: Same as attribute Class: the mechanism used for defining the data elements and methods for a particular type

of object. All of the objects within any given class will have the same composition and behavior, but the state of each can be different (based on the data in their variables at any given time).

Base Class: in C++, a class from which another class inherits attributes and methods (synonym: superclass, parent class).

Instance: an object. In particular, an object mapped to a type via a classification relation. For example, "the design for the 68040" (an instance of the type "chip design") or "the application WP" (an instance of type "application") (synonym: object).

Method: the specific implementation of an operation for a class; code that can be executed in response to a request. A method can extend or override the behavior defined in the operation. In many systems, the selection of a specific method to respond to a request can be done at compilation or execution. A method is an implementation of behavior of a type (synonym: member function).

Page 5: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 5 ericsson L

D-AMPS Wireless Office Services

Glossary ContinuedGlossary Continued

Object: a representation of a real-world thing encapsulating the data and all of its procedures within itself. Anything to which a type applies; an instance of a type or class. An instance of a class is comprised of the values linked to the object (object state) and can respond to the requests specified for the class.

Scope: the part of the program over which the name is defined. Subclass: a class that inherits the attributes and methods of another class (synonym:

derived class, child class). Type: a predicate that describes the common properties and behavior for a collection of

objects (synonym: object type).

Page 6: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 6 ericsson L

D-AMPS Wireless Office Services

Advantages of C++Advantages of C++

Developed by Bjarne Stroustrup as a systems programming language with object-oriented extensions

A better “C” Complex user-defined data types and methods that operate on

them Support for object oriented programming without requiring it Storage layout of structures is compatible with C

Page 7: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 7 ericsson L

D-AMPS Wireless Office Services

Better “C”Better “C”

End-of-line comment The double slash can be used to ignore text until the end of

line Strict type checking - function prototypes are required The built-in type “bool” has been added

Conversions to int are provided Two values: true, false

Local variables can be defined anywhere in a block The ability to inline small functions Compatibility with C functions

In order to call C functions, linkage-specification must be used

Page 8: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 8 ericsson L

D-AMPS Wireless Office Services

Better “C” ContinuedBetter “C” Continued

New declaration operator reference “&” A reference is an alternate name for an object

int x = 1;

int &y = x; // x and y refer to the same int

The value of a reference cannot be changed after initialization For class, struct, union, and enum the tag of the type can be

used as the typename Function overloading

Multiple functions with the same name in the same scope Default values for function arguments

Values for arguments can be defined in the function prototype and are provided by the compiler when the user does not specify them on the function call

Functions can be members of class, struct or union

Page 9: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 9 ericsson L

D-AMPS Wireless Office Services

What is a Class and an ObjectWhat is a Class and an Object

Class: the mechanism used for defining the data elements and methods for a particular type of object. All of the objects within any given class will have the same composition and behavior, but the state of each can be different (based on the data in their variables at any given time).

Object: a representation of a real-world thing encapsulating the data and all of its procedures within itself. Anything to which a type applies; an instance of a type or class. An instance of a class is comprised of the values linked to the object (object state) and can respond to the requests specified for the class.

Page 10: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 10 ericsson L

D-AMPS Wireless Office Services

What Makes a Good ClassWhat Makes a Good Class

According to B. Stroustrup Something with a small, well defined set of operations Something that can be seen as a “black box” manipulated

through a set of operations Something whose implementation could conceivably be

modified without affecting the way the set of operations is used

Something one might want more than one of

Page 11: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 11 ericsson L

D-AMPS Wireless Office Services

Basic ClassBasic Class

class TimeSwitch

{

public: // interface functions

TimeSwitch(caddr_t addr); // Constructor

~TimeSwitch(void); // Destructor

private: // implementation

caddr_t myHWAddr; // Data Member

};

TimeSwitch::TimeSwitch(caddr_t addr): myHWAddr(addr)

{

; // Do Nothing Special

}

~TimeSwitch::TimeSwitch(void)

{

; // Do Nothing Special

}

Page 12: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 12 ericsson L

D-AMPS Wireless Office Services

Class Member Visibility & Data HidingClass Member Visibility & Data Hiding

One of the fundamental object-oriented principles is data-hiding C++ allows us to limit the visibility of class members with the

keywords public and private. public

Visible to users of the class private

Hidden from users of the class Implementation details Only seen by members of this class

Data contained in a class is referred to as a “has-a” relationship These keywords can appear anywhere in a class definition and

can appear multiple times. The convention is to place public members first followed by private.

The default visibility for a class is private, for a struct it is public

Page 13: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 13 ericsson L

D-AMPS Wireless Office Services

Organizing DevelopmentOrganizing Development

Usually broken into two files .h - header files containing class definitions, inline functions,

#defines, prototypes, etc. .cc - source files containing function implementation, static

data member initialization, helper functions Generally one class definition per .h with a matching .cc Items in the public section of the .h file form the interface for the

class Limit compilation dependencies by forward declarations in

header filesclass Message;

bool isValid(const Message *); // don’t need to know the size

Inline functions must be defined in the .h file The C++ compiler provides a preprocessor symbol #define

__cplusplus

Page 14: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 14 ericsson L

D-AMPS Wireless Office Services

ConstructorConstructor

The constructor provides the allocation and initialization of the user defined type

The constructor is called when an object is created The constructor must have the same name as the class and

does not have a return value A constructor with no parameters is called the default

constructor If no constructor is provided, the compiler provides a default Two types of initialization can be used:

Preferred Method: Comma separated initializer list One step process - copy constructor Nonstatic objects are Initialized in order they are declared Only way to initialize reference member data

Alternative Method: Assignment of data members Two step process - constructor, assignment operator

Page 15: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 15 ericsson L

D-AMPS Wireless Office Services

DestructorDestructor

The destructor provides deallocation of the user defined types contained in the class

The destructor is called when an object goes out of scope or is deleted

Every class has one and only one destructor The destructor must have the same name as the class prefixed

with a tilde “~” The destructor has no arguments and no return value If no destructor is provided, the compiler will provide a default

one

Page 16: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 16 ericsson L

D-AMPS Wireless Office Services

Making Copies of ObjectsMaking Copies of Objects

Special Constructor called the “copy constructor” Same name as the class with a const reference parameter

TimeSwitch(const TimeSwitch &)

If the copy constructor is needed and not provided, the compiler will provide a default one doing a memberwise copy. If you have dynamic memory this is probably not what you want!

Put it in the private section and leave it undefined to prevent it from being called accidentallyprivate:

TimeSwitch(const TimeSwitch &);

Page 17: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 17 ericsson L

D-AMPS Wireless Office Services

Creating ObjectsCreating Objects

Objects can be instantiated wherever variables can be used Objects can be instantiated automatically or dynamically

int main(void)

{

TimeSwitch ts(TS1_ADDR); // automatic variable

TimeSwitch ts1(ts); // copy constructor

TimeSwitch ts2 = ts1; // also copy constructor

TimeSwitch *tsPtr = new TimeSwitch(TS2_ADDR); // dynamic

// ...

delete tsPtr; // destructor for tsPtr called here

} // destructor for ts, ts1, and ts2 called here

Objects created automatically are destroyed when the object goes out of scope

Objects created dynamically with operator new() must be destroyed with operator delete()

Page 18: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 18 ericsson L

D-AMPS Wireless Office Services

Creating Objects ContinuedCreating Objects Continued

The ANSI C modifiers can be used with object instances and have the same meaning as C// in code section and not modifiable

const TimeSwitch ts(TS1_ADDR);

// do not optimize

volatile TimeSwitch ts(TS1_ADDR);

// limit access if file scope or persistence if function scope

static TimeSwitch ts(TS1_ADDR);

Arrays of objects can be instantiated, but the objects created will use the default constructor

Dynamically created arrays of objects must be destroyed with the operator delete []delete [] myArray;

Page 19: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 19 ericsson L

D-AMPS Wireless Office Services

Member FunctionsMember Functions

Functions contained within a class definition Follow the same rules as C functions In addition to call by value and pointer, C++ adds call by

reference Object should be passed by const & if they should not be

changed// in the .h class definition

public:

bool connect(const Circuit &c1, const Circuit &c2)

// in an accessing .cc file call by const referenceCircuit aParty, bParty;

TimeSwitch ts(HW_ADDR);

bool retVal = ts.connect(aParty, bParty);

Able to access class data members without explicit reference to the object

Page 20: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 20 ericsson L

D-AMPS Wireless Office Services

Inline Member FunctionsInline Member Functions

Similar to macros, but performed by the compiler Strong type checking Can lead to code bloat, should be used with small simple

functions May be difficult to debug since no “function” is generated The keyword inline is only a compiler hint Note: Global or static functions can be inline too

inline uint min(uint a, uint b) {return (a<b)?a:b);}

Page 21: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 21 ericsson L

D-AMPS Wireless Office Services

Accessor Member FunctionsAccessor Member Functions

If the class needs to provide access to member data, it should be done through access member functions

Public member data is strongly discouraged! Accessors are usually declared inline

Declared in the public interface of the class header In the class definition, functions with bodies do not need the

inline keyword to be considered for inlining For accessing data members, the convention is to name the

function the name of the variable without the “my” For setting data members, the convention is to preface the name

of the variable (without the “my”) with the word “set”public:

caddr_t hwAddress(void) {return myHWAddr;} const

void setHWAddress(caddr_t addr) {myHWAddr = addr;}

Page 22: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 22 ericsson L

D-AMPS Wireless Office Services

Const is Our FriendConst is Our Friend

Compiler enforces this constraint, use it! Prefer constant over #define

Real symbols and type information available Will occupy ROM space :(

Constant parameters Often used with references to give parameters pass-by-value

semantics without the overhead Pointers

char *p = “hello”; // non-const pointer, non-const data

const char *p = “hello”; // non-const pointer, const data

char *const p = “hello”; // const pointer, non-const data

const char *const p = “hello”; // const pointer, const data

Member functions Only const functions can be used with const objects Cannot modify member data or call non-const functions

Page 23: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 23 ericsson L

D-AMPS Wireless Office Services

Assignment OperatorAssignment Operator

If the assignment operator is needed and not provided, the default assignment operator performs a memberwise assignment of nonstatic data members. If you have pointers this is probably not what you want!

Put it in the private section and leave it undefined to prevent it from being called accidentallyprivate:

TimeSwitch &operator=(const TimeSwitch &);

operator= should check for assignment to self and return without performing the assignment if they are equivalent This can save time in some cases

Message m1;

m1 = m1; // odd but valid

Page 24: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 24 ericsson L

D-AMPS Wireless Office Services

Assignment Operator ContinuedAssignment Operator Continued

operator= should return a reference to *this Allows chaining of variables

Message m1, m2, m3;

m1 = m2 = m3;

ExampleMessage &Message::operator=(const Message &rhs)

{

if(this != &rhs) // check for assignment to self

{

// Do Assignment

}

return *this; // always return a reference to this

}

Page 25: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 25 ericsson L

D-AMPS Wireless Office Services

Self-ReferenceSelf-Reference

For nonstatic member functions, a hidden argument is passed referring to the object itself

Known as the “this” pointer Declared by the compiler to be a const pointer

TimeSwitch *const this;

Can be used to reference members, but this is not necessarythis->myHWAddr;

Used to pass or return your address or reference to yourself Assignment operator example Passing myself to another function

globalList->add(this) //add myself to the list

Page 26: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 26 ericsson L

D-AMPS Wireless Office Services

Classes or Structs to Partition the NamespaceClasses or Structs to Partition the Namespace

C has limited options for limiting name conflicts Keyword static for file scope Programmer imposed naming style

C++ expands on C C++ allows nested classes and structs C++ allows enums and constants to be placed in classes or

structsstruct OM_GLOBALS

{

static const char *libVersion;

}

OM_GLOBALS::libVersion = “1.0”; // a little more verbose

The ANSI C++ standard adds namespaces

Page 27: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 27 ericsson L

D-AMPS Wireless Office Services

Basic Class ContinuedBasic Class Continued

class Circuit;

class TimeSwitch

{

public: // interface functions

enum Mode {OPERATION, TEST};// Two Modes

TimeSwitch(caddr_t addr); // Constructor

~TimeSwitch(void); // Destructor

caddr_t hwAddress(void) {return myHWAddr;} const

void setHWAddress(caddr_t addr) {myHWAddr = addr;}

void setMode(Mode);

bool connect(const Circuit &c1, const Circuit &c2);

private: // implementation

caddr_t myHWAddr; // Data Member

// Compiler generated functions

TimeSwitch(const TimeSwitch &);

TimeSwitch &operator=(const TimeSwitch &);

};

Page 28: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 28 ericsson L

D-AMPS Wireless Office Services

Overloading Function NamesOverloading Function Names

Using the same name for different operations on different types; two or more different functions with the same name in the same scope

void execCommand(const char *); // execute the char string

void execCommand(const Signal &); // execute a Signal

Functions must have the same return type Note: functions differing only in their constness can be overloaded

Message &operator[](uint pos) {return myList[pos];}

const Message &operator[](uint pos) {return myList[pos];} const

Type information is usually encoded in the function name by way of “name mangling” Provides unique function names for the linker Provides strong type checking at link time

Unspecified number of arguments are supported through (…) and <stdarg.h>

Page 29: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 29 ericsson L

D-AMPS Wireless Office Services

Accessing C LibrariesAccessing C Libraries

Very easy to access C functions from C++ Before accessing C functions the compiler must be told that

they are C libraries Linkage declaration

extern “C” char *strcpy(char *, const char *);

Linkage blockextern “C”

{#include “snmp.h”

}

Implementation-dependent properties Disables “name mangling” for the C functions Name mangling is not in mentioned in the C++ standard

Not needed if the C libraries are compiled with a C++ compiler

Page 30: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 30 ericsson L

D-AMPS Wireless Office Services

Local Variable (Constants too) DefinitionLocal Variable (Constants too) Definition

Variables can be defined anywhere in a blockvoid updateRadioHead(void)

{makeConnection();uns8 rhNumber = lookupRH();

// rhNumber valid for the rest of the block

}

Variable scope extends to the end of the block Be careful, the constructor for a local variable is executed each

time the local variable’s block is executed Common point of confusion

for(int index=0; index<MAX; index++)

{...}

// index is still valid here, but this is changing in the ANSI draft

// if you intend it to be valid here, declare index before the “for”

Page 31: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 31 ericsson L

D-AMPS Wireless Office Services

Static Member DataStatic Member Data

One copy of the data regardless of the number of objects Order of initialization is not defined for static objects

Initialized before control is passed to main() Must be initialized in the .cc file

// message.h file

class Message

{

private:

static unsigned int refCount;

...

}

// message.cc file

unsigned int Message::refCount = 0;

Page 32: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 32 ericsson L

D-AMPS Wireless Office Services

Static Member FunctionsStatic Member Functions

Can be called without an instance of the class using the scope resolution operator “::”

Does not receive a “this” pointer Since the implied “this” pointer isn’t passed, calling is slightly

faster Can operate only on static members and global names Can be a const function

Page 33: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 33 ericsson L

D-AMPS Wireless Office Services

Singleton PatternSingleton Pattern

class MessageRouter

{

public:

static MessageRouter &theRouter(void) const;

~MessageRouter(void) { }; // needs to be defined

void start(void);

private:

MessageRouter(void) { }; // needs to be defined

MessageRouter(const MessageRouter &);

MessageRouter &operator=(const MessageRouter &);

};

MessageRouter &MessageRouter::theRouter(void)

{

static MessageRouter myRouter; // only created once

return myRouter;

}

// Usage in a .cc file

MessageRouter::theRouter().start()

Page 34: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 34 ericsson L

D-AMPS Wireless Office Services

Scope Resolution ExampleScope Resolution Example

class Example

{

public:

void print(int);

static int count(void);

private:

int x;

}

Example::print(int x)

{

Example::x = x;

::print(x);

}

int main(void)

{

int refCount = Example::count();

}

Page 35: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 35 ericsson L

D-AMPS Wireless Office Services

Scope Resolution OperatorScope Resolution Operator

A name prefixed with just “::” must be a global name int length = ::strlen(aString);

A class name followed by “::” followed by a name gives specific access to the class member of that name Implementation of a member function Access to static member functions or data Specific access if the name is hidden

Page 36: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 36 ericsson L

D-AMPS Wireless Office Services

Defining Other OperatorsDefining Other Operators

Make their function intuitive Strive to make them function like the built-in types Functions for the following operators can be declared

+ - * / % ^ & | ~ ! = < > += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= && || ++ -- ->* , -> [] () new new[] delete delete[]

The name of the operator function is the keyword operator followed by the operator itself

An operator can be called like any other function; the use of the operator is only a shorthand way of writing the call The following two lines are equivalent

bool result = (ts1 == ts2); // shorthand

bool result = ts1.operator==(ts2); // explicit call

Page 37: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 37 ericsson L

D-AMPS Wireless Office Services

Example Defining the (In)Equality OperatorExample Defining the (In)Equality Operator

Equality operator (operator==) Tests for equality of two objects Should work like built-in types (char, int, etc.)bool TimeSwitch::operator==(const TimeSwitch &rhs)

{

return ((this == &rhs) || (myHWAddr == rhs.myHWAddr))

}

Inequality operator (operator!=) Define operator!= in terms of operator==bool TimeSwitch::operator!=(const TimeSwitch &rhs)

{

return (!operator==(rhs));

}

Page 38: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 38 ericsson L

D-AMPS Wireless Office Services

Equality Operator ContinuedEquality Operator Continued

Using operator==() As we would expect TimeSwitch t1(HW_ADDR), t2(HW_ADDR);

if(t1 == t2) ; // true since addresses are equal

if(t1 == t1) ; // true since addresses are equal

Also valid, but strange looking if(t1.operator==(t2)) ; // true since addresses are equal

Page 39: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 39 ericsson L

D-AMPS Wireless Office Services

Review 1Review 1

T/F The goal of C++ is that the user only “pays” for features that they use

T/F C++ requires object oriented programming _____ functions are similar to macros but provide strong type

checking A reference is an ______ for an object or variable Name four member functions that will be generated by the

compiler What is the name and return value for a constructor The _____ and _____ keywords define the visibility of class

members You can limit compilation dependencies by using ______

declarations Give an example of instantiating an object How can you prevent a copy constructor from being called

Page 40: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 40 ericsson L

D-AMPS Wireless Office Services

Review 2Review 2

Automatic objects are destroyed when the object goes out of ______

Why should you use const references when passing function arguments

A class definition contains member _____ and member ______ In a class definition, functions with bodies are ______ Prefer ______ over #define Const member functions can only be used with _____ objects Message m2 = m1; // what function(s) is called What implicit variable do all nonstatic member functions have What differentiates functions with the same name in the same

scope When do you need to use extern “C” What symbols are used to define the scope

Page 41: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 41 ericsson L

D-AMPS Wireless Office Services

Review 3Review 3

T/F Variables must be defined at the top of the scope Given 5 objects of a class, how many copies of a data member

do you get for a:

static member

nonstatic member T/F Static member data can be initialized in the class definition T/F Static member functions can be called without having an

object T/F Static member functions can operate on nonstatic data

members To resolve “name hiding” use the ____ operator To define an operator function, use the ______ keyword followed

by _____ Give an example of one way to call the operator function

Page 42: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 42 ericsson L

D-AMPS Wireless Office Services

What Comes First, Objects or Classes What Comes First, Objects or Classes

May be easiest to think of objects first Look at the problem domain and pick the nouns Review the sequence diagrams Think about operations on the objects Look for commonality and group those objects together

Define the classes needed to represent the objects Create 4x5 CRC cards

Class Name Responsibility Collaboration

Keep the interfaces minimal Strive to minimize dependencies

Organize tightly coupled classes into subsystems KISS -- Keep It Simple Stupid!!

Page 43: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 43 ericsson L

D-AMPS Wireless Office Services

Object-Oriented ThinkingObject-Oriented Thinking

Objects have state (member data) Objects have well defined interface (member functions) Object oriented programming is about giving up global control

and relying on the objects local knowledge In an object oriented system, much of the control is delegated to

the objects Good object oriented design depends on the management of

object coupling Several design patterns deal with decoupling object oriented

systems

Page 44: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 44 ericsson L

D-AMPS Wireless Office Services

Specialization and GeneralizationSpecialization and Generalization

Another object oriented principle called inheritance A specialized class is derived from a base class The derived class obtains all of the properties, data, and

functions, of the base class A derived class can always be used in place of a base class, but

the opposite is rarely true class TimeSwitchMonitor

{

//...

void add(TimeSwitch *ts);

//...

}

// can be passed an RLUTimeSwitch

TimeSwitchMonitor monitor;

RLUTimeSwitch *ts = new RLUTimeSwitch(HW_ADDR);

monitor.add(ts);

Page 45: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 45 ericsson L

D-AMPS Wireless Office Services

Specialization and Generalization ContinuedSpecialization and Generalization Continued

Public keyword precedes the base-list Often referred to as an “is-a” relationship

class RLUTimeSwitch: public TimeSwitch

{...};

Derived class constructor should call the base class constructor, along with any parameters, in the initializer list

RLUTimeSwitch::RLUTimeSwitch(caddr_t addr) :

TimeSwitch(addr)

{...};

If the base class constructor is not explicitly called, the base class default constructor will be used

Constructed bottom up: base classes in declaration order, members in declaration order, constructor body

Destroyed automatically in the opposite order

Page 46: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 46 ericsson L

D-AMPS Wireless Office Services

Virtual FunctionsVirtual Functions

Can provide new behavior for base class functions The base class indicates a function can be overridden by using

the keyword virtual in front of the function declaration in the class definition virtual bool connect(const Circuit &c1, const Circuit &c2);

Ensures that the proper version of the function is called based on the type Works for pointers and references

TimeSwitch *ts = new RLUTimeSwitch(TS_ADDR);

ts->connect(circuit1, circuit2); // Actually calls

// RLUTimeSwitch connect

This behavior is known as polymorphism Destructors can be virtual

Guarantees the derived class is destroyed when a base class is destroyed

There is no such thing as a virtual static function

Page 47: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 47 ericsson L

D-AMPS Wireless Office Services

Virtual Functions, How Do They Do That?Virtual Functions, How Do They Do That?

Usually implemented with a virtual function table (vtbl) Very implementation dependent May add storage overhead to the class which may disrupt the

expected memory layout of the object Adds a level of indirection, and thus overhead, when the

virtual function is called If you need this functionality you probably can’t implement it

better than the compiler If you don’t need the functionality, don’t make the functions

virtual Whatever you do, don’t make all functions virtual because

“somebody might want to override them later”

Page 48: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 48 ericsson L

D-AMPS Wireless Office Services

Abstract ClassesAbstract Classes

Also known as Abstract Base Classes Represent abstract concepts for which an object cannot exist Abstract if there is at least one pure virtual function Declare pure virtual functions by an initializer = 0

// In definition of class TimeSwitch

virtual bool connect(const Circuit &c1, const Circuit &c2)=0;

Compiler prevents construction of objects of abstract classesTimeSwitch ts(HW_ADDR); // compiler will flag this as an error

TimeSwitch *ts = new TimeSwitch(HW_ADDR); // also an error

Derived classes must provide an implementation of the virtual function Base class can provide an implementation A pure virtual function not defined in a derived class remains

a pure virtual function

Page 49: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 49 ericsson L

D-AMPS Wireless Office Services

Abstract Classes ContinuedAbstract Classes Continued

Often used to provide an interface without exposing any implementation details

An abstract class may not be used as an argument type (pass-by-value) or return type

Pointers and references to abstract classes can be declared // from the TimeSwitchMonitor example

void add(TimeSwitch *ts); // pointers are valid

void add(TimeSwitch &ts); // references are too

void add(TimeSwitch ts); // not this!!

Page 50: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 50 ericsson L

D-AMPS Wireless Office Services

Class Member Visibility & Data Hiding RevisitedClass Member Visibility & Data Hiding Revisited

Keyword “protected” extends visibility to derived classes Derived classes can access members in the public and

protected section Breaks encapsulation by allowing derived classes direct access

to protected membersclass RLUTimeSwitch: public TimeSwitch

{

public:

// accessible by all classes

protected:

// accessible by this class and derived classes

private:

// only accessible by this class

};

Page 51: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 51 ericsson L

D-AMPS Wireless Office Services

Foundation Classes - Tools.h++Foundation Classes - Tools.h++

A toolkit of classes for building “real world” programs Developed by Rogue Wave Software (www.roguewave.com) Supported on a variety of platforms including UNIX, Windows

and most importantly VxWorks Bundled with a variety of compilers including Sun’s SparcWorks Provide good solid implementation for data structures Thread safe Faster time to market - already implemented and debugged Delivered in source code form Advanced use of C++ language - good learning tool

Page 52: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 52 ericsson L

D-AMPS Wireless Office Services

Overview of Tools.h++Overview of Tools.h++

All RogueWave class names begin with the letters “RW” RWCString, RWSlistCollectables, etc.

All function names begin with a lower case letter, with the first letter of subsequent words capitalized.

Organized into several categories General

RWCString, RWCRExp, RWDate, etc. Collections

RWBag, RWSet, RWSlistCollectables, RWHashDictionary Iterators

RWBagIterator, RWSlistCollectablesIterator Virtual I/O Streams

RWvios Link with the Rogue Wave library - usually librw.a

Page 53: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 53 ericsson L

D-AMPS Wireless Office Services

RWCStringRWCString

RWCString offers very powerful and convenient facilities for manipulating strings that are as efficient as the familiar standard C <string.h> functions

Implemented using a technique called copy on write. The copy constructor and assignment operators still

reference the old object and hence are very fast. An actual copy is made only when a "write" is performed, that

is if the object is about to be changed. Can handle embedded nulls.

#include <rw/cstring.h>

{

RWCString aString;

RWCString bString(“This is a string”);

aString = bString;

}

Page 54: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 54 ericsson L

D-AMPS Wireless Office Services

RWCString ContinuedRWCString Continued

Can be constructed with RWCString and const char * Access to a C style \0 terminated char * via the data() member

functionconst char *ptr = aString.data()

Access to lengthsize_t length(void) const;

Convert Casevoid toUpper(void);

void toLower(void);

Easy to check for equality using the operator==if(aString == bString) {...}

Append to existing string with operator+=RWCString &operator+=(const RWCString &); // append the string

Indexing operationschar &operator[](size_t i); // i’th character

RWCString operator()(size_t start, size_t len); // Substring

Page 55: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 55 ericsson L

D-AMPS Wireless Office Services

Smalltalk-like Collection ClassesSmalltalk-like Collection Classes

An object stored in collections must inherit abstract base class RWCollectable, with suitable definition for virtual functions hash() and isEqual(). The function hash() is used to find objects with the same hash value, then isEqual() is used to confirm the match.

Provide a method for applying a user-supplied function to each element of the collectionvirtual void apply(RWapplyCollectable ap, void *)=0;

The user-supplied function should have the following prototype

void yourApplyFunction(RWCollectable *, void *)

Some basic collectable classes are provide RWCollectableInt, RWCollectableString, RWCollectableTime

For other classes to be collectable, they must derive from the abstract base class RWCollectable

Page 56: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 56 ericsson L

D-AMPS Wireless Office Services

Redefining RWCollectable Member FunctionsRedefining RWCollectable Member Functions

Most methods of RWCollectable are virtual with default functionality

Commonly redefined member functions isEqual() matches objects, the default is to match on address compareTo() is necessary to sort items in a collection. If p1

and p2 are pointers to collectable objects, the statementp1->compareTo(p2);

should return:

0 if *p1 “is equal to” *p2

>0 if *p1 is “larger” than *p2

<0 if *p1 is “smaller” than *p2

The default is based on addressesreturn this == p2 ? 0 : (this > p2 ? 1 : -1);

hash() is used for collection classes using a hash table lookup, the default is to return the address of this

Page 57: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 57 ericsson L

D-AMPS Wireless Office Services

RWBagRWBag

Represents a group of unordered elements, not accessible by an external key.

Duplicates are allowed. If an item is added to the collection that compares equal

(isEqual) to an existing item in the collection, then the count is incremented. Note that this means that only the first instance of a value is actually inserted: subsequent instances cause the occurrence count to be incremented.

The hash() function and isEqual() function are used by RWBag

Page 58: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 58 ericsson L

D-AMPS Wireless Office Services

RWSetRWSet

Represents a group of unordered elements, not accessible by an external key

Duplicates are not allowed. The function hash() is used to find objects with the same hash

value, then isEqual() is used to confirm the match. An item c is considered to be "already in the collection" if there

is a member of the collection with the same has value as c for which isEqual(c) returns TRUE. In this case, method insert(c) will not add it, thus insuring that there are no duplicates.

The hash() function and isEqual() function are used by RWSet

Page 59: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 59 ericsson L

D-AMPS Wireless Office Services

RWSlistCollectablesRWSlistCollectables

A group of ordered elements without keyed access. Duplicates are allowed Ordering is determined by the order of insertion and removal Is implemented as a singly-linked list The virtual function isEqual() is used to find a match between a

target and an item in the collection

Page 60: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 60 ericsson L

D-AMPS Wireless Office Services

RWHashDictionaryRWHashDictionary

Page 61: Ericsson L D-AMPS Wireless Office Services Page 1 Training Goals Understand the C++ Language Understand Basic Object Oriented Design Introduction to Design

Page 61 ericsson L

D-AMPS Wireless Office Services

Summary of DWOS StyleSummary of DWOS Style