Történik itt memóriaelszivárgás?
//Szintaxal: [link]
//main.cpp
# include <iostream> //std::cout,std::endl,std::cin,std::cin.get
# include <string> //std::string,strcpy,strncpy
# include <cstring> //strlen
# include <stdlib.h> //# define EXIT_SUCCESS 0
# include <algorithm> //std::sort
typedef unsigned short ushort; //Szinoníma készítése az unsigned short-ra.
typedef unsigned int uint; //Szinoníma készítése az unsigned int-re.
using std::cout; //std::cout névtér be emelése a jelenlegi névtérbe.
using std::cin; //std::cin névtér be emelése a jelenlegi névtérbe.
using std::endl; //std::endl névtér be emelése a jelenlegi névtérbe.
using std::string; //std::string névtér be emelése a jelenlegi névtérbe.
using std::sort; //std::sort névtér be emelése a jelenlegi névtérbe.
enum BREED {TERRIER,HASKI,PINCSI,TACSKO};
//Mammal
class Mammal
{
public:
Mammal();
Mammal(ushort age,ushort weight);
Mammal(const Mammal &rhs);
~Mammal();
const Mammal& operator=(const Mammal &rhs);
const Mammal& operator++(); //prefix
const Mammal operator++(int); //postfix
const Mammal operator+(const Mammal &rhs);
void SetAge(ushort age) {*itsAge = age;}
void SetWeight(ushort weight) {*itsWeight = weight;}
ushort GetAge()const {return *itsAge;}
ushort GetWeight()const {return *itsWeight;}
void Move()const {cout <<"Mammal odebb lepett \n";}
void Move(ushort move)const {cout <<"Mammal "<< move <<" lepett \n";}
void Sleep()const {cout <<"Mammal alszik \n";}
void Eat()const {cout <<"Mammal eszik \n";}
void Speak()const {cout <<"Mammal beszel \n";}
protected:
ushort *itsAge;
ushort *itsWeight;
private:
};
class Dog: public Mammal
{
public:
Dog();
Dog(ushort age,ushort weight,BREED breed);
Dog(const Dog &rhs);
~Dog();
const Dog& operator=(const Dog &rhs);
void SetBreed(BREED breed) {*itsBreed = breed;}
BREED GetBreed()const {return *itsBreed;}
void BegForFood()const {cout <<"Kajaert kuncsorgas \n";}
void Tail()const {cout <<"Farokcsovalas \n";}
void Speak()const {cout <<"Vau,vau \n";}
void Move()const {cout <<"A kutya lepett egyet \n";}
protected:
BREED *itsBreed;
private:
};
Mammal::Mammal():
itsAge(new ushort(0)),
itsWeight(new ushort(0))
{cout <<"Mammal Konstruktor \n";}
Mammal::Mammal(ushort age,ushort weight = 0):
itsAge(new ushort(age)),
itsWeight(new ushort(weight))
{cout <<"Mammal (ushort,ushort) Konstruktor \n";}
Mammal::Mammal(const Mammal &rhs)
{
cout <<"Mammal Copy Konstruktor \n";
itsAge = new ushort;
itsWeight = new ushort;
*itsAge = rhs.GetAge();
*itsWeight = rhs.GetWeight();
}
Mammal::~Mammal()
{
cout <<"Mammal Destruktor \n";
delete itsAge;
delete itsWeight;
itsAge = NULL;
itsWeight = NULL;
}
const Mammal& Mammal::operator=(const Mammal &rhs)
{
if(this==&rhs)
return *this;
delete itsAge;
delete itsWeight;
itsAge = new ushort;
itsWeight = new ushort;
*itsAge = rhs.GetAge();
*itsWeight = rhs.GetWeight();
return *this;
}
const Mammal& Mammal::operator++()
{
cout <<"Mammal Operator++ prefix \n";
++(*itsAge);
return *this;
}
const Mammal Mammal::operator++(int)
{
cout <<"Mammal Operator++ postfix \n";
const Mammal Temp(*this);
++(*itsAge);
return Temp;
}
const Mammal Mammal::operator+(const Mammal &rhs)
{
cout <<"Mammal Operator+ \n";
return Mammal(*itsAge+rhs.GetAge(),*itsWeight+rhs.GetWeight());
}
Dog::Dog():
Mammal(),
itsBreed(new BREED(PINCSI))
{cout <<"Dog Konstruktor \n";}
Dog::Dog(ushort age,ushort weight,BREED breed):
Mammal(age,weight),
itsBreed(new BREED(breed))
{cout <<"Dog (ushort,ushort,BREED) Konstruktor \n";}
Dog::Dog(const Dog &rhs)
{
cout <<"Dog Copy Konstruktor \n";
itsBreed = new BREED;
*itsBreed = rhs.GetBreed();
}
Dog::~Dog()
{
cout <<"Dog Destruktor \n";
delete itsBreed;
itsBreed = NULL;
}
const Dog& Dog::operator=(const Dog &rhs)
{
cout <<"Dog Operator= \n";
if(this==&rhs)
return *this;
delete itsBreed;
itsBreed = new BREED;
*itsBreed = rhs.GetBreed();
return *this;
}
//main
int main()
{
Dog *family = new Dog[5];
family[0].Speak();
family[0].Move();
//family[0].Move(10); //Hiba! függvényfelülírás miatt a többi move elvan fedve.
family[0].Mammal::Move(10);
Dog &kormos = family[0];
cout <<"Kormos most "<< kormos.GetAge() <<" eves \n";
delete [] family;
return EXIT_SUCCESS;
}
Kapcsolódó kérdések:
Minden jog fenntartva © 2024, www.gyakorikerdesek.hu
GYIK | Szabályzat | Jogi nyilatkozat | Adatvédelem | Cookie beállítások | WebMinute Kft. | Facebook | Kapcsolat: info(kukac)gyakorikerdesek.hu
Ha kifogással szeretne élni valamely tartalommal kapcsolatban, kérjük jelezze e-mailes elérhetőségünkön!