Answer:
Following are the program in the C++ Programming Language.
//set header file
#include <iostream>
//set header file for string
#include <string>
//set namespace
using namespace std;
//define class
class AnimalData
{
//set access modifier
public:
//define function
void SetName(string givenName)
{
//initialize the value of function's argument
fullName = givenName;
};
//define function
void SetAge(int numYears)
{
//initialize the value of function's argument
ageYears = numYears;
};
//here is the solution
//define function
void PrintAll()
{
//print output with message
cout << "Name: " << fullName<<endl;
//print output with message
cout << "Age: " << ageYears<<endl;
};
//set access modifier
private:
//set integer data type variable
int ageYears;
//set string data type variable
string fullName;
};
//define class and inherit the parent class
class PetData: public AnimalData
{
//set access modifier
public:
//define function
void SetID(int petID)
{
idNum = petID;
};
//override the function for print id
void PrintAll()
{
AnimalData::PrintAll();
cout << "ID: " <<idNum ;
};
//set access modifier
private:
//set integer type variables
int idNum,fullName,ageYears;
};
//define main method
int main()
{
//create object of the child class
PetData userPet;
//call function through object
userPet.SetName("Fluffy");
//call function through object
userPet.SetAge (5);
//call function through object
userPet.SetID (4444);
//call function through object
userPet.PrintAll();
cout << endl;
return 0;
}
<u>Output</u>:
Name: Fluffy
Age: 5
ID: 4444
Explanation:
<u>Following are the description of the program</u>:
- Set required header file and namespace.
- Define class 'AnimalData' and inside the class, set integer data type variable 'ageYears', string data type variable 'fullName'.
- Then, define function 'SetName()' with string data type argument 'givenName' and initialize the value of argument in the string variable.
- Then, define function 'SetAge()' with integer data type argument 'numYears' and initialize the value of the argument in the integer variable.
- Define function 'PrintAll()' to print the value of the following variables with message.
- Define class 'PetData' and inherit the parent class in it, then set integer data type variable 'idNum'.
- Then, define function 'SetID()' with integer data type argument 'petID' and initialize the value of the argument in the integer variable.
- Override the function 'PrintAll()' to print the value of the following variable with message.
Finally, define main method and create the object of the child class, then call the following functions through the class object.