sexta-feira, 22 de fevereiro de 2019

C++ builder - de metro para centímetros

Quando declaramos uma classe em C++,
por definição ela é privada,
isto significa que não podemos acessar,
nem suas variáveis membros,
nem suas funções membros através de outras
funções que não seja também membro da classe.
Uma classe pode possuir tantos membros privados
como públicos, isto é um modo de conseguirmos
encapsular os seus membros e funções,
para que determinados itens não sejam acessados
por outras funções que não convém.
Para tornar partes de uma classe públicas,
isto é, acessíveis a outras partes do seu programa,
você deve declará-las após a palavra reservada public.
Todas as variáveis ou funções definidas depois de public
estão disponíveis a todas as outras funções no programa.
Para mostrar na prática o uso de classes em C++,
declarei a classe Convert_Metros_Centimetro,
que está ápta a converter metros em centímetros.
Tanto para o valor inserido como para o lido,
foram utilizados dois objetos TEdit do C++ Builder.


//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "OnPaint.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//Declarando uma classe de nome: Convert_Metros_Centimetro
class Convert_Metros_Centimetro {
//Tornando as variáveis membros públicas
public:
const static int METRO = 1;
const static int CENTIMETRO = METRO * 100;
/*========================================================================*/
//Por fazer parte da classe Convert_Metros_Centimetro
//Dizemos que esta é uma função membro
int converter ( int metro, int centimetro ) {
return ( metro * centimetro );
}
};
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1 ( TComponent* Owner ) :
TForm ( Owner ) {
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Informe ( TObject *Sender ) {
Canvas -> Font -> Color = clBlack;
Canvas -> TextOut ( 230, 250, "Por: " );
Canvas -> Font -> Color = clRed;
Canvas -> TextOut ( 280, 250, "Samuel Lima" );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::OnPaint ( TObject *Sender ) {
Canvas -> Pen -> Width = 10;
Canvas -> Rectangle ( 05, 05, 595, 295 );
Canvas -> Font -> Size = 16;
Canvas -> Font -> Name = "alarm clock";
Canvas -> Font -> Color = clRed;
Canvas -> TextOut ( 80, 20, "C++ BUILDER - DE METRO PARA CENTIMETRO" );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click ( TObject *Sender ) {
Canvas -> Font -> Name = "Cosolas";
//Criando o objeto conv do tipo Convert_Metros_Centimetro
Convert_Metros_Centimetro conv;
int x = Edit1 -> Text.ToIntDef ( 0 );
int conversor = conv.converter ( x, conv.CENTIMETRO );
if ( x > 0 ) {
Edit2 -> Text = conversor;
} else {
Edit2 -> Clear ( );
}
Edit1 -> SetFocus ( );
Informe ( Sender );
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------

#ifndef OnPaintH
#define OnPaintH
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include <Vcl.ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TEdit *Edit1;
TEdit *Edit2;
TButton *Button1;
TLabel *Label1;
TLabel *Label2;
void __fastcall OnPaint(TObject *Sender);
void __fastcall Button1Click(TObject *Sender);
    void __fastcall Informe ( TObject *Sender );
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

object Form1: TForm1
  Left = 342
  Top = 219
  Width = 616
  Height = 338
  AutoScroll = True
  Caption = 'C++ BUILDER - CONVERSOR DE UNIDADE METRICA'
  Color = clSkyBlue
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  Position = poDesigned
  OnPaint = OnPaint
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 176
    Top = 114
    Width = 50
    Height = 13
    Caption = 'Metro ( s )'
  end
  object Label2: TLabel
    Left = 376
    Top = 114
    Width = 75
    Height = 13
    Caption = 'Cent'#237'metro ( s )'
  end
  object Edit1: TEdit
    Left = 144
    Top = 133
    Width = 121
    Height = 28
    Color = clOlive
    Font.Charset = ANSI_CHARSET
    Font.Color = clWindowText
    Font.Height = -19
    Font.Name = 'alarm clock'
    Font.Style = []
    ParentFont = False
    TabOrder = 0
  end
  object Edit2: TEdit
    Left = 352
    Top = 133
    Width = 121
    Height = 28
    Color = clOlive
    Font.Charset = ANSI_CHARSET
    Font.Color = clWindowText
    Font.Height = -19
    Font.Name = 'alarm clock'
    Font.Style = []
    ParentFont = False
    TabOrder = 1
  end
  object Button1: TButton
    Left = 288
    Top = 136
    Width = 42
    Height = 25
    Caption = 'Ok'
    TabOrder = 2
    OnClick = Button1Click
  end
end

Nenhum comentário:

Postar um comentário

Observação: somente um membro deste blog pode postar um comentário.