Skip to content

Instantly share code, notes, and snippets.

@peta909
Created February 27, 2019 07:04
Show Gist options
  • Save peta909/fac507699397126069085863b8c66a52 to your computer and use it in GitHub Desktop.
Save peta909/fac507699397126069085863b8c66a52 to your computer and use it in GitHub Desktop.
#include "pch.h"
#include <iostream>
#include <string>
using namespace std;
//Parent Class
class Animal
{
public:
string name;
string food;
int legs;
Animal();
~Animal();
void Naming();
virtual void Eating();
};
//Child Class
class Cats : public Animal
{
public:
Cats();
~Cats();
int toys;
void Playing();
virtual void Eating();
};
//Constructor belonging to Animal class
Animal::Animal()
{
cout << "Animal class constructor called!" << endl;
name = "Default animal";
food = "Animal food";
legs = 4 ;
}
//Destructor belonging to Animal class
Animal::~Animal()
{
cout << "Animal class destroyer called!" << endl;
}
//Definition of member function Naming() in Animal class
void Animal::Naming() {
cout << "My name is " << name << endl;
}
void Animal::Eating() {
cout << "My food is " << food << endl;
}
//Constructor belonging to Cats class
Cats::Cats()
{
cout << "Cats class constructor called!" << endl;
name = "Cats";
food = "Cats food";
toys = 1;
}
//Destructor belonging to Cats class
Cats::~Cats()
{
cout << "Cats class destroyer called!" << endl;
}
//Definition of member function Playing() in Cat class
void Cats::Playing() {
cout << "I have " << toys << " toy(s)." << endl;
}
void Cats::Eating() {
cout << "i love to eat " << food << endl;
}
int main()
{
Animal Generic_Animal;
Animal* pGeneric_Animal = &Generic_Animal;
pGeneric_Animal->Naming();
pGeneric_Animal->Eating();
cout << "\n\n" << endl;
string Cat1food;
Cats* pCat1 = new Cats;
pCat1->name = "Garfield";
pCat1->food = "Fish";
pCat1->Naming();
pCat1->Playing();
pCat1->Eating();
getchar(); //added to prevent console from closing after code is executed.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment