Notes
Slide Show
Outline
1
C++ Introduction
  • Classes and objects
2
Introduction to Objects
  • In OOP (object oriented programming) the structure of a program revolves around the concept of  “objects”: an object
    • Represents an instance of an entity existing in the real world
    • Is made of some “data members” which constitute the internal representation of the object status
    • Permits to manipulate/query its status only through a cleanly defined interface, constituted by some “member functions” that can access its data members
    • Therefore, communicates with other objects which model the problem at hand in the complete design hierarchy through invocations of their member functions


3
C++ classes
  • The C++ class keyword permits to describe the common layout (data members) and interface (allowed operations) of every object instance which belongs to a data type that the user is modeling.
  • After having declared a class of objects which share the same internal representation and the same allowed operations (member functions) as many instances of this new data type can be created as you like


4
Hiding the implementation of the class
  • The C++ language provides three member access specifiers to describe the boundaries of which data members/member functions are available to whom:
  • public: means that what follows (data member or member function) is accessible to everyone,
  • private: means that what follows is accessible only from the member functions of the class itself
  • protected: means that what follows is accessible only from the class itself or from a class derived from it.
  • The default access specifier for a class if not explicitly stated is private.
5
Reuse: reusing the implementation
  • When you have created an useful class, you can reuse its services not only declaring objects of the newly created data type, but also instantiating it inside of bigger classes
  • This relationship in OOP is called composition or aggregation and is depicted as follows in an UML diagram (which reads: a car has an engine)
6
Reuse: reusing the interface
  • When you have created an useful class, you can also create a class which is similar to it, but which provides either a different implementation of the same member functions or also some additional new member functions
  • This relationship in OOP is called inheritance and is depicted as follows in an UML diagram (which reads: a Derived class is a class inherited from Base, and therefore is a Base (with some more interesting features))
7
Interchangeable objects with polymorphism
  • When you inherit a class from an existing one, you can threat objects of the derived class like objects of the base class: this means that an object of the base data type (or a pointer to it, or a reference to it) can store also an object of the derived data type:


8
Interchangeable objects with polymorphism (cont…)
  • A function that calls a member function declared in a base class through a pointer or a reference to the base data type can call the version of the member function which is overriden in the derived class, if the method is tagged with the virtual keyword.


9
Interchangeable objects with polymorphism (cont…)
  • A function that calls a member function declared in a base class through a pointer or a reference to the base data type can call the version of the member function which is overriden in the derived class, if the method is tagged with the virtual keyword.


10
Creating and destroying objects
  • Objects (instances) of the user defined data types modeled using the class keyword are created and destroyed absolutely the same way as how the native, basic data types are
  • Objects can be created on the stack area when declared locally inside of a function, in which case they are destroyed when their visibility terminates (they go “out of scope”)
  • Objects can be created by mean of pointers dynamically on the heap area using the new keyword, in which case they must be destroyed explicitly using delete.
  • No garbage collection automatically done natively in the C++ language (altough third party garbage collectors
  •      exist)