User: alciro    User
 Original    Translate to:  Deutsch  English  Français  中文  
 

Programación en C++ Builder

 Arrays (arrays)
 Pointers
3. Example of class in c + +
8. AnsiString class methods
 C + + projects
 Packages, distribute an application without installation
 Exchange or bubble sorting
 String string.h functions

3. Example of class in c + +

Although the creation of a class can be developed entirely in a single file, it is not advisable. The best option is to create a project with separate files for each application. The first has to create an application with the name class containing a Unit (file with source code) with the main program called Circulo.cpp. The second step is to create a new Unit to contain the development of the class, for example CCirculo.cpp. The class needs a header file to your statement which we call CCirculo.h.

Steps:

  1. Major Programme: New -> Other .. -> Console Wizard
  2. Unit Class: New -> Other .. -> Cpp File
  3. Header File: New -> Other .. -> Header File

If we have done all the steps we can see the files that are part of the project by clicking View-> Project Manager as shown in the figure below.

Project files (Project Manager)

Main program (Circulo.cpp)

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

# Include <vcl.h>
# Include <conio.h>
# Include <iostream.h>
# Include "CCirculo.h"

# Pragma hdrstop

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

# Pragma argsused
int main (int argc, char * argv []) {
   float dat
   CCirculo cir1, cir2, cir3 (10);

   court <<"Radius of circle 1";
   cin>> dat;
   cir1.radio (dat);
   court <<endl <<"diameter circle 2";
   cin>> dat;
   cir2.diametro (dat);

   court <<endl <<"Circle 1, Area =" <<cir1.area () <<"perimeter =" <<cir1.perimetro () <<endl;
   court <<endl <<"Circle 2, Area =" <<cir2.area () <<"perimeter =" <<cir2.perimetro () <<endl;
   court <<endl <<"Circle 3, Area =" <<cir3.area () <<"perimeter =" <<cir3.perimetro () <<endl;

   getch ();
   return 0;
}
//------------------------------------------------ --------------------------- 

Header file with class declaration (CCirculo.h)

 # Ifndef CCirculo_h
# Define CCirculo_h

{CCirculo class
public: / / Variables and functions of the kind of public sphere
	CCirculo (), / / ​​Default Constructor
	CCirculo (float) / / Constructor overloaded
	~ CCirculo () / / Destructor

	radio (float r) / / Function to set the radius of the circle
	diameter (float d) / / Function to set the diameter of the circle
	float area () / / Calculate the area of ​​the circle
	float perimeter () / / Calculate the perimeter of the circle

private: / / Variables and functions of the kind of private sphere
	float * rad;
};

# Endif 

Desrrollo file with the class (CCirculo.cpp)

 # Include "CCirculo.h"

# Define PI 3.14159265

/ / Default Constructor
CCirculo: CCirculo () {
	rad = new float;
	* Rad = 0;
}

/ / Constructor overloaded
CCirculo: CCirculo (float r) {
	rad = new float;
	* Rad = r;
}

/ / Destructor
CCirculo:: ~ CCirculo () {
	delete rad;
}

/ / Set the radius of the circle
CCirculo:: radio (float r) {
	* Rad = r;
}

/ / Set the diameter of the circle
CCirculo: diameter (float d) {
	* Rad = d / 2;
}

/ / Calculate the area of ​​the circle
CCirculo float:: area () {
	return Pi * (* rad) * (* rad);
}

/ / Compute the perimeter of the circle
CCirculo float:: perimeter () {
	return 2 * Pi * (* rad);
} 

After development of the kind we can use the Class Browser (CassExplorer) to navigate through its contents.

ClassExplorer gives us a tree view of the elements that make up the class. We can distinguish the variables of the functions, elements and public or private builders and the destroyer. By Class Explorer can perform a quick navigation by clicking on the different elements you access to your code.

Class structure (ClassExplorer)

When using the class and through any object of this is public access to items that have defined. The Builder as any IDE, provides support emerging class content as shown in the figure below.

Help tip of the objects in C + + Builder
Loading
copyright © 2007-2024  www.alciro.org  All rights reserved.         
Share |