1 / 27

C++ cse.unt/~Nielsen/1040/ ../~sweany/CSCE1040F13/Home.html

C++ http://www.cse.unt.edu/~Nielsen/1040/ ../~sweany/CSCE1040F13/Home.html. Nov 27, 2013 CSCE 1040: Computer Science II Rodney Nielsen. Major 4. Due Thursday, December 5, 11:55 PM You can/should use C++. Extra Credit 1. Submit reports on bug resolution What was the error message

alyson
Download Presentation

C++ cse.unt/~Nielsen/1040/ ../~sweany/CSCE1040F13/Home.html

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. C++http://www.cse.unt.edu/~Nielsen/1040/../~sweany/CSCE1040F13/Home.htmlC++http://www.cse.unt.edu/~Nielsen/1040/../~sweany/CSCE1040F13/Home.html Nov 27, 2013 CSCE 1040: Computer Science II Rodney Nielsen

  2. Major 4 • Due Thursday, December 5, 11:55 PM • You can/should use C++

  3. Extra Credit 1 • Submit reports on bug resolution • What was the error message • Where did you try to find the solution • Were you successful • What was the bug • What was the solution

  4. Reading • Chapter 13 • by this past Monday, November 25 • http://en.wikipedia.org/wiki/C++ • http://en.wikipedia.org/wiki/Class_(computer_science) • http://en.wikipedia.org/wiki/Abstraction_(computer_science) • http://en.wikipedia.org/wiki/Information_hiding • http://en.wikipedia.org/wiki/Inheritance_(computer_science) • http://en.wikipedia.org/wiki/Method_overriding • http://en.wikipedia.org/wiki/Type_polymorphism • http://en.wikipedia.org/wiki/Function_overloading

  5. Classes • In object-oriented programming, a class is a construct that is used to define a distinct type. The class is instantiated into instances of itself – referred to as class instances, class objects, instance objects or simply objects. A class defines constituent members that enable its instances to have state and behavior. Data field members (member variables or instance variables) enable a class instance to maintain state. Other kinds of members, especially methods, enable the behavior of class instances. Classes therefore define the type of their instances.

  6. Classes • A class usually represents a noun, such as a person, place or thing, or something nominalized. For example, a "Banana" class would represent the properties and functionality of bananas in general. A single, particular banana would be an instance of the "Banana" class, an object of the type "Banana".

  7. C++ Classes • Objects • C++ introduces object-oriented programming (OOP) features to C. It offers classes, which provide the four features commonly present in OOP (and some non-OOP) languages: abstraction, encapsulation, inheritance, and polymorphism.

  8. Abstaction • In computer science, abstraction is the process by which data and programs are defined with a representation similar in form to its meaning (semantics), while hiding away the implementation details. Abstraction tries to reduce and factor out details so that the programmer can focus on a few concepts at a time. A system can have several abstraction layers whereby different meanings and amounts of detail are exposed to the programmer. For example, low-level abstraction layers expose details of the computer hardware where the program run, while high-level layers deal with the business logic of the program. • The following English definition of abstraction helps to understand how this term applies to computer science, IT and objects: • abstraction - a concept or idea not associated with any specific instance

  9. Information Hiding • In computer science, information hiding is the principle of segregation of the design decisions in a computer program that are most likely to change, thus protecting other parts of the program from extensive modification if the design decision is changed. The protection involves providing a stable interface which protects the remainder of the program from the implementation (the details that are most likely to change). • Written another way, information hiding is the ability to prevent certain aspects of a class or software component from being accessible to its clients, by, for example, using programming language features (like private variables).

  10. C++ Encapsulation • Encapsulation is the hiding of information to ensure that data structures and operators are used as intended and to make the usage model more obvious to the developer. C++ provides the ability to define classes as its primary encapsulation mechanisms. Within a class, members can be declared as either public, protected, or private to explicitly enforce encapsulation. A public member of the class is accessible to any method. A private member is accessible only to methods that are members of that class and to methods and classes explicitly granted access permission by the class ("friends"). A protected member is accessible to members of classes that inherit from the class in addition to the class itself and any friends.

  11. C++ Encapsulation • The OO principle is that all of the methods (and only the methods) that access the internal representation of a type should be encapsulated within the type definition. C++ supports this (via member methods and friend methods).

  12. C++ Encapsulation • It is generally considered good practice to make all data private or protected, and to make public only those methods that are part of a minimal interface for users of the class. This can hide the details of data implementation, allowing the designer to later fundamentally change the implementation without changing the interface in any way.

  13. Inheritance • In object-oriented programming (OOP), inheritance is a way to establish Is-a relationships between classes or objects. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes, superclasses, or parent classes. The resulting classes are known as derived classes, subclasses, or child classes. The relationships of classes through inheritance give rise to a hierarchy.

  14. Method Overriding • Method overriding, in object oriented programming, is a language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes. The implementation in the subclass overrides (replaces) the implementation in the superclass by providing a method that has same name, same parameters or signature, and same return type as the method in the parent class. The version of a method that is executed will be determined by the object that is used to invoke it. If an object of a parent class is used to invoke the method, then the version in the parent class will be executed, but if an object of the subclass is used to invoke the method, then the version in the child class will be executed.

  15. C++ Method Overriding Example #include <iostream> class Rectangle { public: explicit Rectangle(double l, double w) : length(l), width(w) {} virtual void print() const; private: double length; double width; }; void Rectangle::print() const { // print() method of base class std::cout << "Length = " << this->length << "; Width = " << this->width; } class Box : public Rectangle { public: explicit Box(double l, double w, double h) : Rectangle(l, w), height(h) {} virtual void print() const; // virtual is optional here, but it is a good practice to remind it to the developer private: double height; }; void Box::print() const { // print() method of derived class Rectangle::print(); // Invoke parent print() method. std::cout << "; Height= " << this->height; }

  16. C++ Inheritance • Inheritance allows one data type to acquire properties of other data types. Inheritance from a base class may be declared as public, protected, or private. This access specifier determines whether unrelated and derived classes can access the inherited public and protected members of the base class. If the access specifier is omitted, it is private.

  17. Polymorphism • In programming languages and type theory, polymorphism (from Greek πολύς, polys, "many, much" and μορφή, morphē, "form, shape") is the provision of a single interface to entities of different types. A polymorphic type is a type whose operations can also be applied to values of some other type, or types. There are several fundamentally different kinds of polymorphism.

  18. C++ Polymorphism • Polymorphism enables one common interface for many implementations, and for objects to act differently under different circumstances. • C++ supports several kinds of static (compile-time) and dynamic (run-time) polymorphisms. Compile-time polymorphism does not allow for certain run-time decisions, while run-time polymorphism typically incurs a performance penalty.

  19. Polymorphism Example int add(int x, int y) { return x + y; } char* add(char* s, char* t) { return strcat(s, t); } void main() { cout << "add(1,2) = " << add(1,2) << endl; cout << "add(\"Hello\",\"World\") --> "; cout << add("Hello","World") << endl; }

  20. Method Overloading • Method overloading or method overloading is a feature found in various programming languages such as Ada, C++, C#, D, and Java, that allows creating several methods with the same name which differ from each other in the type of the input and the output of the method. It is simply defined as the ability of one method to perform different tasks.

  21. Method Overloading Example 1 • doTask() and doTask(object O) are overloaded methods. To call the latter, an object must be passed as a parameter, whereas the former does not require a parameter, and is called with an empty parameter field. A common error would be to assign a default value to the object in the second method, which would result in an ambiguous call error, as the compiler wouldn't know which of the two methods to use.

  22. Method Overloading Example 2 • Another appropriate example would be a print(object O) method. In this case one might like the method to be different when printing, for example, text or pictures. The two different methods may be overloaded as print(text_object T); print(image_object P). If we write the overloaded print methods for all objects our program will "print", we never have to worry about the type of the object, and the correct method call again, the call is always: print(thing), regardless of thing’s type.

  23. C++ Static Polymorphism • Method overloading allows programs to declare multiple methods having the same name (but with different arguments). The methods are distinguished by the number or types of their formal parameters. Thus, the same method name can refer to different methods depending on the context in which it is used. The type returned by the method is not used to distinguish overloaded methods and, if it were the only difference in two method signatures, it would result in a compile-time error message.

  24. C++ Static Polymorphism • When declaring a method, a programmer can specify a default value for one or more parameters. Doing so allows the parameters with defaults to optionally be omitted when the method is called, in which case the default arguments will be used. When a method is called with fewer arguments than there are declared parameters, explicit arguments are matched to parameters in left-to-right order, with any unmatched parameters at the end of the parameter list being assigned their default arguments. In many cases, specifying default arguments in a single method declaration is preferable to providing overloaded method definitions with different numbers of parameters.

  25. C++ Dynamic Polymorphism – Inheritance • Variable pointers (and references) to a base class type in C++ can refer to objects of any derived classes of that type in addition to objects exactly matching the variable type. This allows arrays and other kinds of containers to hold pointers to objects of differing types. Because assignment of values to variables usually occurs at run-time, this is necessarily a run-time phenomenon.

  26. C++ Dynamic Polymorphism – Inheritance • C++ provides a dynamic_cast operator, which allows the program to safely attempt conversion of an object into an object of a more specific object type (as opposed to conversion to a more general type, which is always allowed). This feature relies on run-time type information (RTTI). Objects known to be of a certain specific type can also be cast to that type with static_cast, a purely compile-time construct that has no runtime overhead and does not require RTTI.

  27. C++ Dynamic Polymorphism – Virtual member methods • Ordinarily, when a method in a derived class overrides a method in a base class, the method to call is determined by the type of the object. A given method is overridden when there exists no difference in the number or type of parameters between two or more definitions of that method. Hence, at compile time, it may not be possible to determine the type of the object and therefore the correct method to call, given only a base class pointer; the decision is therefore put off until runtime. This is called dynamic dispatch. The most specific implementation of the method is invoked, according to the actual run-time type of the object. If the object type is known, this may be bypassed by prepending a fully qualified class name before the method call, but in general calls to virtual methods are resolved at run time.

More Related