Object Oriented Programming in C++ Presented by Errol Russell
-
Upload
keely-logan -
Category
Documents
-
view
18 -
download
5
description
Transcript of Object Oriented Programming in C++ Presented by Errol Russell
4/23/08 CS 331 – OOP Group 1
Object Oriented Programming in C++
Presented by Errol Russell
4/23/08 CS 331 – OOP Group 2
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.
4/23/08 CS 331 – OOP Group 3
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
4/23/08 CS 331 – OOP Group 4
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
4/23/08 CS 331 – OOP Group 5
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
4/23/08 CS 331 – OOP Group 6
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
4/23/08 CS 331 – OOP Group 7
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.
4/23/08 CS 331 – OOP Group 8
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
4/23/08 CS 331 – OOP Group 9
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
4/23/08 CS 331 – OOP Group 10
Example 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 11
The Output
4/23/08 CS 331 – OOP Group 12
Example 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 13
What it Prints
4/23/08 CS 331 – OOP Group 14
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.
4/23/08 CS 331 – OOP Group 15
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
4/23/08 CS 331 – OOP Group 16
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.
4/23/08 CS 331 – OOP Group 17
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.
4/23/08 CS 331 – OOP Group 18
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.
4/23/08 CS 331 – OOP Group 19
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.
4/23/08 CS 331 – OOP Group 20
A Diagram of Virtual Function Flow
4/23/08 CS 331 – OOP Group 21
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”
4/23/08 CS 331 – OOP Group 22
Strengths of C++
• Execution speed
• Multi-paradigm
• C++ is like a “glue”– “well suited to tying together the various parts
of a programming project”
4/23/08 CS 331 – OOP Group 23
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
4/23/08 CS 331 – OOP Group 24
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