Beruflich Dokumente
Kultur Dokumente
*apuntador
*Aux1 = B y *Aux2 = B
char *aux1,*aux2;
ABB1234 “B”
“A”
...
aux1 *aux1*aux1
????
y *aux2
aux1 = new char;
aux2 = new char;
AFF1234
aux2 *aux2
*aux1='A';
????
aux2=aux1;
*aux1='B';
cout << "*Aux1 = " << *aux1 << " y *Aux2= " <<*aux2 <<endl;
delete aux1;
#include <cstdlib>
#include <iostream>
typedef int* pint; //definición de tipo apuntador int
// Subrutina
int a,b,c; //variables int void Uno(pint x,int y,int &z,pintw)
pint p1; //variable de tipo definido{(apuntador)
void Uno(pint x,int y,int &z,pint w); //declaración*x=*x
subrutina
* *x;
using namespace std; //directiva de C++ y= y * y ;
int main(int argc, char *argv[]) z= z * z ;
{ *w = *w * *w;
a= 2 ; };
b= 3 ;
c= 4 ;
p1=new int;
*p1=5;
Uno(&a,b,c,p1);
cout << "a: " << a << " b: " << b << " c: " << c << " d: " << *p1
<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
struct alumno {
string nombre;
int nota;
};
typedef alumno* PunteroAlumno;
int main() {
PunteroAlumno p,q;
p=new alumno;
q=new alumno;
p->nombre="Ana";
q->nombre="Jose";
p->nota=15;
q->nota=20;
cout << "Nombre: " << p->nombre << " Nota : " << p->nota << endl;
cout << "Nombre: " << q->nombre << " Nota : " << q->nota << endl;
q=p;
cout << "Nombre: " << p->nombre << " Nota : " << p->nota << endl;
cout << "Nombre: " << q->nombre << " Nota : " << q->nota << endl;
};
«Alumno.h»: «Alumno.cpp»:
class Alumno { Alumno::Alumno(string nombre, int nota){
private: this->nombre = nombre;
string nombre; this->nota = nota;
int nota; };
public: void Alumno::AsigNombre(string nom){
Alumno(){}; nombre=nom;
Alumno(string nombre, int nota); };
void AsigNombre(string nom); string Alumno::ObtNombre(){
string ObtNombre(); return nombre;
void AsigNota(int nota); };
int ObtNota(); void Alumno::AsigNota(int nota){
}; this->nota=nota;
};
int Alumno::ObtNota(){
return nota;
};
«main.cpp»:
#include <cstdlib>
#include <iostream>
#include "Alumno.h"
using namespace std;
int main(int argc, char *argv[]) {
Alumno *p, *q;
p=new Alumno();
q=new Alumno(.David.,12);
p->AsigNombre("Ana");
q->AsigNombre("Jose");
p->AsigNota(15);
q->AsigNota(20);
cout << "Nombre: " << p->ObtNombre() << " Nota : " << p->ObtNota() << endl;
cout << "Nombre: " << q->ObtNombre() << " Nota : " << q->ObtNota() << endl;
q=p;
cout << "Nombre: " << p->ObtNombre() << " Nota : " << p->ObtNota() << endl;
cout << "Nombre: " << q->ObtNombre() << " Nota : " << q->ObtNota() << endl;
}
struct tipopersona
{
string nombre;
tipopersona *hijo;
};
tipopersona *persona;
char resp='N';
persona = new(tipopersona);
cout << "Nombre: "; cin >> persona->nombre;
cout << "Tiene Hijo?"; cin >> resp;
if (resp=='S'){
persona->hijo=new(tipopersona);
cout << "Nombre del Hijo: "; cin >> persona->hijo->nombre;
persona->hijo->hijo=NULL;}
else
persona->hijo=NULL;
cout << "Datos del Padre :" << endl; cout << "Nombre: ";
cout << persona->nombre << endl; cout << "Datos del Hijo :" << endl;
if (persona->hijo==NULL)
cout << "No tiene hijos" << endl;
else {
cout << "Nombre: ";
cout << persona->hijo->nombre << endl;
delete persona->hijo;
};
delete persona;
}
Persona::Persona(string nom, int ced) {
nombre=nom;
cedula=ced;
«Persona.h»: };
class Persona { void Persona::AsigNombre(string nom) {
string nombre; nombre=nom;
int cedula; };
Persona* hijo; void Persona::AsigCedula(int ced){
public: cedula=ced;
Persona(){}; };
Persona(string nom, int ced); void Persona::AsigHijo(Persona *hij){
void AsigNombre(string nom); hijo=hij;
void AsigCedula(int ced); };
void AsigHijo(Persona *hij); string Persona::ObtNombre(){
string ObtNombre(); return nombre;
int ObtCedula(); };
Persona* ObtHijo(); int Persona::ObtCedula(){
}; return cedula;
};
Persona* Persona::ObtHijo(){
return hijo;
};
«main.cpp»:
int main(int argc, char *argv[]) {
Persona *persona;
char resp='N';
string nombre;
int cedula;
persona = new Persona();
cout << "Nombre: "; cin >> nombre;
persona->AsigNombre(nombre);
cout << "cedula: "; cin >> cedula;
persona->AsigCedula(cedula);
cout << "Tiene Hijo?"; cin >> resp;
if (resp=='S'){
cout << "Nombre del Hijo: "; cin >> nombre;
cout << "Cedula del Hijo: "; cin >> cedula;
persona->AsigHijo(new Persona(nombre,cedula));
persona->ObtHijo()->AsigHijo(NULL);}
else
persona->AsigHijo(NULL);
cout << "Datos del Padre :" << endl;
cout << "Nombre: ";
cout << persona->ObtNombre() << endl;
cout << "Cedula: ";
cout << persona->ObtCedula() << endl;
cout << "Datos del Hijo :" << endl;
if (persona->ObtHijo()==NULL)
cout << "No tiene hijos" << endl;
else {
cout << "Nombre del Hijo: ";
cout << persona->ObtHijo()->ObtNombre() << endl;
cout << "Cedula del Hijo: ";
cout << persona->ObtHijo()->ObtCedula() << endl;
delete persona->ObtHijo();
};
delete persona;
}