240 likes | 372 Views
Object Oriented Programming in C++ Presented by Errol Russell. History 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.
E N D
Object Oriented Programming in C++ Presented by Errol Russell CS 331 – OOP Group
History 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. CS 331 – OOP Group
History 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:2009 CS 331 – OOP Group
The 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 together CS 331 – OOP Group
The 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 templates CS 331 – OOP Group
The 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 • Inheritance CS 331 – OOP Group
Specific 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. CS 331 – OOP Group
Specific 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 for CS 331 – OOP Group
New 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 • throw CS 331 – OOP Group
Example of Inheritance class 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; } CS 331 – OOP Group
The Output CS 331 – OOP Group
Example of Virtual Functions class 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; } CS 331 – OOP Group
What it Prints CS 331 – OOP Group
The 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. CS 331 – OOP Group
Virtual 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 implementation CS 331 – OOP Group
Virtual 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. CS 331 – OOP Group
Virtual 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. CS 331 – OOP Group
Virtual 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. CS 331 – OOP Group
Virtual 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. CS 331 – OOP Group
A Diagram of Virtual Function Flow CS 331 – OOP Group
Strengths of C++ • “single, portable language that works better than any alternative in each of several areas” • “works well when [a more desirable language] is. . .not available, and because it interfaces easily with the libraries and the other languages you use” CS 331 – OOP Group
Strengths of C++ • Execution speed • Multi-paradigm • C++ is like a “glue” • “well suited to tying together the various parts of a programming project” CS 331 – OOP Group
Weaknesses of C++ • It is a "bloated" and complicated language • The ISO standard of the C++ language is about 310 pages (excluding library) • No language features to create multi-threaded software • Can multithread when it interfaces with OS or 3rd party applications, but creates portability concerns CS 331 – OOP Group
My References • http://www.research.att.com/~bs/books.htmlhttp://www.cantrip.org/realworld.html • Sebesta’s Programming Languages 8th edition. • Deitel & Deitel’s How to Program C++ 5th Edition • http://www.java2s.com/Code/Cpp/Class/Asimpleexampleofinheritance.htm CS 331 – OOP Group