-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInheritance.cpp
More file actions
54 lines (39 loc) · 892 Bytes
/
Inheritance.cpp
File metadata and controls
54 lines (39 loc) · 892 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
class Animal{
public:
bool alive = true;
void eat()
{
std::cout << "This animal is eating\n";
}
};
class Dog : public Animal{
public:
void bark()
{
std::cout << "The dog goes woof!\n";
}
};
class Cat : public Animal{
public:
void meow()
{
std::cout << "The cat goes meow!\n";
}
};
int main()
{
//inheritance = A class can recieve attributes and methods from another class.
// Children classes inherit from a Parent class.
// Helps to reuse similar code found within multiple classes
Dog dog;
Cat cat;
std::cout << dog.alive << '\n';
dog.eat();
dog.bark();
std::cout << cat.alive << '\n';
cat.eat();
//cat.bark(); wont work,.bark() is something only the Dog class can do
cat.meow();
return 0;
}