Logistics Job related news C++ Classes Co-op orientation - - PDF document

logistics
SMART_READER_LITE
LIVE PREVIEW

Logistics Job related news C++ Classes Co-op orientation - - PDF document

Logistics Job related news C++ Classes Co-op orientation Friday, Sept 16 th 12:00 1:30pm 70-1400 Friday, Oct 14 th 1:00 2:30pm Eastman Aud. Career Fair Wednesday, Sept 28 th 11am 4pm Gordon Field


slide-1
SLIDE 1

1

C++ Classes Logistics

  • Job related news

– Co-op orientation

  • Friday, Sept 16th 12:00 – 1:30pm 70-1400
  • Friday, Oct 14th 1:00 – 2:30pm Eastman Aud.

– Career Fair

  • Wednesday, Sept 28th
  • 11am – 4pm
  • Gordon Field House

Project

  • Any questions?
  • Coding style

– Commenting – File organization – Use of RCS

C++ == (C with Classes)

  • Classes are extensions of struct

– Access rules on data members – Constructor / Destructor functions – Overloaded Operators – Member functions

Access rules

  • Like in Java…data members can be

– Public – all functions have access to data member – Private – only member functions have access to data member – Protected – only class and devired classes have access to the data member

File organization

  • Header file (.h)

– Declaration of class – Included in all source that use the class.

  • Source file (.cpp)

– Code implementing all class functions

  • Let’s look at a simple class
slide-2
SLIDE 2

2

Box.h

#ifndef BOX_DEFINED #define BOX_DEFINED class Box { private: int height, width, depth; public: Box (int h, int w, int d); int volume (); }; #endif

Box.cpp

#include “Box.h” Box::Box (int h, int w, int d) { height=h; width=w; depth=d; } int Box::volume () { return height * width * height; }

Classes and memory

  • Box:

height width depth Member functions

Constructor

  • Special member function used to create an
  • bject of a class.

– Has same name as the class. – Does not return anything. – Used to initialize data members.

Box.cpp

#include “Box.h” Box::Box (int h, int w, int d) { height=h; width=w; depth=d; } int Box::volume () { return height * width * height; }

Object initialization

  • Objects are initialized using constructors made

available by the class.

Box mybox (3,4,5); Box *boxOnHeap = new Box(3,4,5); // Note we can do this with intristic data // types as well int a (7); char b (‘j’); float f (7.8);

slide-3
SLIDE 3

3

Sidebar: initialization v. assignment

  • This

int a (7); //initialization

  • Is the same as this

int a=7; //initialization

  • But not this

int a; a=7; //assignment

Default constructor

  • Constructor that takes no argument
  • Used to construct a “default” object.
  • Generally a good idea to provide a default

constructor.

Default Constructor

class Box { private: int height, width, depth; public: Box (int h, int w, int d); int volume (); };

// This is okay Box mybox (3,4,5); // This is not Box defaultBox;

Constructor overloading

  • A class can have multiple constructors.
  • Which gets called depends upon arguments

listed when instantiated.

Default Constructor

class Box { private: int height, width, depth; public: Box(); Box (int h, int w, int d); int volume (); };

// This is okay Box mybox (3,4,5); // Now,this is okay Box defaultBox;

Default constructor

include “Box.h” Box::Box (int h, int w, int d) { height=h; width=w; depth=d; } Box::Box () { height=1; width=1; depth=1; }

slide-4
SLIDE 4

4

Default constructor arguments

  • You can provide default args for your constructor

class Box { private: int height, width, depth; public: Box (int h=1, int w=1, int d=1); int volume (); }; Box thisBox0; Box thisBox1(3); Box thisBox2(3,4); Box thisBox3(5,6,9);

C++ Rules for constructors

  • If a class does not define any constructors, a

default constructor is automatically created for it

  • If a class defines at least one constructor, a

default constructor must be explicitly define

  • WARNING: Never trust the compiler to do

it for you!!!

Default Constructor

class Box { private: int height, width, depth; public: Box (int h, int w, int d); int volume (); };

// This is okay Box mybox (3,4,5); // This is not Box defaultBox;

Default Constructor

class Box { private: int height, width, depth; public: int volume (); };

// Now, this is okay Box defaultBox;

Objects and arrays

  • Objects placed in arrays must have a default

constructor.

//default constructor required Box boxArray[20]; Box boxArrayOnHeap = new Box[20]; // but not here Box *boxPtrArray[20];

Parameter initialization list

  • Initializes data members upon construction

rather than in body of constructor

Box::Box (int h, int w, int d): height(h), width(w), depth (d) { }

slide-5
SLIDE 5

5

Parameter initialization list

  • Copy vs. Assignment

Box::Box (int h, int w, int d) { height=h; width=w; depth=d; }

Can also be written using parameter initialization list (this way is more efficient).

Box::Box (int h, int w, int d): height(h), width(w), depth (d) {} // no assignment is made.

Parameter initialization list

class PairOfBoxes { private: Box b1; Box b2; public: PairOfBoxes (); }; class Box { private: int height, width, depth; public: Box (int h, int w, int d); };

Parameter initialization list

  • Must use initialization list

PairOfBoxes::PairOfBoxes(): b1 (1,2,3), b2(4,5,6) {}

Copy constructor

  • Used to construct an object from another
  • bject of the same class
  • Object has access to non-public members of
  • bjects of the same class

Copy constructor

class Box { private: int height, width, depth; public: Box (const Box& B); Box (int h, int w, int d); }; Box::Box (const Box& B) : height (B.height), width (B.width), height (B.height) {}

Copy constructor

Box B (1,2,3); Box C(B); Or Box B (1,2,3); Box C = B; // initialization not assignment

slide-6
SLIDE 6

6

Constructor

  • Copy Constructor

– If no copy constructor is defined for a class, the default copy constructor is used.

  • Member by member copy of data from one object to

another.

  • Can be troublesome if class have pointers as data

members.

  • WARNING: Never trust the compiler to do

it for you!!!

Destructor

  • A destructor for a class is called when the memory
  • f an object of that class is reclaimed:

– A global (static) object is reclaimed when the program terminates. – A local (automatic) object is reclaimed when the function terminates (stack is popped). – A dynamically allocated object is reclaimed when someone invokes the delete operator on it. – Like Java finalize

Destructor

void aFunction (Foo f) { Foo f2; Foo *fooptr = new Foo(); ... delete fooptr; // destructor called } // after function is complete, destructor called on f and f2

Destructor

  • Destructors have the same name as the class but

preceded with a ~.

  • Destructors do not return a value.
  • Destructors take no arguments and cannot be
  • verloaded.
  • Destructors are used for cleaning up object data /

state

– Allocated memory – Close files – Close network connections, etc.

Destructors

class Foo { private: int *array_member; int asize; ... public: Foo (int size); ~Foo (); }

Destructors

Foo::Foo (int size) : asize (size), array_member (new int[size]) { for (int i=0; i<size; i++) array_member[i] = 0; } Foo::~Foo () { // cleanup what was allocated by // constructor if (array_member != 0) delete array_member; }

slide-7
SLIDE 7

7

Summary

  • Classes

– Member access – Constructors

  • Default constructors
  • Copy constructors

– Destructors

For next time

  • Continue with classes…

– Conversions/Conversion Constructors – Operators – When and where object are created

  • Questions?
  • Have a good weekend