ÄúµÄλÖãºÑ°ÃÎÍøÊ×Ò³£¾±à³ÌÀÖÔ°£¾C/C++±à³Ì£¾Tutorial of C++

C++ Programming Tutorial I (a)

Object-Oriented Programming Concepts


What is an object ?

  • Real-world objects:

    • All have state and behavior

    • Bicycles have state: current gear, two wheels, number of gears...

    • Bicycles have behavior: braking, accelerating, changing gears...

  • Software objects:

    • Also have state and behavior

    • Maintains its state in variables

    • Implements its behavior in methods


Definition: An object is a software bundle of variables and related methods.



  • Software objects can:

    • represent real-world objects, such as a bicycle

    • represent abstract concepts, such as an "Event" object in a window system



Encapsulation

  • What is encapsulation?

    • Anything that an object does not know or cannot do is excluded from the object.

    • Encapsulation is used to hide unimportant implementation details from other objects.

    • Implementation details of an object can be changed at any time without affecting other parts of the program.


Definition: Packaging an object's variables within the protective custody of its methods is called encapsulation.


  • Examples,

    • When you want to change gears on your bicycle, you don't need to know how the gear mechanism works, you just need to know which lever to move.

    • With software objects, you don't need to know how a method is implemented, you just need to know which method to invoke and its interface.

  • Benefits:

    • Modularity

      • Source code can be written and maintained independently

      • An object can be passed easily around

    • Information hiding

      • An object has a public interface for other objects to communicate with.

      • An object can maintain private information and methods that can be changed without affecting other objects.

  • Sometimes:

    • For implementation or efficiency reasons, an object may wish to expose some of its variables or hide some of its methods.


What are messages?

  • A single object is a component of a larger program that contains many other objects.

  • Application functionality is achieved by interactions of these objects.


Software objects interact and communicate with each other by sending messages to each other.




  • Components of a message:

    • The object to whom the message is addressed. (Your Bicycle)

    • The name of the method to perform. (changeGears)

    • Any parameters needed by the method. (lower gear)




What are classes?

  • Your bicycle

    • Your bicycle is just one of many bicycles in the world.

    • Bicycle have some state and behaviour in common.

    • However, each bicycle's state is independent of and can be different from other bicycles.

    • Your bicycle object is an instance of the class of objects known as bicycles.

    • Bicycles of the same model are built using the same blueprint.

  • Software objects

    • Possible to have many objects of the same kind that share common characteristics.

    • Objects of the same kind can be built using the same blueprint.

    • Software "blueprints" for objects are called classes.


Definition: A class is a blueprint or prototype that defines the variables and methods common to all objects of a certain kind.





Object instantiation:

  • Remember: a blueprint of a bicycle is not really a bicycle.

  • When you create an instance of a class, you create an object of that type.

  • The system allocates memory for the instance variables declared by the class.

  • You can then invoke the object's instance methods to make it do something.


Classes provide the benefit of reusability.
  • Programmers use the same class, and thus the same code, over and over again to create many objects.



Polymorphism

  • What is polymorphism?

    • The attribute that allows one interface to be used with a general class of actions.

    • It is the compiler's job to select the specific action as it applies to each situation.

  • Example,

    • Three types of stacks, one for interger values, one for strings, and one for a specific structure.

    • You can create three sets of functions called push() and pop() , one for each type of data.

    • The general interface is that of pushing and popping data onto and from a stack.

    • When you push data on or pop from the stack, it is the type of the data that will determine which specific version of push() or pop() , that will be called.


Polymorphism helps reduce complexity by allowing the same interface to be used to specify a general class of actions.



What is inheritance?

  • The process by which one object can acquire the properties of another object.

  • Concept of classification

  • Red Delicious apple ---> apple ---> fruit ---> food

  • An object (class) need only define those qualities that make it unique.


  • Allow classes to be defined in terms of other classes.

  • Mountain bikes, racing bikes, and tandems are all different kinds of bicycles.

  • These are subclasses of the bicycle class.

  • The bicycle class is the superclass of these subclasses.



  • Each subclass inherits instance variables from the superclass.

  • Each subclass inherits methods from the superclass.

  • Subclasses can add their own variables and methods.

  • Subclasses can override inherited methods.



The class hierarchy can be as deep as needed.
  • Methods and variables are inherited down through the levels.

  • The further down in the hierarchy a class appears, the more specialized its behavior.



End of Tutorial I (a)


[ tutorial 1(a)| tutorial 1(b)| tutorial 2| tutorial 3| tutorial 4]