1 / 18

COSC3306: Programming Paradigms Lecture 7: Object-Oriented Programming with C++

COSC3306: Programming Paradigms Lecture 7: Object-Oriented Programming with C++. Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003. C++. A C program is a collection of functions.

johnsoncarl
Download Presentation

COSC3306: Programming Paradigms Lecture 7: Object-Oriented Programming with C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COSC3306:Programming ParadigmsLecture 7: Object-OrientedProgramming with C++ Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003

  2. C++ • A C program is a collection of functions. • An object-oriented C++ program has only one function that is not part of a class. It is always named main and has a specific structure as the following. • return_data_type main() • { •  body of function • } • The first element of the main function is a returned data type, and anything within the braces belongs to function and therefore constitute the body of the function. • A C++ program uses at least one class: an application class from which an object is created to represent the program itself.

  3. Object and Class Declarations • A class declaration has the following syntax: • class class_name • { • private  protected  public : • definition of stored data • public  protected  private: • definition of member functions • } ;

  4. Three sources for classes • You can declare and implement your own classes. These will always be available to you as source code. • You can use classes that someone else has written and given to you. In this case, you have source code for such classes or using object code for the implementation of the classes. • You can use classes from the program libraries that accompany your C++ development software. The implementation of these classes is typically supplied as object code.

  5. Object and Class Implementations • After a class is declared, its name can be used as a type name to declare variables of that class type known as objects or sometimes called instances of class. The lifetime of the object is determined by the scope and lifetime rules of the language. C++ object can be created in two forms as the following. • Creating Objects using Static Binding. • Creating Objects using Dynamic Binding.

  6. To create an object using static binding • We define a variable whose data type is the class from which we want to create an object. • AppClass s; • Age a;

  7. To create an object using dynamic binding • To create an object using dynamic binding, we use the keyword new, followed by the name of class from which the object is to be created and a parameter list that matches one of the class constructors. The new operator performs three things. • It allocates memory for an object. • It takes the input parameters and passes then into the object constructor. • It returns a pointer to the memory location of the created object.

  8. Examples • AppClass *s; • s  new AppClass; • Age *a; • a  new Age; • …… • …… • delete object_name; • delete s;

  9. Member Functions Declarations • return_data_type FunctionName (ParamterDataType1, . . . ); • void shift(int amount); • bool is_on( ) const;

  10. Member Functions Implementations • class_name : : member_name • Age : : getChild( ) • int Age : : getChild( ) { return childAge; } Ex9-1

  11. Constant member function • void throttle : : flow( ) const •  Precondition : shut_off has been called at least once to initialize throttle. •  Postcondition : The value returned is the current flow as a proportion of the •  maximum flow. • { • return position  6.0;  since the throttle has six positions • } • void throttle : : is_on( ) const •  Precondition : shut_off has been called at least once to initialize throttle. •  Postcondition : if the throttle’s flow is above 0, then the function returns •  true; otherwise it returns false. • { • return (flow( ) 0); • }

  12. Accessing Members of Class •  In general, to tell an object to execute one of its member functions, we send a message to the object, and the syntax for this function call depends on whether you are using the static or dynamic bindings as follows: • When the object is created by static binding, we use the member access operator as dot operator “.”. • When the object is created by dynamic binding, we use the member access operator as arrow operator “”, consisting of a minus () sign and a greater than () sign with no intervening spaces.

  13. Friend Classes and Functions •  In the spirit of data encapsulation, the members of a class should be kept private so that they are not accessible to functions outside of the class. • The C++ friend mechanism allows the programmer to bypass class access restrictions, meaning that eliminates data encapsulation. • For example, consider the following segments of code: • class A { • friend class B; • }; • class B { • friend class C; • }; Ex9-2

  14. Inheritance • Inheritance is a form of software reusability in which new classes are created from existing ones by taking their attributes and behavior and including those with capabilities the new classes require. •  The declaration of derived classes must include the name of the class from which the new class is derived and the type of inheritance that is in effect. The derived class declaration has the following syntax • class class_name:inheritance_type base_class_name

  15. inheritance_type • The inheritance_type can be either public, protected or private. • The public and protected members of a base class are also public and protected in a public derived class. • The public and protected members of a base class are protected in a protected derived class. • In a private derived class, both the public and protected members of the base class are private.

  16. An example of inheritance •  POINT.H • #ifndef POINT_H • #define POINT_H • class Point • { • friend ostream &operator  (ostream &, const Point &); • public: • Point(float  0., float  0.);  default constructor • void setPoint(float, float);  set coordinates • float GetX( ) const { return X; }  get X coordinate • float GetY( ) const { return Y; }  get Y coordinate • protected:  accessible by derived classes • float X, Y;  X and Y coordinates of Point class • }; • #endif Ex9-3 Circlemain.cpp

  17. Polymorphism • The quality of being able to assume different forms is called polymorphism. • C++ allows you to give two or more different definitions to the same function name, which means you can reuse names that have strong intuitive appeal across a variety of situations. • In C++ polymorphism is implemented via virtual functions, functions that must be referenced with a pointer (using dynamic binding). In other words, when a request is made through a base-class pointer (or reference) to use a virtual function, C++ chooses the correct redefined function in the appropriate derived class associated with the object. In this respect, the keyword virtual tells C++ to wait until a method is called before deciding which definition of that method to use. • virtual function_type function_name (arguments); • EX9-4, Ex9-5

  18. Summary • OOP = • Objects (Abstraction) • +Classes (ADT) • +Inheritances (Classification) • +Overloading (Polymorphism) • +Dynamic Binding (Polymorphism)

More Related