Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. //PRINCIPAL.CPP
  2. #include <string>
  3. #include <iostream>
  4. #include <vector>
  5. #include "Libro.hpp"
  6.  
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11. //Creacion de objeto de manera estatica
  12. CLibro Libro;
  13. cout << "Mostrando objeto de manera estatica" << endl;
  14. cout << Libro.getAutor() << endl << endl;
  15. //Creacion de objeto de manera dinamica
  16. cout << "Mostrando objeto de manera dinamica" << endl;
  17. CLibro*L1 = new CLibro();
  18. cout << L1->getAutor() << endl<<endl;
  19. ////////////////////////////////////////////////////////////
  20. //Creando coleccion de libros mediante STL-Vector
  21. //Los que usan * es dinamico jejeje
  22. //estatico(.)
  23. //dinamico (->)
  24.  
  25. //vector estatico de objetos estaticos
  26. vector<CLibro> Libros1;
  27. Libros1.push_back(Libro);
  28. Libros1.push_back(*L1);
  29.  
  30. //vector dinamico de objetos estaticos
  31. vector <CLibro> *Libros2;
  32. Libros2->push_back(Libro);
  33. Libros2->push_back(*L1);
  34.  
  35. //vector estatico de objetos dinamicos
  36. vector <CLibro*> Libros3;
  37. Libros3.push_back(&Libro);
  38. Libros3.push_back(L1);
  39.  
  40. //vector dinamicos de objetos dinamicos
  41. vector <CLibro*> *Libros4;
  42. Libros4->push_back(&Libro);
  43. Libros4->push_back(L1);
  44.  
  45. cin.ignore();
  46. cin.get();
  47. return 0;
  48. }
  49.  
  50.  
  51. // int x=> valor
  52. // * x => direccion
  53. // x => valor
  54. // y=> direccion
  55. // int f z => referencia (valor en su propia direccion)
  56.  
  57.  
  58.  
  59. ****Libro.hpp****
  60. #pragma once
  61. #ifndef _LIBRO_HPP-
  62. #define _LIBRO_HPP_
  63. #include <string>
  64. using namespace std;
  65. class CLibro
  66. {
  67. public:
  68. CLibro(string Titulo, string Autor, short Identificador, float Precio)
  69. {
  70. this->Titulo = Titulo;
  71. this->Autor = Autor;
  72. this->Identificador = Identificador;
  73. this->Precio = Precio;
  74. }
  75. CLibro()
  76. {
  77. Titulo = "Pato Yunque";
  78. Autor = "Cesar Vallejo";
  79. Identificador=1;
  80. Precio = 12.5f;
  81. }
  82. ~CLibro() {};
  83. string getTitulo() { return Titulo; };
  84. string getAutor() { return Autor; };
  85. short getIdentificador() { return Identificador; };
  86. float getPrecio() { return Precio; };
  87. void SetTitulo(string Titulo) { this->Titulo = Titulo; };
  88. void SetAutor(string Autor) { this->Autor = Autor; }
  89. void setIdentificador(short Identificador) { this->Identificador=Identificador; };
  90. void setPrecio(float Precio) { this->Precio = Precio; };
  91.  
  92.  
  93. private:
  94. string Titulo;
  95. string Autor;
  96. short Identificador;
  97. float Precio;
  98.  
  99. };
  100. #endif // !_LIBRO_HPP-
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107. Ejemplo 1:
  108. #include <iostream>
  109. using std::cout;
  110. using std::endl;
  111. int suma(int x,int y)
  112. {
  113. return x + y;
  114. }
  115. float suma(float x, float y)
  116. {
  117. return x + y;
  118. }
  119. short suma(short x, short y)
  120. {
  121. return x + y;
  122. }
  123. /////////////////////////////////////////////
  124. template <typename G>
  125. G suma(G x, G y)
  126. {
  127. return x + y;
  128. }
  129. /////////////////////////////////////////////
  130. int main()
  131. {
  132. int x1 = 10, y1 = 20;
  133. float x2 = 10.2f, y2 = 20.2f;
  134. short x3 = 10, y3 = 20;
  135. cout << "enteros: " << suma(x1, y1)<<endl;
  136. cout << "flotantes: " << suma(x2, y2)<<endl;
  137. cout << "enteros corto: " << suma(x3, y3)<<endl;
  138. ///////////////////////////////////////////////////
  139. suma<int>(x1, y1);
  140. cout << "enteros: " << suma<int>(x1, y1) << endl;
  141. cout << "flotantes: " << suma<float>(x2, y2) << endl;
  142. cout << "enteros corto: " << suma<int>(x3, y3) << endl;
  143. system("pause");
  144. return 0;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement