Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For these exercises, practice them with pen & paper first. Try to give yourself

ID: 3727988 • Letter: F

Question

For these exercises, practice them with pen & paper first. Try to give yourself no more than 20 minutes per exercise Then, put the code as written into an editor (You will need to create full class de finitions and a main function in order to test appropriately) and see how you did. 1. This problem requires that you write a class definition, and implement two of its functions. Given the following class definition class Base public: Base Base (char y) char getLetter) const; void setLetter (char c) private: char letter; Derive a class from Base called Derived. It must have the following: . Default Constructor Constructor that takes a single char Constructor that takes a single char and one int . An overloaded insertion operator . A new private data member int called num Write out the class definition, and implement the constructor that takes two parameters (use the initialization section), and the insertion operator. The insertion must print the Derived object in the following way: (char, int), with the parentheses.

Explanation / Answer

#include <iostream>
using namespace std;

class Base   // base class
{
public:
Base(){}
Base(char y)
{
letter = y;
}
char getLetter() const
{
return letter;
}
void setLetter(char c)
{
letter = c;
}

private:
char letter;

};

class Derived : public Base   //derived class
{
public:
Derived(){}
Derived(char c):Base(c) // sending argument to Base class constructor
{

}
Derived(char c,int num):Base(c)
{
this->num = num;
}
friend ostream& operator<<(ostream& os, const Derived& d); // overloaded insertion operator

private:
int num;



};

ostream& operator<<(ostream& os, const Derived& d)
{
    os << "(" << d.getLetter() << ',' << d.num <<")" ;
    return os;
}
int main() {

Derived d('A');

Derived d1('B',4);

cout<<d1;
return 0;
}

Output:

(B,4)

Please upvote if it is helpful. Do ask if you have any queries.