Skip to content
Snippets Groups Projects
Commit 2a3aa83a authored by Karakas, Aleksandar's avatar Karakas, Aleksandar
Browse files

add code for inheritance

parent 2614aabe
No related branches found
No related tags found
No related merge requests found
Showing with 179 additions and 0 deletions
#include <cstdio>
#include <cstring>
// TODO: destructor (free memory allocated on the heap (for name_))
// TODO: copy constructor (implement deep copy)
class Pig {
char* name_; // longer names now supported (compared to `char name_[10];`)
unsigned age_;
public:
Pig(const char* name, unsigned age)
: name_{ new char[strlen(name) + 1] }, age_{age}
{
strcpy(name_, name);
}
void drink() { printf("%s drinks\n", name_); }
void bath() { printf("Bath! :D\n"); }
unsigned getAge() { return age_; }
void setAge(unsigned age) { age_ = age; }
};
class Cat { // not a pig
char* name_;
unsigned age_;
public:
Cat(const char* name, unsigned age)
: name_{ new char[strlen(name) + 1] }, age_{age}
{
strcpy(name_, name);
}
void drink() { printf("%s drinks\n", name_); }
void play() { printf("Play! :D\n"); } // cats play and don't take a bath
unsigned getAge() { return age_; }
void setAge(unsigned age) { age_ = age; }
};
int main()
{
Pig pig{ "Babe", 0 };
pig.bath();
pig.drink();
Cat cat{ "Grumpy", 7 };
cat.play();
// cat.bath(); // <-- error: no member named 'bath' in 'Cat' (Cats don't enjoy taking a bath – only pigs do)
cat.drink();
return 0;
}
# include "pet.hpp"
// TODO: add include guards (see pet.hpp for an example)
class Cat : public Pet {
public:
Cat(const char* name, unsigned age)
: Pet{ name, age } {}
Cat(const Cat& other) = delete;
~Cat() = default;
void play() { printf("%s plays!\n", name_); }
};
\ No newline at end of file
#include <cstdio>
#include "pig.hpp"
#include "cat.hpp"
int main()
{
Pig pig{ "Babe", 0 };
pig.drink();
pig.bath();
Cat cat{ "Grumpy", 0 };
cat.drink();
cat.play();
return 0;
}
\ No newline at end of file
#ifndef PET_H
#define PET_H
#include <cstring>
#include <cstdio>
class Pet {
unsigned age_;
protected:
char* name_;
public:
Pet(const char* name, unsigned age)
: age_{age}, name_{ new char[strlen(name) + 1] }
{
strcpy(name_, name);
}
Pet(const Pet& other) = delete;
~Pet() { delete[] name_; }
unsigned getAge() { return age_; }
void setAge(unsigned age) { age_ = age; }
void drink() { printf("%s drinks\n", name_); }
};
#endif
\ No newline at end of file
# include "pet.hpp"
// TODO: add include guards (see pet.hpp for an example)
class Pig : public Pet {
public:
// using Pet::Pet; // possible instead of defining the constructor for Pig. Using this line, Pig inherits Pet's constructor.
Pig(const char* name, unsigned age)
: Pet{ name, age } {}
Pig(const Pig& other) = delete;
~Pig() = default;
void bath() { printf("%s enjoys taking a bath!\n", name_); }
};
\ No newline at end of file
# include "pet.hpp"
// TODO: add include guards (see pet.hpp for an example)
class Cat : public Pet {
public:
Cat(const char* name, unsigned age)
: Pet{ name, age } {}
Cat(const Cat& other) = delete;
~Cat() = default;
void play() { printf("%s plays!\n", name_); }
};
#include <cstdio>
#include "pig.hpp"
#include "cat.hpp"
int main() {
Pet* pets[2]{
new Pig{ "Babe", 0 },
new Cat{ "Grumpy", 7}
};
for (auto pet : pets) {
pet->drink();
}
return 0;
}
#ifndef PET_H
#define PET_H
#include <cstring>
#include <cstdio>
class Pet {
unsigned age_;
protected:
char* name_;
public:
Pet(const char* name, unsigned age)
: age_{age}, name_{ new char[strlen(name) + 1] }
{
strcpy(name_, name);
}
Pet(const Pet& other) = delete;
~Pet() { delete[] name_; }
unsigned getAge() { return age_; }
void setAge(unsigned age) { age_ = age; }
// virtual void drink() = 0; // if we use this line instead of the next, Pet becomes an abstract class.
virtual void drink() { printf("%s drinks.\n", name_); }
};
#endif
\ No newline at end of file
# include "pet.hpp"
// TODO: add include guards (see pet.hpp for an example)
class Pig : public Pet {
public:
// using Pet::Pet; // possible instead of defining the constructor for Pig. Using this line, Pig inherits Pet's constructor.
Pig(const char* name, unsigned age)
: Pet{ name, age } {}
Pig(const Pig& other) = delete;
~Pig() = default;
void bath() { printf("%s enjoys taking a bath!\n", name_); }
void drink() override { printf("%s drinks water.\n", name_); }
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment