Question

In: Computer Science

Give an example of inheritance, you must have a parent and child and test program with...

Give an example of inheritance, you must have a parent and child and test program with at least one variable in each and setters and getters and constructors. The test program must test all the methods.

Solutions

Expert Solution

#include <iostream>
using namespace std;

class Person // base class
{
   private:
   string firstName;
   string lastName;
   string honorific;
  
   public:

//constructor
   Person(string firstName,string lastName,string honorific)
   {
       this->firstName = firstName;
       this->lastName = lastName;
       this->honorific = honorific;
   }

//set and get methods
   void setFirstName(string firstName)
   {
       this->firstName = firstName;
   }
   string getFirstName()
   {
       return firstName;
   }
   void setLastName(string lastName)
   {
       this->lastName = lastName;
   }
   string getLastName()
   {
       return lastName;
   }
   void setHonorific(string honorific)
   {
           this->honorific = honorific;
   }
   string getHonorific()
   {
       return honorific;
   }
   void display()
   {
       cout<<honorific<<" "<<firstName<<" "<<lastName;
   }
   ~Person() // destructor
   {
       cout<<"\ndestructor of person";
   }
  
};

class Student : public Person // derived class
{
   private:
   string courseName;
  
   public:
   Student(string firstName,string lastName,string honorific,string courseName):Person(firstName,lastName,honorific)
   {
       this->courseName = courseName;
   }
   void setCourseName(string courseName)
   {
       this->courseName = courseName;
   }
   string getCourseName()
   {
       return courseName;
   }
   void display()
   {
       Person::display();
       cout<<"\nStudying in course : "<<courseName;
   }
   ~Student()
   {
       cout<<"\ndestructor of student";
   }
};
int main() {
  
   Student st("Harris","Johnson","Dr.","Masters of Cardiology");
  
   st.display();
  
   return 0;
}

Output:

Dr. Harris Johnson
Studying in course : Masters of Cardiology
destructor of student
destructor of person

Do ask if any doubt. Please up-vote.


Related Solutions

explain the principle of the inheritance give an example( CS related) of class inheritance
explain the principle of the inheritance give an example( CS related) of class inheritance
How could you describe this a sensitive child to a parent that feels their child is...
How could you describe this a sensitive child to a parent that feels their child is just shy (a parent that overlooks their child's sensitive behavior)? Without telling the parent they are wrong about their child's behavior?
give an briefly explain 3 forms of inheritance, give an example of each?
give an briefly explain 3 forms of inheritance, give an example of each?
Design You will need to have at least four classes: a parent class, a child class,...
Design You will need to have at least four classes: a parent class, a child class, a component class, and an unrelated class. The component object can be included as a field in any of the other three classes. Think about what each of the classes will represent. What added or modified methods will the child class have? What added fields will the child class have? Where does the component belong? How will the unrelated class interact with the others?...
You are the parent of a child who is enrolled in an elementary school. You are...
You are the parent of a child who is enrolled in an elementary school. You are discussing the controversial topic of vaccination with another parent, who argues against it. How do you respond? Support all your assertions with evidence.
Write a “C” program(Linux) that creates a pipe and forks a child process. The parent then...
Write a “C” program(Linux) that creates a pipe and forks a child process. The parent then sends the following message on his side of the pipe “I am your daddy! and my name is <pid-of the-parent-process>\n”, the child receives this message and prints it to its stdout verbatim. The parent then blocks reading on the pipe from the child. The child writes back to its parent through its side ofthe pipe stating “Daddy, my name is <pid-of-the-child>”. The parent then...
7. What are the different forms of Inheritance? Give an example for each.
7. What are the different forms of Inheritance? Give an example for each.
Give an example of a situation when you should perform a paired T-test and an example...
Give an example of a situation when you should perform a paired T-test and an example of when you should do an unpaired comparison.
1a. Provide an example of a research question in which you must test if the mean...
1a. Provide an example of a research question in which you must test if the mean value of a variable is equal to its historical value and describe the null hypothesis.. 1b. Which field of business uses these methods the most?
If a child has an AB blood type, ____. a. one parent can have blood type...
If a child has an AB blood type, ____. a. one parent can have blood type O b. both parents must have different blood types c. both parents must have blood type AB d. both parents can be any blood type e. neither parent can have blood type AB
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT