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

C++11 1. This problem requires that you write a class definition, and implement

ID: 3729809 • Letter: C

Question

C++11

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

Hi,

The given program is just to make you understand about Inheritance in C++.

I coded everything you needed. But make sure you understand everything through comments for every declaration and implementation. If you got any doubt, please revert to me.. Happy to help you.

#include <iostream>

using namespace std;

class Base {

public:

Base();

Base(char y);

char getLetter() const;

void setLetter(char c);

private:

char letter;

};

Base::Base(){ //initializing base class default constructor

//void

}

Base::Base(char y){ //initializing base class constructor

letter = y;

}

void Base::setLetter(char c){ //implementing base class insertion operator

letter = c;

}

char Base::getLetter() const { //implementing base class insertion operator

return letter;

}

class Derived: public Base { //derived class with public inheritance

public:

Derived(); //Default Constructor

Derived(char y); //Constructor that takes a single char

Derived(char y, int i); //Constructor that takes a single char and one int

void setLetter(char c, int i); //An overloaded insertion operator

private:

int num;

};

Derived::Derived(char y): Base(y){ //initializing derived class constructor with single argument

//can do if want any

}

Derived::Derived(): Base() { //initializing derived class default constructor

//can do if want any

}

//implement the constructor that takes two parameters (use the initialization section)

Derived::Derived(char y, int i): Base(y){ //initializing derived class constructor

num = i;

//cout << "( " << Base::getLetter() << "," << num << ")"; optional, uncomment if you want

}

//the insertion operator.

//The insertion must print the Derived object in the following way: (char, int), with the parentheses.

void Derived::setLetter(char y, int i) { //implementing insertion operator in derived class

Base::setLetter(y);

num = i;

cout << "( " << Base::getLetter() << "," << num << ")";

}

int main(){

//Derived derived('A', 10); directly initializing the derived class object

Derived derived;

derived.setLetter('B', 20);

return 0;

}

Happy coding :)

All the best.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote