DOC PREVIEW
UAF CS 311 - Object Oriented Programming in C++

This preview shows page 1-2-23-24 out of 24 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

PowerPoint PresentationHistory of C++Slide 3The C++ ParadigmSlide 5Slide 6Specific Enhancements to CSlide 8New Syntax in C++Example of InheritanceThe OutputExample of Virtual FunctionsWhat it PrintsThe Virtual Function TableVirtual Functions/Tables and Dynamic BindingSlide 16Slide 17Slide 18Slide 19A Diagram of Virtual Function FlowStrengths of C++Slide 22Weaknesses of C++My References4/23/08 CS 331 – OOP Group 1Object Oriented Programming in C++Presented by Errol Russell4/23/08 CS 331 – OOP Group 2History of C++•Created by Bjarne Stroustrup in 1979 at Bell Labs•Desire to add class constructs to the already popular C language. •“C with Classes” •Provides a number of features that “spruce up” the C language.•Old C language with Classes/OOP added into the mix.•Provides the capabilities for object-oriented programming.4/23/08 CS 331 – OOP Group 3History of C++•1986: Bjarne Stroustrup releases his book The C++ Programming Language•1998: Standard ratified as ISO/IEC 14882:1998•1999: Draft standard ISO/IEC 14882:1999•2003: Current version is the 2003 version, ISO/IEC 14882:2003. •200X: A new version of the standard (C++0x) is currently being developed. Will probably be ISO/IEC 14882:20094/23/08 CS 331 – OOP Group 4The C++ Paradigm•A programming paradigm is a fundamental style of programming.•C++ is a multi-paradigm language–Does not strictly adhere to one paradigm over another–Incorporates multiple paradigms together4/23/08 CS 331 – OOP Group 5The C++ Paradigm•Procedural Programming Paradigm –Procedures •Series of computational steps to be carried out, called at any point during a program's execution.•Abstraction Paradigm–Control Abstraction–Data Abstraction•Generic Programming Paradigm–Think templates4/23/08 CS 331 – OOP Group 6The C++ Paradigm•Object Oriented Programming (OOP) Paradigm–A notable paradigm in C++ that was not in C, and the focus of the rest of this talk–"objects" and their interactions to design applications and computer programs.–Based on several techniques •Encapsulation•Modularity•Polymorphism•Inheritance4/23/08 CS 331 – OOP Group 7Specific Enhancements to C•Templates–Write code without having to consider a specific data type. Used heavily when creating Abstract Data Types•Exception Handling–Helps avoid problems that would disrupt the normal flow of execution. “Catch” problems before they happen.•Polymorphism–Can assign different meanings/usages to an object based on context. For example, you could use an integer as a Boolean in some cases.4/23/08 CS 331 – OOP Group 8Specific Enhancements to C•Operator Overloading–Operators can be re-written to perform different tasks•Multiple Inheritance –One class can inherit from multiple classes•“Diamond Problem” solved with virtual inheritance•Classes–Constructs for objects•Virtual Functions–“. . .behavior is determined by the definition of a function with the same signature furthest in the inheritance lineage of the instantiated object on which it is called.” –It “knows” which of the functions it needs to execute the code for4/23/08 CS 331 – OOP Group 9New Syntax in C++•new – creates a new instance of an object•delete – destroys an object•The : operator used for inheritance•this – self-referential object pointer•Error handling–try–catch–throw4/23/08 CS 331 – OOP Group 10Example of Inheritanceclass Shape{public:int sides;void setSides(int n) {sides=n;};};class Square : public Shape{public:void getSides() {cout<<sides;}; // we have access to “sides” because of the// inheritance from “Shape”. The nice thing about// OOP and inheritance is that we don’t have to // rewrite a bunch of code, we just inherit it.};int main(){Square X;X.setSides(4);X.getSides();return 0;}4/23/08 CS 331 – OOP Group 11The Output4/23/08 CS 331 – OOP Group 12Example of Virtual Functionsclass Shape{public:virtual void message()=0;};class Square : public Shape{public:void message() { cout<<"I have 4 sides!"<<endl;}};class triangle : public Shape{public:void message() {cout<<"I have 3 sides!"<<endl;}};int main(){Shape* shapez[2];shapez[1]=new Square;shapez[2]=new triangle; for (int i = 1; i < 3; i++) {shapez[i]->message(); } return 0;}4/23/08 CS 331 – OOP Group 13What it Prints4/23/08 CS 331 – OOP Group 14The Virtual Function Table•Dynamically bound methods can be called from any instance of a class or its derived classes•Methods stored only once in a virtual function table (vtable).•Function calls are represented as offsets from the beginning of the table.4/23/08 CS 331 – OOP Group 15Virtual Functions/Tables and Dynamic Binding•The program contains a base class and two derived classes. •The base class is abstract and defines a pure virtual function •Derived classes provide the appropriate implementation4/23/08 CS 331 – OOP Group 16Virtual Functions/Tables and Dynamic Binding•A virtual function table is a mechanism used in OOP language implementation in order to support dynamic run-time method binding. •That is, virtual functions and the virtual function table are used to support dynamic binding.4/23/08 CS 331 – OOP Group 17Virtual Functions/Tables and Dynamic Binding•The virtual functions in the previous program give an example of dynamic binding.•Dynamic binding happens when invoking a derived class's member function using a pointer to its super class. •The implementation of the derived class will be invoked instead of that of the base class.4/23/08 CS 331 – OOP Group 18Virtual Functions/Tables and Dynamic Binding•Languages like C++ separate the interface of objects from the implementation•They tend to use the virtual function table approach because it •Allows objects to use a different implementation by using a different set of method pointers.4/23/08 CS 331 – OOP Group 19Virtual Functions/Tables and Dynamic Binding•When the program calls the “sides” method on a Shape pointer (which can point to any of the base or derived classes) •Run-time environment must be able to determine which implementation to call, depending on the actual type of object Shape points to.4/23/08 CS 331 – OOP Group 20A Diagram of Virtual Function Flow4/23/08 CS 331 – OOP Group 21Strengths of C++•“single, portable language that works better than any alternative in each of several areas”•“works well when [a more desirable


View Full Document
Download Object Oriented Programming in C++
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Object Oriented Programming in C++ and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Object Oriented Programming in C++ 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?