Extras din notiță
Constructori si destructori
#include "conio.h"
#include "iostream"
using namespace std;
class complex
{private:
double re; double im;
public:
complex();
complex(double re);
complex(double re,double im);
~complex();
complex(complex &r);
void display(); };
complex::~complex(){ cout<<"distruge instanta"; }
complex::complex(complex &r)
{ complex::re=r.re;
complex::im=r.im;}
void complex::display(){ cout<<"partea reala: "<<re<<"tpartea imaginara: "<<im<<endl;}
complex::complex(){ complex::re=0; complex::im=0;}
complex::complex(double re){ complex::re=re; complex::im=0;}
complex::complex(double re,double im){complex::re=re; complex::im=im;}
int main(){ complex c1;
c1.display();
complex c2(5,1);
c2.display();
complex c3(1,2);
c3.display();
complex c4=c3;
complex c5(c2);
c4.display();
c5.display(); getch();}
Constructori de initializare,constructori de copiere si destructori cu alocare dinamica
#include "iostream"
#include"conio.h"
using namespace std;
class DStack{
int *pvect;
int size;
int tos;
public:
DStack(int s){
cout << "Constructor initializaren";
pvect = new int[s];
size = s;
tos = 0;}
DStack(DStack &r);
~DStack();
void push(int x);
int pop();};
DStack::DStack(DStack &r){
cout << "Constructor copieren";
size = r.size;
tos = r.tos;
pvect = new int[size];
for (int i=0; i< size; i++)
pvect[i] = r.pvect[i];}
DStack::~DStack(){cout << "Destructorn";
if (pvect){
delete pvect;
pvect = NULL;}}
DStack h(){
DStack stack(200);
return stack;}
int main(){h();
cout << "Revenire din h()n";
getch();}
Preview document
Conținut arhivă zip
- Clase, Clase Derivate, Constructori, Destructori - Programe POO.doc