Ada_OOP97
-
Upload
carlosivanx -
Category
Documents
-
view
224 -
download
0
Transcript of Ada_OOP97
-
8/7/2019 Ada_OOP97
1/33
2001.09.24 Ada 95 1
Ada 95Mais uma linguagem para aprogramao orientada pelos
objectos
Pedro Guerreiro
Departamento de Informtica
Faculdade de Cincias e Tecnologia
Universidade Nova de Lisboa
-
8/7/2019 Ada_OOP97
2/33
2001.09.24 Ada 95 2
O que o Ada? Uma linguagem de programao de uso geral
aplicaes financeiras
aplicaes administrativas aplicaes cientficas
aplicaes embutidas
aplicaes de sistema
aplicaes de tempo real
Usada em vrios domnios militar
das telecomunicaes aeroespacial.
-
8/7/2019 Ada_OOP97
3/33
2001.09.24 Ada 95 3
O que o Ada? (cont.) Pensada para a programao em grande escala
software cuja vida til longa software que tem que ser mantido software que requer equipas de programadores
Com preocupaes de rigor e fiabilidade : Sintaxe pesada
Semntica bem definida Muitas verificaes feitas pelo compilador, para apanhar mais
erros mais cedo.
O Ada a linguagemda engenharia desoftware!
-
8/7/2019 Ada_OOP97
4/33
2001.09.24 Ada 95 4
De onde vem o Ada?1954-1957 FORTRAN1958-1960 Algol
...1971 Pascal...1970 e poucos O Departamento de Defesa (DoD) dos
EUA preocupa-se com os custoscrescentes do software.
...
1975 O DoD cria o Higher Order LanguageWorking Group, HOLWG.1975-1978 Avaliao pelo HOLWG das linguagens
existentes face s necessidades do DoD.
-
8/7/2019 Ada_OOP97
5/33
2001.09.24 Ada 95 5
De onde vem o Ada? (cont.)1978 Concurso pblico internacional para odesenho de uma nova linguagem,satisfazendo requisitos enunciados.
1981 Ganha o concurso a equipa da CII-HB(Frana) chefiada por Jean Ichbiah, comuma linguagem baseada no Pascal, quemais tarde recebe o nome Ada (em
homenagem a Ada Lovelace, colaboradorade Charles Babbage).1983 O Ada torna-se uma norma ANSI.1987 O Ada torna-se uma norma ISO.
1988 Comea o projecto Ada 9X.1995 Ada 95 adoptado como norma ISO e ANSI.1996 O fogueto europeu Ariane 5 explode, pouco
depois do lanamento, devido a um bug no seu
software Ada :-(
-
8/7/2019 Ada_OOP97
6/33
2001.09.24 Ada 95 6
O Ada na Internet
Produtos Tutoriais
Manual de referncia hypertext on-line.
Notcias Factos
FAQs
Livros Compiladores
Links
http://www.adahome.com/
-
8/7/2019 Ada_OOP97
7/33
2001.09.24 Ada 95 7
O Ada (83) no orientado pelosobjectos
No tem:
classes
herana
polimorfismo
Mas tem: pacotes
tipos derivados
pacotes genricos
Mesmo assim, serviupara desenvolversoftware orientadopelos objectos.
-
8/7/2019 Ada_OOP97
8/33
2001.09.24 Ada 95 8
Um pacote no uma classeUma classe um tipo; um pacote uma coleco derecursos.
class Point
{
...
}
package Geometry is
type Point is ...
type Line is ...
type Figure is ...
procedure Rotate(...);
end Geometry;class Line
{
...
}
Em C++, cada classe um mdulo, e cada
classe um tipo:
Em Ada, um mdulo um pacote. Num pacote
pode haver vrios tipos:
-
8/7/2019 Ada_OOP97
9/33
2001.09.24 Ada for Some of Us by Pedro Guerreiro 9
Um pacote pode ser um objecto
package Car_Plate is
subtype Car_Plate_Type is String(1..8);
procedure Advance;
procedure Advance(How_Many: in Natural);procedure Advance_To_Next_Letters;function Current return Car_Plate_Type;
No_More_Plates: exception;
end Car_Plate;
Por exemplo, um pacote para atribuir matrculas para vecu-los, em Portugal (saltando o K, W, Y). O pacote tem estadoeoperaes (para avanar e para obter a matrcula corrente):
...Text_IO.Put(Car_Plate.Current);
Car_Plate.Advance;...
Exemplo de utilizao
-
8/7/2019 Ada_OOP97
10/33
2001.09.24 Ada 95 10
Com um pacote genrico podemoscriar vrios objectos do mesmo tipo
Exemplo: Uma tabela genrica:
genericSize: in Positive;
type Item_Type is private;
package Search_Table is
procedure Add(Item: in Item_Type);function Item(Index: Positive) return Item_Type;
function Has(Item: in Item_Type) return Boolean;
function Empty return Boolean;
function Full return Boolean;
function Length return Natural;
Table_Overflow: exception;
Illegal_Index: exception;
end Search_Table;
-
8/7/2019 Ada_OOP97
11/33
2001.09.24 Ada 95 11
Instanciando um pacote genricoExemplo: uma tabela de nmeros de telefone(representados por Unbounded_String):
package Telephones isnew Search_Table(Size => 256,
Item_Type => Unbounded_String);
Utilizao:Telephones.Add(To_Unbounded_String("0936 828944"));...Telephones.Add(To_Unbounded_String(01 8406251"));
loopPrompts.Prompt_Unbounded_String(My_Telephones,
"Um telefone, sff: ");exit when My_Telephones = Null_Unbounded_String;if Telephones.Has(My_Telephones) then
Ada.Text_IO.Put_Line("Sim");else
Ada.Text_IO.Put_Line("Nao");
end if;end loop;
-
8/7/2019 Ada_OOP97
12/33
2001.09.24 Ada 95 12
Programao orientada pelosobjectos em Ada 95
Ada 95 uma linguagem para a programao orientada
pelos objectos.
A programao orientada pelos objectos em Ada faz-se
com tipos abstractos, herana, extenso de tipos epolimorfismo.
Ada no tem classes como o C++, o Eiffel ou o Java.
Por outro lado, o Ada tem a programao classwide.
-
8/7/2019 Ada_OOP97
13/33
2001.09.24 Ada 95 13
Herana
type D is new P;
A herana consegue-se atravs dos tipos derivados (que vmdo Ada 83):
O tipo derivado herda automaticamente as operaes
primitivasdo tipo base.
As operaes primitivas de um tipo T so aquelas declaradasna parte visvel do pacote em que T declarado, e que tmum parmetro, ou o resultado, de tipo T.
-
8/7/2019 Ada_OOP97
14/33
2001.09.24 Ada 95 14
Uma hierarquia de heranaVejamos uma pequena hierarquia de herana:
Currency
MarkDollar
-
8/7/2019 Ada_OOP97
15/33
2001.09.24 Ada 95 15
O tipo de baseO pacote Currencies declara o tipo de base Currency_Type ealgumas operaes:with Ada.Text_IO;
with Ada.Text_IO.Editing;
package Currencies istype Currency_Type is
delta 0.01 digits 18 range 0.0 .. 9_999_999_999_999_999.99;
package Currency_Output isnew Ada.Text_IO.Editing.Decimal_Output(Currency_Type);
package Currency_IO isnew Ada.Text_IO.Decimal_IO(Currency_Type);
procedure Get(Amount: out Currency_Type);
procedure Get(File: in Ada.Text_IO.File_Type;Amount: out Currency_Type);procedure Put(Amount: in Currency_Type; Width: in Integer);procedure Put(File: in Ada.Text_IO.File_Type;
Amount: in Currency_Type; Width: in Integer);...
-
8/7/2019 Ada_OOP97
16/33
2001.09.24 Ada 95 16
(continuao)
As operaes que tm um argumento ou resultado de tipoCurrency so primitivas. Mais tarde, quando declararmos:
...
function Currency_Symbol return String;
function "*"(Left: Currency_Type; Right: Float)return Currency_Type;
function "*"(Left: Currency_Type; Right: Integer)
return Currency_Type;
end Currencies;
type Mark_Type is new Currency_Type;
todas estas operaes ficam disponveis para argumentos detipo Mark_Type. No entanto, pode ser que algumas tenham
que ser redefinidas.
-
8/7/2019 Ada_OOP97
17/33
2001.09.24 Ada 95 17
Operaes no tipo de base (1)
with Ada.Strings.Fixed;
package body Currencies is
procedure Get(Amount: out Currency_Type) isbegin
Get(Ada.Text_IO.Current_Input, Amount);end Get;
procedure Get(File: in Ada.Text_IO.File_Type;Amount: out Currency_Type) is
beginCurrency_IO.Get(File, Amount);
end Get;
procedure Put(Amount: in Currency_Type; Width: in Integer) isbegin
Put(Ada.Text_IO.Current_Output, Amount, Width);end Put;
...
Estas operaes existiro para todos os tipos derivados deCurrency_Type, ainda que algumas sejam redefinidas:
-
8/7/2019 Ada_OOP97
18/33
2001.09.24 Ada 95 18
Operaes no tipo de base (2)...procedure Put(File: in Ada.Text_IO.File_Type;
Amount: in Currency_Type;Width: in Integer) is
use Ada.Strings.Fixed;begin
Currency_Output.Put(File,
Amount,Ada.Text_IO.Editing.To_Picture((Width - 4) * 'z' & "9.99"));
end Put;
function Currency_Symbol return String isbegin
return "";end Currency_Symbol;
function "*"(Left: Currency_Type; Right: Float)return Currency_Type is
beginreturn Currency_Type(Float(Left) * Right);
end "*";
function "*"(Left: Currency_Type; Right: Integer)return Currency_Type is
beginreturn Currency_Type(Float(Left) * Float(Right));
end "*";
end Currencies;
-
8/7/2019 Ada_OOP97
19/33
2001.09.24 Ada 95 19
Tipos derivados
package Currencies.Dollars is
type Dollar_Type is new Currency_Type;
function Currency_Symbol return String;
procedure Put(Amount: in Dollar_Type; Width: in Integer);procedure Put(File: in Ada.Text_IO.File_Type;
Amount: in Dollar_Type;
Width: in Integer);
end Currencies.Dollars;
O tipo Dollar_Type deriva de Currency_Type.
Estes dois procedimentos Put escondem os que so obtidos
por derivao.
-
8/7/2019 Ada_OOP97
20/33
2001.09.24 Ada 95 20
Programa examplowith Ada.Text_IO;with Currencies.Marks;
procedure Main_Marks is
package Marks renames Currencies.Marks;
use type Marks.Mark_Type;
Borrowed: Marks.Mark_Type;
Pay_Back: Marks.Mark_Type;
Tax: Constant := 0.05125;begin
Ada.Text_IO.Put("Quantos pediste? ");
Marks.Get(Borrowed);
Pay_Back := Borrowed + Borrowed * Tax;
Ada.Text_IO.Put("Pediste: ");Marks.Put(Borrowed, 10);
Ada.Text_IO.New_Line;
Ada.Text_IO.Put("Vais ter que pagar: ");
Marks.Put(Pay_Back, 10);
Ada.Text_IO.New_Line;end Main_Marks;
-
8/7/2019 Ada_OOP97
21/33
2001.09.24 Ada 95 21
Extenso de tiposPor vezes, ao derivar um tipo, queremos extend-lo comnovos campos. Eis uma hierarquia de tipos derivados de
Person, na qual cada tipo derivado extende o tipo de base:
Tax_Payer
Person
Citizen Friend
Voter
Emigrant Immigrant
-
8/7/2019 Ada_OOP97
22/33
2001.09.24 Ada 95 22
Tipos marcados (tagged types)with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;with Ada.Text_IO;
package Persons is
type Person_Type is tagged private;type Gender_Type is (Male, Female, Unknown);
package Gender_IO isnew Ada.Text_IO.Enumeration_IO(Gender_Type);
procedure Make(Person: out Person_Type;Name: in Unbounded_String;Gender: in Gender_Type);
function Person_Of(Name: in Unbounded_String; Gender: in Gender_Type)return Person_Type'Class;
function Name_Of(Person: in Person_Type) return Unbounded_String;function Gender_Of(Person: in Person_Type) return Gender_Type;procedure Display(Person: in Person_Type);
private...
end Persons;
Uma pessoa tem nome e gnero:
-
8/7/2019 Ada_OOP97
23/33
2001.09.24 Ada 95 23
Registos marcados (tagged records)
with ...
package Persons is
...private
type Person_Type is tagged
record
Name: Unbounded_String;
Gender: Gender_Type;end record;
end Persons;
Cada valor de um tipo marcado tem uma marca que identificao tipo. S tipos marcados (tagged) podem ser extendidos:
-
8/7/2019 Ada_OOP97
24/33
2001.09.24 Ada 95 24
Os tipos marcados no so classes
Only tagged types can be extended:
with My.Strings;
with Ada.Text_IO;
package body Persons is
Gender_Letters: constant array(Gender_Type) of Character :=
(Male => 'M', Female => 'F', Unknown => 'U');
procedure Make(Person: out Person_Type;
Name: in Unbounded_String;Gender: in Gender_Type) is
begin
Person := Person_Type'(Name, Gender);
end Make;
function Person_Of(Name: in Unbounded_String; Gender: in Gender_Type)return Person_Type'Class is
begin
return Person_Type'(Name, Gender);
end Person_Of;
...
-
8/7/2019 Ada_OOP97
25/33
2001.09.24 Ada 95 25
(continuao)...function Name_Of(Person: in Person_Type) return Unbounded_String is
begin
return Person.Name;
end Name_Of;
function Gender_Of(Person: in Person_Type) return Gender_Type is
begin
return Person.Gender;
end Gender_Of;
procedure Display(Person: in Person_Type) is
beginMy.Strings.Put(Person.Name);
Ada.Text_IO.Put(" (" & Gender_Letters(Person.Gender) & ")");
end Display;
end Persons;
-
8/7/2019 Ada_OOP97
26/33
2001.09.24 Ada 95 26
Estendendo um tipo marcadoUm cidado uma pessoa cuja data de nascimento enacionalidade so conhecidos e que tem um nmero deidentificao:
with Ada.Calendar;
package Persons.Citizens is
type Citizen_Type is new Person_Type with private;
...
private
type Citizen_Type is new Person_Type with
recordBirth_Date: Ada.Calendar.Time;
Nationality: Unbounded_String;
Identification: Unbounded_String;
end record;
end Persons.Citizens;
-
8/7/2019 Ada_OOP97
27/33
2001.09.24 Ada 95 27
Subprogramas no tipo derivadoOs subprogramas disponveis para o tipo derivado (estendido)so os herdados mais os que declararmos agora:package Persons.Citizens is
...procedure Make(Citizen: out Citizen_Type;
Person: in Person_Type;Nationality: in Unbounded_String;Birth_Date: in Ada.Calendar.Time;Identification: in Unbounded_String);
function Citizen_Of(Person: Person_Type;Nationality: Unbounded_String;Birth_Date: in Ada.Calendar.Time;Identification: Unbounded_String)
return Citizen_Type'Class;function Nationality_Of(Citizen: Citizen_Type) return Unbounded_String;
function Birth_Date_Of(Citizen: Citizen_Type) return Ada.Calendar.Time;function Identification_Of(Citizen: Citizen_Type)
return Unbounded_String;procedure Display(Citizen: in Citizen_Type);function Older_Than(Left: Citizen_Type'Class;
Right: Citizen_Type'Class) return Boolean;
...
-
8/7/2019 Ada_OOP97
28/33
2001.09.24 Ada 95 28
Programao classwideConsideremos uma funo Older_Than para comparar aidade de dois cidados. A funo deve poder ser aplicada adois argumentos de tipo Citizen_Type ou de qualquer tipoderivado de Citizen_Type. O tipo Citizen_TypeClassrepresenta o tipo classwideformado por todos os valores dostipos da hierarquia de Citizen_Type:
package body Persons.Citizens is
...
function Older_Than(Left: Citizen_Type'Class;Right: Citizen_Type'Class) return Boolean is
use type Ada.Calendar.Time;beginreturn Left.Birth_Date
-
8/7/2019 Ada_OOP97
29/33
2001.09.24 Ada 95 29
Agregados de extensoPara construir um valor do tipo derivado a partir de um valordo tipo de base e dos valores dos novos componentes usa-se
um agregado de extenso:procedure Make(Citizen: out Citizen_Type;
Person: in Person_Type;
Nationality: in Unbounded_String;
Birth_Date: in Ada.Calendar.Time;
Identification: in Unbounded_String) is
begin
Citizen := Citizen_Type'(Person with
Birth_Date => Birth_Date,
Nationality => Nationality,
Identification => Identification);
end Make;
-
8/7/2019 Ada_OOP97
30/33
2001.09.24 Ada 95 30
Funes que devolvem tipos
classwidePara evitar dificuldades ao herdar funes que devolvem otipo de base, melhor faz-las derivar o respectivo tipo
classwide:function Citizen_Of(Person: Person_Type;
Nationality: Unbounded_String;Birth_Date: Ada.Calendar.Time;Identification: Unbounded_String)
return Citizen_Type'Class is
beginreturn Citizen_Type'(Person with
Birth_Date => Birth_Date,Nationality => Nationality,Identification => Identification);
end Citizen_Of;
function Person_Of(Name: in Unbounded_String;
Gender: in Gender_Type)
return Person_Type'Class is
begin
return Person_Type'(Name, Gender);
end Person_Of;
-
8/7/2019 Ada_OOP97
31/33
2001.09.24 Ada 95 31
Exemplo com tipos derivadoswith Persons;with Persons.Citizens;with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;with Ada.Text_IO;with Ada.Calendar;with my.Strings;
procedure Main_Citizens isCitizen: Persons.Citizens.Citizen_Type;Person: Persons.Person_Type;
begin
Persons.Citizens.Make(Citizen,
Persons.Person_Type(Persons.Person_Of(To_Unbounded_String("Miguel"), Persons.Male)),
To_Unbounded_String("Portuguese"),Ada.Calendar.Time_Of(1952, 12, 19),
To_Unbounded_String("FA/M/12786"));My.Strings.Put(Persons.Citizens.Name_Of(Citizen));Ada.Text_IO.New_Line;Persons.Gender_IO.Put(Persons.Citizens.Gender_Of(Citizen));Ada.Text_IO.New_Line;My.Strings.Put(Persons.Citizens.Nationality_Of(Citizen));
Ada.Text_IO.New_Line;end Main_Citizens;
-
8/7/2019 Ada_OOP97
32/33
2001.09.24 Ada 95 32
Derivando a partir de um tipo
derivadoAs hierarquias de herana podem ser arbitrariamenteprofundas:package Persons.Citizens.Tax_Payers is
type Fiscal_Number_Type is new Integer range 100_000_000..999_999_999;
type Tax_Payer_Type is new Citizen_Type with private;
procedure Make(...);function Tax_Payer_Of(...) return Tax_Payer_Type'Class;function Fiscal_Number_Of(...) return Fiscal_Number_Type;procedure Display(Tax_Payer: in Tax_Payer_Type);
private
type Tax_Payer_Type is new Citizen_Type withrecord
Fiscal_Number: Fiscal_Number_Type;end record;
end Persons.Citizens.Tax_Payers;
-
8/7/2019 Ada_OOP97
33/33
2001.09.24 Ada 95 33
Concluses