add a copy and a "normal" constructor
This commit is contained in:
parent
ddec3de9b6
commit
f6d9636caa
1 changed files with 12 additions and 0 deletions
12
classes.cpp
12
classes.cpp
|
@ -3,6 +3,13 @@
|
||||||
|
|
||||||
class Animal {
|
class Animal {
|
||||||
public:
|
public:
|
||||||
|
Animal() {
|
||||||
|
std::cout << "constructor" << std::endl;
|
||||||
|
}
|
||||||
|
Animal(const Animal& other) {
|
||||||
|
std::cout << "copy constructor" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
// Note: using *virtual* does the difference here!
|
// Note: using *virtual* does the difference here!
|
||||||
virtual void makeSound() const {
|
virtual void makeSound() const {
|
||||||
std::cout << "<generic animal sound> from " << this << std::endl;
|
std::cout << "<generic animal sound> from " << this << std::endl;
|
||||||
|
@ -49,16 +56,21 @@ int main() {
|
||||||
std::cout << "== Some animals" << std::endl;
|
std::cout << "== Some animals" << std::endl;
|
||||||
Animal animal;
|
Animal animal;
|
||||||
animal.makeSound();
|
animal.makeSound();
|
||||||
|
|
||||||
|
std::cout << "== animal" << std::endl;
|
||||||
call(animal);
|
call(animal);
|
||||||
callref(animal);
|
callref(animal);
|
||||||
callptr(&animal);
|
callptr(&animal);
|
||||||
|
|
||||||
|
std::cout << "== cat" << std::endl;
|
||||||
animal = cat; // <- lol, operator
|
animal = cat; // <- lol, operator
|
||||||
animal.makeSound();
|
animal.makeSound();
|
||||||
call(cat);
|
call(cat);
|
||||||
callref(cat); // <- meow
|
callref(cat); // <- meow
|
||||||
|
callref(animal); // <- generic
|
||||||
callptr(&cat); // <- meow
|
callptr(&cat); // <- meow
|
||||||
|
|
||||||
|
std::cout << "== cow" << std::endl;
|
||||||
animal = cow; // <- lol, operator
|
animal = cow; // <- lol, operator
|
||||||
animal.makeSound();
|
animal.makeSound();
|
||||||
call(cow);
|
call(cow);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue