| Registro em Pascal |
type
TRegistro= record
Campo0: TipoX;
Campo1: TipoY;
...
CampoN: TipoZ;
end;
|
| CPF |
type
TCPF= record
NomeCompleto : String;
DataNascimento : String;
NumeroInscricao: String;
end;
|
| CPF |
// Classe type TCPF= class NomeCompleto : String; DataNascimento : String; NumeroInscricao: String; end; // Objeto da classe TCPF var CPF: TCPF; |
| Criando o objeto CPF |
CPF:= TCPF.Create;
|
| Hereditariedade |
type // Classe base TCPF= class NomeCompleto : String; DataNascimento : String; NumeroInscricao: String; end; // Classe derivada TCPFAtual= class (TCPF) DataEmissao: String; end; |
| CPF atual |
type
TCPFAtual= class
NomeCompleto : String;
DataNascimento : String;
NumeroInscricao: String;
DataEmissao : String;
end;
|
| Atributos e métodos |
interface
type
TCPF= class
NomeCompleto : String; // Atributo
DataNascimento : String; // Atributo
NumeroInscricao: String; // Atributo
function Formatar: String; // Método
end;
implementation
// Ex.: pra '00000000191' seria retornado '000.000.001-91'
function TCPF.Formatar: String;
begin
if Length (NumeroInscricao) = 11 then
Result:= Copy (NumeroInscricao, 01, 3) + '.' +
Copy (NumeroInscricao, 04, 3) + '.' +
Copy (NumeroInscricao, 07, 3) + '-' +
Copy (NumeroInscricao, 10, 2)
else
Result:= '';
end;
|
| Escopo de atributos e métodos |
type
TCPF= class
protected // Acesso restrito
NomeCompleto : String;
DataNascimento : String;
NumeroInscricao: String;
public // Acesso público
function Formatar: String;
end;
|
| Sobrecarga de métodos |
type
TCPF= class
protected
NomeCompleto : String;
NumeroInscricao: String;
DataNascimento : String;
public
constructor Create (ANumeroInscricao, ANomeCompleto, ADataNascimento: String);
function ObterAtributo (AAtributo: Integer): String; virtual;
end;
TCPFAtual= class (TCPF)
protected
DataEmissao: String;
public
constructor Create (ANumeroInscricao, ANomeCompleto, ADataNascimento, ADataEmissao: String);
function ObterAtributo (AAtributo: Integer): String; override;
end;
|
| CPF.dpr |
program CPF;
uses Forms, Principal in 'Principal.pas' {FormPrincipal}, ClasseCPF in 'ClasseCPF.pas';
begin
Application.Initialize;
Application.CreateForm (TFormPrincipal, FormPrincipal);
Application.Run;
end.
|
| Principal.pas |
unit Principal;
interface
uses Windows, Forms;
type
TFormPrincipal= class (TForm)
procedure FormCreate (Sender: TObject);
end;
var
FormPrincipal: TFormPrincipal;
implementation
uses ClasseCPF;
{$R *.dfm}
procedure TFormPrincipal.FormCreate (Sender: TObject);
var
CPF : TCPF;
CPFAtual: TCPFAtual;
begin
CPF := TCPF.Create ('00000000191', 'Nosferatus da Silva', '6/6/6');
CPFAtual := TCPFAtual.Create ('00000000191', 'Nosferatus da Silva', '6/6/6', '9/9/9');
FormPrincipal.Caption:= CPF.Formatar + ' ' + CPF.ObterAtributo (1) + ' ' + CPFAtual.ObterAtributo (3);
CPF.Free;
CPFAtual.Free;
end;
end. |
| ClasseCPF.pas |
unit ClasseCPF;
interface
type
TCPF= class
protected
NomeCompleto : String;
NumeroInscricao: String;
DataNascimento : String;
public
constructor Create (ANumeroInscricao, ANomeCompleto, ADataNascimento: String);
function ObterAtributo (AAtributo: Integer): String; virtual;
function Formatar: String;
end;
TCPFAtual= class (TCPF)
protected
DataEmissao: String;
public
constructor Create (ANumeroInscricao, ANomeCompleto, ADataNascimento, ADataEmissao: String);
function ObterAtributo (AAtributo: Integer): String; override;
end;
implementation
constructor TCPF.Create (ANumeroInscricao, ANomeCompleto, ADataNascimento: String); begin NomeCompleto := ANomeCompleto; NumeroInscricao:= ANumeroInscricao; DataNascimento := ADataNascimento; end;
function TCPF.ObterAtributo (AAtributo: Integer): String;
begin
case AAtributo of
0: Result:= NumeroInscricao;
1: Result:= NomeCompleto;
2: Result:= DataNascimento;
else
Result:= '';
end;
end;
// Ex.: pra '00000000191' seria retornado '000.000.001-91'
function TCPF.Formatar: String;
begin
if Length (NumeroInscricao) = 11 then
Result:= Copy (NumeroInscricao, 01, 3) + '.' +
Copy (NumeroInscricao, 04, 3) + '.' +
Copy (NumeroInscricao, 07, 3) + '-' +
Copy (NumeroInscricao, 10, 2)
else
Result:= '';
end;
constructor TCPFAtual.Create (ANumeroInscricao, ANomeCompleto, ADataNascimento, ADataEmissao: String); begin inherited Create (ANomeCompleto, ANumeroInscricao, ADataNascimento); DataEmissao:= ADataEmissao; end;
function TCPFAtual.ObterAtributo (AAtributo: Integer): String;
begin
if AAtributo = 3 then
Result:= DataEmissao
else
Result:= ObterAtributo (AAtributo);
end;
end. |

