ITEC 320
E N D
Presentation Transcript
ITEC 320 Lecture 24 OO in Ada (2)
Review • Questions? • Inheritance in Ada
Abstract types • What are they? • Why are they used?
Example • What do you think this means? package P is type T is abstract tagged record … end record; Procedure Op(X: T) is abstract; end p;
Interfaces • Code package Q is type Intis interface; procedure Op(X: Int) is abstract; procedure N(X: Int) is null; procedure Action(X: Int’Class); end Q; Similar, except not allowed to contain information like a record.
Why? • What do you think the purpose of abstract / interface capabilities are? • What scenarios do they allow? • What do they prevent?
Shortcut • Java syntax • varName.methodName(params); • Ada syntax • procedureName(records); record X is … end record procedure Op(var: X, other params here)… Can be called with X.Op(other params); --Ada and Java sitting in a tree Note: has to be tagged and class wide
Private variables • Keep the user away from your data! package Geometry is type Object is abstract tagged private; private type Object is tagged record x_coord : Float; y_coord : Float; end record; end Geometry; Requires a child package to inherit / use the Object class
Multiple inheritance • C++ method • Diamond problem • Java method • Ada method • Can inherit from one tagged • All others have to be interfaces • Allows for procedures / functions • type NT is new T and Int1 and Int2 with …
Ada.fin • Finished with Ada • What is different? • What is better? • What is worse? • Now moving on to C++
C++ intro • History then • Coding
C++ • History of C++… • “C++ was designed to provide Simula’s facilities for program organization together with C’s efficiency and flexibility for systems programming. “ • Write coding in assembly, C, C++….
OO • Simula 67 • 1965 (Vietnam, civil rights, Thunderbirds debuted) • Based off of algol… • Introduced basics of OO programming that we use today • Classes, inheritance, class variables, instance variables, class methods (virtual)
C • Multiple people using a computer at the same time… • Broken project called Multics…. • Need was still there, it became UNIX • Needed a language for the new OS… • CPL,BCPL,B, then C!
Pictures C++ = + Simula 67 Beautiful yet impractical C Ridiculously fast, yet difficult to work with
Java • Some parts are C-like • Memory management? • Designed not to fail You Computer
C++ • Complete control • Assembly to OO • Memory management • Speed • Used widely Did you check if all seven pointers were non-null or only six? Do you feel lucky, well do ya? “If programming in Pascal is like being put in a straightjacket, then programming in C is like playing with knives, and programming in C++ is like juggling chainsaws.” – Anonymous
Hello World #include <iostream> #include <string> using namespace std; intmain(intargc, char** argv) { string hello = "Hello World"; cout << hello << endl; return 0; } System libraries Standard namespace (functions?) Function definition, entry point for every C/C++ program (command line args) String class and initialization Print to console Return code of program to OS Command Prompt: vi hello.cpp g++ -o Hello hello.cpp ./Hello Returns 0, can be used with scripts
Producing programs File.java File.class JVM Actual machine code javac java conversion file.cpp Machine code that executes file g++ –o file file.c
Tools • g++ - Free C++ compiler • VI / Emacs / Crimson editor (or others) • putty and rucs / your own linux machine • C++ should be portable between windows and linux…it usually isn’t • Develop on platform you use to demonstrate…
Memory Java C++
Summary • Abstract types