Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Animal.h
- #include <iostream>
- #include <string>
- using namespace std;
- class Animal
- {
- private:
- string animal;
- public:
- Animal(){}
- Animal(string animal)
- {
- this->animal = animal;
- }
- void move()
- {
- cout<<"\n\tEvery animal moves.";
- }
- };
- Dog.h
- #include <iostream>
- #include <string>
- #include "Animal.h"
- using namespace std;
- class Dog:public Animal
- {
- private:
- string dog
- ;
- public:
- Dog(string dog):Animal()
- {
- this->dog=dog;
- }
- void move()
- {
- cout<<"\n\tDog moves running.\n";
- }
- };
- Main.cpp
- #include <iostream>
- #include <string>
- #include "Dog.h"
- using namespace std;
- int main()
- {
- Animal anyAnimal("Jery");
- Dog myDog("Tom");
- Animal &anyAnimalRef = anyAnimal;
- Dog &myDogRef = myDog;
- cout<<"\nCalling function move() for objects";
- anyAnimal.move();
- myDog.move();
- cout<<"\nCalling function move() with references";
- anyAnimalRef.move();
- myDogRef.move();
- cout<<"\nCalling function move() with the changed reference";
- anyAnimalRef = myDog;
- anyAnimalRef.move();
- cout<<"\n\n Calling function move() with base-class reference to derived-class object";
- Animal &anyAnimalRef2 = myDog;
- anyAnimalRef2.move();
- cout<<endl;
- cout<<endl;
- system ("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement