2011年12月21日 星期三

Introduction to C++


What is C++ ?
  • it's "a better C"
  • it supports data abstraction
  • it's an Object-Oriented Programming Language

OOPLs have the following characteristics:
  • Encapsulation is the combining the data structure with actions. The data structure is used to represent the properties, the state, or characteristics of objects. The actions represent permissible behaviors of objects. These are controlled through the member functions that are attached to objects. It is through encapsulation that an object may be much more realistically represented - by including both the properties and the behavior in its definition. Further, the class designer can control the user interface much easier with this approach.
  • Inheritance is the ability to define a hierarchical relationship between objects that permits objects of a more specific class to inherit the properties (data) and behaviors (functions) of a more general class. For example, you might have dog objects with associated properties and behaviors and also beagle objects that will have all the properties and behaviors of dogs with a few more specific properties and behaviors of their own.
  • Polymorphism is the ability for different objects to interpret messages (functions) differently. For example, asking different shape objects (circle, square, rectangle, trapezoid) to return their area will result in different implementations. This is accomplished with virtual functions using "late (or dynamic) binding".
OOPLs support modular programming, ease of development and maintainability.

What is Object Oriented Programming?

C++ is an object oriented programming language. This means that the language is oriented toward programming with objects. Well, duh! Object oriented programming is a different approach than you learned in C. In C++, you create classes, after thinking about the design and what you want to accomplish, and, by the way, you usually don't get that part right on the first try. The class consists of members - data members or member functions. Member functions are also called methods. They're also called behaviors. Once you've created a class, you can declare (or define) a variable of that class type. That variable can be referred to as an object, or an instance (of the class type). You can then call a function using the object. Another way of saying that is, "you can apply the member function to the object". The member function is, well, first of all, it's just a function, meaning, it does something. Sometimes, the member function does something to the object. Sometimes, the member function does something for the object. Sometimes, the member function tells you something about the object. Sometimes, the member function is used to create the object (that's called a constructor). Sometimes, the member functions are used to destroy, or get rid of, the object (that's called a destructor). Some member functions look like operators (think + - / * -> % ! ~) when you called. C++ is all about accomplishing your task using objects and using the member functions that belong to the class(es). You can't really do anything with C++ that can't do with C, but once you get the hang of it, it's, in my opinion, a more natural way of getting the job done. Don't expect to immediately start coding using an object oriented approach, but may by the time you have completed this course, and have been exposed to dozens of examples in which classes have been used in many way, something should maybe ought to rub off.

C++ is "a better C", supports data abstraction, and is an effective OOPL primarily due to it use of classes. In C++, the class is the cornerstone of the language. Yes, C++ includes new syntax, new operators, new functions, and enhanced libraries, but it's really the implementation of classes, that give the language its identity.

Classes are:
  • a more powerful type of struct
  • data (properties, characteristics, state) and behaviors (methods)

For example, a dog class may contain data members such as breed, color, height, weight, number of feet (usually 4), eye color, etc. The class may also contain the behaviors that the dog can exhibit (the actions that it can perform, or the methods that it can apply). The dog may be able to sit, to run, to eat (that might affect its weight), etc. Sit, run, and eat would be member functions of the dog class. The class itself is not data.

An object is an instance (or occurrence) of a class. For example, you might have a dog, called Spot. Spot is an object. (Don't try to use this analogy on spouses!) Spot will possess all the properties of dog and can perform the behaviors of a dog. You might want Spot to sit down, so you'd say to spot: spot.sit(); That's, of course, assuming that Spot knows a little C++. The following C++ code illustrates these concepts.

class dog
{
  private:
    char breed[25];
    char color[20];
    int height;
    float weight;
    int feet;
    char eye_color[10];
  public:
    void sit(void);
    void eat(void);
    void run(void);
};

int main(void)
{
  dog spot;
  spot.sit();
  spot.eat();
  spot.run();
  return 0;
}


C++ 是比 C 好的 language, 主要的特性有:  Encapsulation, Inheritance, 與 Polymorphism.
C++ 的 data 稱為 member data; behavior 稱為 member function.

沒有留言:

張貼留言