Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming in C++ Ryan Kafuman 07/06/09. Programming in C++ ● Classes and Object Oriented Design ● Error Handling ● Function Overloading ● Operator Overloading.

Similar presentations


Presentation on theme: "Programming in C++ Ryan Kafuman 07/06/09. Programming in C++ ● Classes and Object Oriented Design ● Error Handling ● Function Overloading ● Operator Overloading."— Presentation transcript:

1 Programming in C++ Ryan Kafuman 07/06/09

2 Programming in C++ ● Classes and Object Oriented Design ● Error Handling ● Function Overloading ● Operator Overloading ● RTTI and C++ style casting ● Namespaces ● Templates

3 Object Oriented Design: What is an object? An object is an encapsulation of data and functionality. Each object can be thought of as a black box machine once it is implemented. Constructors create the object, and methods allow manipulation of the object.

4 Object Oriented design: Interfaces Knowing exactly what kinds of manipulations and outputs a type of object supports allows the definition of a set of functions that provide that functionality. This leads to modularization: to add a new impmementation of a particular object, simply requires implementing the interface.

5 Object Oriented Design: Classes Objects belong to classes Object classes define the properties of objects belonging to that class Classes are like structs See examples 1 and 2: Interfacing with Polygons

6 Object Oriented Design: Classes contd. Example1 initialized the object in main Example2 used a constructor Theintrinsic data is still globally accessable in both cases. When designing an interface, you do not want to access data outside the class: encapsulation see example3

7 Class Inheritance Unlike structs, classes can derive from other classes and inherit their properties The class inheriting is called the “derived class”, and the class it inherits from is the “base class” See example4

8 Polymorphism and Multiple Inheritance Classes can derive from more than one class Derived classes can be treated like base classes Polymorphic classes can be addressed as the base class, however function calls can be to functions implemented in the derived class. This uses RTTI, and induces latency, but allows a level of abstraction that C does not offer. See example5

9 Error Handling in C++ C++ offers two new ways of handling two different kinds of errors Assert: If a thing should never happen in a proper implementation, you can assert it. Exceptions can allert the program to other types of run-time errors.

10 Asserts Used for things that should never happen Simply assert it is so: assert(x>0); assert(ptr!=NULL); etc. #define NDEBUG removes all asserts.

11 Exceptions Exceptions handle run-time errors that can occur during normal operation Some functions return error codes. Sometimes functions cannot return error codes.

12 Exceptions Contd. try {... throw(some exception);... } catch(exception e) {... } See example6: Hermitian Matrices

13 Operator Overloading Use familiar operators (+, -, *, /, etc.) to interact with user defined types. Leads to straightforward, easy to read code. Implements like a regular function see example7: Sums and scalar multiplication of vectors

14 Function Overloading No more having to remember many names of different versions of the same function. Functions given the same name, but with different argument lists can coexist. Leads to simpler looking code, with simpler naming conventions. See example8: Measuring Distances

15 More Advanced Topics Namespaces allow global namespace to kept clean and can also allow some modularization. Templates allow an even higher level of abstraction

16 The End Thank You.


Download ppt "Programming in C++ Ryan Kafuman 07/06/09. Programming in C++ ● Classes and Object Oriented Design ● Error Handling ● Function Overloading ● Operator Overloading."

Similar presentations


Ads by Google