Tuesday 1 October 2013

Intro to Object Oriented Programming / C++

Object Oriented Concepts, Paradigms, and Terminology

Encapsulation

  • Encapsulation refers to bundling together sets of related data and/or functions.
  • C++ Classes are very similar to C structs, in that they both include user-defined sets of data items, which collectively describe some entity such as a Student, Book, or Airplane.
  • The difference is that C++ classes include not only data, but also the methods ( functions ) that maintain that data.
  • Conceptually classes can be pervieved in two ways:
    1. A set of data that collectively describes the state of an entity, and the methods responsible for maintaining that data, or
    2. A set of methods that collectively provide a service, and the data needed to provide that service.
  • Objects are specific instances of classes. E.g. objects are variables, classes are types.

Abstraction

  • Abstraction involves separating out the conceptual properties of a defined data type ( class ) from its specific implementation.
  • For example, a List class might be developed that provides support for adding items to the list, removing items from the list, printing the list, sorting the list, etc., without specifying how the List is actually implemented. Because users of the List class don't know how it is implemented, they will not know or care if the implementation changes at some later date.

Information Hiding

  • Information Hiding refers to keeping some internal information protected, so that it is neither visible nor accessible to the outside world.
  • For example, data fields within classes are often declared to be private, which means that only methods that are members of the class can access those data items. The methods then are usually public, meaning that any function or method can call them, although methods can also be private, and data items can be public.
  • Information hiding supports abstraction and encapsulation, by forcing outside functions to access the class data only through the publicly declared interface. As long as the interface doesn't change, the implementation can change without affecting any functions that use the class.

Personification

  • Using the concepts described above, objects are now responsible for maintiaing their own state, in response to messages sent to them. For example, in a banking simulation, a Teller object might ask a Check object to cancel itself, instead of theTeller cancelling the Check. Object-oriented programs are often designed as a set of cooperating objects working together to solve a common problem. ( Some of which represent tangible things in the real world, such as Checks, and some of which are less tangible, such as Meetings, Priorities, or Deadlines.

Inheritance, a.k.a. Specialization / Generalization

  • The concept of inheritance recognizes that often one defined class is really just a special case of another class, sharing a lot of common data fields and methods.
  • In this case, a more specialized class can be created as an extension of a more general class, with only the special differences created for and maintianed by the new class.
  • For example, Car, Motorcycle, and Airplane are all types of Vehicle. So if a Vehicle class were created that maintained all the state information common to all vehicle types, then a Car class could be developed that is a specialization of Vehicle, dealing with only the special properties that make Cars different from other Vehicle types.
  • In this example, the Car class is said to inherit from the Vehicle class, and is said to be a child of Vehicle.

Special Features of C++

I / O by streaming data to/from stream objects

  • cout << "Please enter the score for student " << i << " from 0 to 100.0: ";
  • cin >> scores[ i ];
  • if( scores[ i ] < 0.0 )
              cerr << "Error: " << scores[ i ] << "cannot be negative.\n";

Function Overloading

  • In C++ multiple functions can have the same name, provided that the number and/or types of arguments are different. For example:
    • int square( int ); // returns i * i;
    • double square( double ); // returns X * X;
    • void square( double length, double X, double Y ); // Draws a square with a side length at a location.

Operator Overloading

  • C++ programmers can write methods / functions that define how operators work with defined data types. For example:
    • Book b( "Favorite Bedtime Stories" );
    • Chapter c( "Hansel and Gretel" );
    • b += c;
    • // Based on the function "Book & operator += ( Book & theBook, Chapter & theChapter );"

Method Overriding

  • Method overriding is very similar to the concept of overloading, with two key exceptions:
    • The type and number of the function arguments match
    • One function is in a parent class, and the other is inside a child of that parent.
  • In essence this allows a child class to replace ( override ) a function that it inherited from its parent.

Exceptions

  • When a function encounters a problem that it can't handle, it throws and object of type Exception.
  • The system then finds a suitable Exception Handler to catch the Exception, and deal with the problem.
  • Different types of Exceptions and handlers can be defined, such as a null pointer Exception or divide by zero Exception.

Templates

  • Allow programmers to define functions and/or classes "generically", in which the data types involved are not specified.
  • Templates then allow the compiler to create versions of the code suitable for whatever data type(s) it may need, as needed.

Function Default Parameters

  • Functions can be written with default values for parameters. For example:
    • double area( double length, double width = length ) { return length * width; }
      • Can be called with one argument ( a square ) or two ( a rectangle )

Constructors and Destructors

  • Constructors are special methods within classes that called when an object of the class is created. It is kind of like an initializer, but may involve extensive amounts of code ( and come in several overloaded versions ) to initialize the class objects.
  • Destructors are class methods that are called when an object ceases to exist, and may perform valuable cleanup tasks.

No comments:

Post a Comment