Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //PRINCIPAL.CPP
- #include <string>
- #include <iostream>
- #include <vector>
- #include "Libro.hpp"
- using namespace std;
- int main()
- {
- //Creacion de objeto de manera estatica
- CLibro Libro;
- cout << "Mostrando objeto de manera estatica" << endl;
- cout << Libro.getAutor() << endl << endl;
- //Creacion de objeto de manera dinamica
- cout << "Mostrando objeto de manera dinamica" << endl;
- CLibro*L1 = new CLibro();
- cout << L1->getAutor() << endl<<endl;
- ////////////////////////////////////////////////////////////
- //Creando coleccion de libros mediante STL-Vector
- //Los que usan * es dinamico jejeje
- //estatico(.)
- //dinamico (->)
- //vector estatico de objetos estaticos
- vector<CLibro> Libros1;
- Libros1.push_back(Libro);
- Libros1.push_back(*L1);
- //vector dinamico de objetos estaticos
- vector <CLibro> *Libros2;
- Libros2->push_back(Libro);
- Libros2->push_back(*L1);
- //vector estatico de objetos dinamicos
- vector <CLibro*> Libros3;
- Libros3.push_back(&Libro);
- Libros3.push_back(L1);
- //vector dinamicos de objetos dinamicos
- vector <CLibro*> *Libros4;
- Libros4->push_back(&Libro);
- Libros4->push_back(L1);
- cin.ignore();
- cin.get();
- return 0;
- }
- // int x=> valor
- // * x => direccion
- // x => valor
- // y=> direccion
- // int f z => referencia (valor en su propia direccion)
- ****Libro.hpp****
- #pragma once
- #ifndef _LIBRO_HPP-
- #define _LIBRO_HPP_
- #include <string>
- using namespace std;
- class CLibro
- {
- public:
- CLibro(string Titulo, string Autor, short Identificador, float Precio)
- {
- this->Titulo = Titulo;
- this->Autor = Autor;
- this->Identificador = Identificador;
- this->Precio = Precio;
- }
- CLibro()
- {
- Titulo = "Pato Yunque";
- Autor = "Cesar Vallejo";
- Identificador=1;
- Precio = 12.5f;
- }
- ~CLibro() {};
- string getTitulo() { return Titulo; };
- string getAutor() { return Autor; };
- short getIdentificador() { return Identificador; };
- float getPrecio() { return Precio; };
- void SetTitulo(string Titulo) { this->Titulo = Titulo; };
- void SetAutor(string Autor) { this->Autor = Autor; }
- void setIdentificador(short Identificador) { this->Identificador=Identificador; };
- void setPrecio(float Precio) { this->Precio = Precio; };
- private:
- string Titulo;
- string Autor;
- short Identificador;
- float Precio;
- };
- #endif // !_LIBRO_HPP-
- Ejemplo 1:
- #include <iostream>
- using std::cout;
- using std::endl;
- int suma(int x,int y)
- {
- return x + y;
- }
- float suma(float x, float y)
- {
- return x + y;
- }
- short suma(short x, short y)
- {
- return x + y;
- }
- /////////////////////////////////////////////
- template <typename G>
- G suma(G x, G y)
- {
- return x + y;
- }
- /////////////////////////////////////////////
- int main()
- {
- int x1 = 10, y1 = 20;
- float x2 = 10.2f, y2 = 20.2f;
- short x3 = 10, y3 = 20;
- cout << "enteros: " << suma(x1, y1)<<endl;
- cout << "flotantes: " << suma(x2, y2)<<endl;
- cout << "enteros corto: " << suma(x3, y3)<<endl;
- ///////////////////////////////////////////////////
- suma<int>(x1, y1);
- cout << "enteros: " << suma<int>(x1, y1) << endl;
- cout << "flotantes: " << suma<float>(x2, y2) << endl;
- cout << "enteros corto: " << suma<int>(x3, y3) << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement