Question

Define an example of inheritance with a base class that has 2 attributes, 2 derived classes...

Define an example of inheritance with a base class that has 2 attributes, 2 derived classes that each have 2 attributes. Implement this in C++. Create a driver program that creates instances of both of the derived classes and exercises the classes’ member functionsl.

Student answer should include the following:

  • 1 base class with 2 attributes, 2 constructors (default and with parameters), get/set methods for each attribute, and display methods
  • 2 derived classes with 2 attributes, 2 constructors (default and with parameters), get/set methods for each attribute and display methods
  • Any other member functions that are appropriate to the context of the class.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

Explanation:

Parent class is defined and Child1 and Child2 are derived from the parent class.

Each class is having two attributes, name and dob alongwith all the getters and setters and a display function for all of them.

In the main function, 2 instances are created of the derived classes and functions are called.

Code:


#include <iostream>

using namespace std;

class Parent
{
string name;
int dob;
  
public:
Parent()
{
name="";
dob=0;
}
Parent(string name, int dob)
{
this->name = name;
this->dob = dob;
}
void setName(string name)
{
this->name = name;
}
void setDob(int dob)
{
this->dob = dob;
}
  
string getName()
{
return name;
}
int getDob()
{
return dob;
}
void display()
{
cout<<"Name: "<<name<<endl;
cout<<"DOB: "<<dob<<endl;
}
  
};

class Child1: public Parent
{
string name1;
int dob1;
  
public:
Child1()
{
name1="";
dob1=0;
}
Child1(string name1, int dob1)
{
this->name1 = name1;
this->dob1 = dob1;
}
void setName1(string name1)
{
this->name1 = name1;
}
void setDob1(int dob1)
{
this->dob1 = dob1;
}
  
string getName1()
{
return name1;
}
int getDob1()
{
return dob1;
}
void display()
{
cout<<"Name: "<<name1<<endl;
cout<<"DOB: "<<dob1<<endl;
}
};

class Child2: public Parent
{
string name2;
int dob2;
  
public:
Child2()
{
name2="";
dob2=0;
}
Child2(string name2, int dob2)
{
this->name2 = name2;
this->dob2 = dob2;
}
void setName2(string name2)
{
this->name2 = name2;
}
void setDob2(int dob2)
{
this->dob2 = dob2;
}
  
string getName2()
{
return name2;
}
int getDob2()
{
return dob2;
}
void display()
{
cout<<"Name: "<<name2<<endl;
cout<<"DOB: "<<dob2<<endl;
}
};

int main()
{
Child1 c1("first child", 10102000);
Child2 c2("second child", 10012000);
  
c1.display();
c2.setName2("second");
c2.setDob2(12011981);
  
c2.display();
  

return 0;
}

Output:

PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!

Add a comment
Know the answer?
Add Answer to:
Define an example of inheritance with a base class that has 2 attributes, 2 derived classes...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In this laboratory, we are going to build a base class and a collection of derived...

    In this laboratory, we are going to build a base class and a collection of derived classes. Write the code to implement the class Person. A person has a name, address,telephone number and and E-Mail address. Be sure to include accessor functions to return each of these values. Create a derived class Student which has a class status (freshman, sophomore, junior, or senior). Create a derived class Employee which has the attributes office, salary, and date hired. Now define a...

  • Design a class named Person and its two derived classes named Student and Employee. MakeFaculty and Staff derived classes of Employee

    In Java(The Person, Student, Employee, Faculty, and Staff classes)Design a class named Person and its two derived classes named Student and Employee. MakeFaculty and Staff derived classes of Employee. A person has a name, address, phone number, and e-mail address. A student has a class status (freshman, sophomore, junior, or senior). An employee has an office, salary, and date hired. Define a class namedMyDate that contains the fields year, month, and day. A faculty member has office hours and a rank....

  • 1. Define a class Rectangle with two attributes: (This is for C++) -length, an integer (default...

    1. Define a class Rectangle with two attributes: (This is for C++) -length, an integer (default value 1) -width, an integer (default value 1) Provide the following member functions: -default constructor -constructor that takes parameters used to initialize the data -get/set function for each attribute -a function perimeter that returns the perimeter of the rectangle -a function area that returns the area of the rectangle b. Write a program that uses the class Rectangle to create two rectangle objects with...

  • using java write a code with notepad++ Create the following classes using inheritance and polymorphism Ship...

    using java write a code with notepad++ Create the following classes using inheritance and polymorphism Ship (all attributes private) String attribute for the name of ship float attribute for the maximum speed the of ship int attribute for the year the ship was built Add the following behaviors constructor(default and parameterized) , finalizer, and appropriate accessor and mutator methods Override the toString() method to output in the following format if the attributes were name="Sailboat Sally", speed=35.0f and year built of...

  • python 3 inheritance Define a class named bidict (bidirectional dict) derived from the dict class; in...

    python 3 inheritance Define a class named bidict (bidirectional dict) derived from the dict class; in addition to being a regular dictionary (using inheritance), it also defines an auxiliary/attribute dictionary that uses the bidict’s values as keys, associated to a set of the bidict’s keys (the keys associated with that value). Remember that multiple keys can associate to the same value, which is why we use a set: since keys are hashable (hashable = immutable) we can store them in...

  • UML Class Diagram with Inheritance Objectives Use UML Correctly indicate inheritance Demonstrate permissions Understand inheritance relationships...

    UML Class Diagram with Inheritance Objectives Use UML Correctly indicate inheritance Demonstrate permissions Understand inheritance relationships Labwork Please read all of the directions carefully. You will create a UML class diagram reflecting the class hierarchy for a fictional program that manages university personnel as constructed according to the graph displayed below. You will need to think about the attributes and behaviors that are unique to each class, and also those attributes and behaviors that are common amongst the subclasses and...

  • Question 3: Q3 (Polymorphism & virtual function) a) Consider the class, derived class, and the virtual functions as shown in Display for Q3. Create a derived class (of Sale) called 'Mailorder...

    Question 3: Q3 (Polymorphism & virtual function) a) Consider the class, derived class, and the virtual functions as shown in Display for Q3. Create a derived class (of Sale) called 'MailorderSale' having one private member variable called 'shipping charge'. It should have constructor function and virtual function 'bill' (this function adds shipping charge to the price). Define all these functions (no need of default constructors). b) In the main function, define one object of DiscountSale with (11,10) initial values and...

  • In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we...

    In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we will expand on our in-class exercise of developing the rectangleType class and overload the following operators: +, -, *, ++, --, ==, !=, >>, and <<. Your program should contain the following features: a. Create a base class named rectangleType with following private data members: • length with double type • width with double type b. rectangleType class should contain the following functions: •...

  • C++ Define the class HotelRoom. The class has the following private data members: the room number...

    C++ Define the class HotelRoom. The class has the following private data members: the room number (an integer) and daily rate (a double). Include a default constructor as well as a constructor with two parameters to initialize the room number and the room’s daily rate. The class should have get/set functions for all its private data members [3pts]. The constructors and the set functions must throw an invalid_argument exception if either one of the parameter values are negative. The exception...

  • This Program should run in C++ In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we will expand on our in-class exercise of developing the rectangleTyp...

    This Program should run in C++ In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we will expand on our in-class exercise of developing the rectangleType class and overload the following operators: +, -, *, ++, --, ==, !=, >>, and <<. Your program should contain the following features: a. Create a base class named rectangleType with following private data members: • length with double type • width with double type b. rectangleType class...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT