Skip to content

Instantly share code, notes, and snippets.

@cms-codes
Created April 9, 2021 03:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cms-codes/821e93b13b07df30b19e4221ead698a4 to your computer and use it in GitHub Desktop.
Save cms-codes/821e93b13b07df30b19e4221ead698a4 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <assert.h>
class Animal
{
public:
double age;
};
class Pet
{
public:
std::string name;
};
// Dog derives from *both* Animal and Pet
class Dog : public Animal, public Pet
{
public:
std::string breed;
};
// Cat also derives from both Animal and Pet
// but the constructor assignment does not see its inherited fields
class Cat : public Animal, public Pet
{
public:
Cat(double age, std::string name, std::string color)
: age(age), name(name), color(color)
{}
std::string color;
};
int main()
{
Cat cat(10, "Max", "black");
assert(cat.color == "black");
assert(cat.age == 10);
assert(cat.name == "Max");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment