Zdrojáky - objektové 2/5
#include <iostream>
using namespace std;
class Zlomok
{
private:
int citatel;
int menovatel;
public:
void vypis()
{
cout << this->citatel << "/" << this->menovatel << endl;
}
//konstruktory
Zlomok(int citat, int menov)
{
this -> citatel =citat;
this -> menovatel=menov;
cout << "Konstruktor zlomku" << this->citatel <<","<< this->menovatel<<endl;
}
Zlomok(int x){this->citatel=this->menovatel=x ;}
Zlomok(){ this -> citatel=1; this -> menovatel =2; }
int getcitatel() { return this -> citatel; }
void setcitatel(int ci) { this -> citatel = ci; }
//copy konstruktor
Zlomok(Zlomok &zzz)
{
this -> citatel = zzz.citatel;
this -> menovatel = zzz.menovatel;
cout << "copy konstruktor" << endl;
}
//destruktor
~Zlomok()
{
cout << "destruktor zlomku " << this -> citatel <<"/"<< this -> menovatel << endl;
}
};
void main()
{
Zlomok z(3,4), z2(3), z1;
z.vypis();
z2.vypis();
z1.setcitatel(8);
z1.vypis();
Zlomok *zsmer;
zsmer=new Zlomok(3,5);
zsmer -> vypis();
delete zsmer;
Zlomok *kopiazl = new Zlomok(z);
}
#include<iostream>
#include<string>
using namespace std;
class OSOBA
{
private:
int vek;
float plat;
char *meno;
public:
void vypis() {cout << "Meno " << this -> meno << endl;}
OSOBA(char *_meno, int _vek)
{
this -> plat =0;
this -> vek = _vek;
this -> meno = new char[strlen(_meno)+1];
strcpy(this -> meno, _meno);
}
//destruktor
OSOBA() { delete[] this -> meno; }
//copy konstruktor
OSOBA (OSOBA & os)
{
this -> plat = os.plat;
this -> vek = os.vek;
char *ret = new char[strlen(os.meno)+5];
strcpy(ret,os.meno);
strcat(ret,"COPY");
this -> meno =new char[strlen(ret)+1];
strcpy(this -> meno, ret);}
};
void main()
{
OSOBA x("Katka",25);
x.vypis();
OSOBA *smerx=new OSOBA(x);
smerx -> vypis();
delete smerx;
}
#include <iostream>
using namespace std;
class Zlomok
{
private:
int citatel;
int menovatel;
static int pocet;
public:
void vypis() const
{
cout << this->citatel << "/" << this->menovatel << endl;
}
//konstruktory
Zlomok(int citat, int menov)
{
this -> citatel =citat;
this -> menovatel=menov;
cout << "Konstruktor zlomku" << this->citatel <<","<< this->menovatel<<endl;
cout << "Pocet objektov: "<< ++pocet << endl;
}
Zlomok(int x)
{
this->citatel=this->menovatel=x;
cout << "Pocet objektov: "<< ++pocet << endl;
}
Zlomok()
{
this -> citatel=1; this -> menovatel =2;
cout << "Pocet objektov: "<< ++pocet << endl;
}
int getcitatel() { return this -> citatel; }
void setcitatel(int ci) { this -> citatel = ci; }
//copy konstruktor
Zlomok(Zlomok &zzz)
{
this -> citatel = zzz.citatel;
this -> menovatel = zzz.menovatel;
cout << "copy konstruktor" << endl;
cout << "Pocet objektov: "<< ++pocet << endl;
}
//destruktor
~Zlomok()
{
cout << "destruktor zlomku " << this -> citatel <<"/"<< this -> menovatel << endl;
cout << "Pocet objektov: "<< --pocet << endl;
}
};
int Zlomok::pocet=0;
class TriedaX
{
public:
const Zlomok z;
TriedaX():z(3,2){}
};
void main()
{
Zlomok zz(1,8);
zz.setcitatel(5);
zz.vypis();
TriedaX tt;
tt.z.vypis();
}