Write the declaration for class \(B\). The class's members should be \(\bullet\) \(\mathrm{m},\) an integer. This variable should not be accessible to code outside the class or to member functions in any class derived from class B. \(\bullet\) \(\mathrm{n},\) an integer. This variable should not be accessible to code outside the class, but should be accessible to member functions in any class derived from class B. \(\bullet\) setM, getM, setN, and getN. These are the set and get functions for the member variables m and n. These functions should be accessible to code outside the class. \(\bullet\) calc, a public virtual member function that returns the value of m times n. Next write the declaration for class \(\mathrm{D}\), which is derived from class \(\mathrm{B}\). The class's members should be \(\bullet\) \(\mathrm{q},\) a float. This variable should not be accessible to code outside the class but should be accessible to member functions in any class derived from class \(\mathrm{D}\) \(\bullet\) \(\mathrm{r},\) a float. This variable should not be accessible to code outside the class, but should be accessible to member functions in any class derived from class D. \(\bullet\) setQ, getQ, setR, and getR. These are the set and get functions for the member variables \(q\) and \(r .\) These functions should be accessible to code outside the class. \(\bullet\) calc, a public member function that overrides the base class calc function. This function should return the value of q times \(r\)

Short Answer

Expert verified
Question: Create two class declarations in C++: Class B and Class D, with Class D inheriting from Class B. Include member variables with different visibility restrictions, set and get functions, and a calc function for both classes. Answer: Class B: - Private: int m; - Protected: int n; - Set and Get functions for m and n; - Public virtual member function: int calc() const; Class D (inherits from Class B): - Protected: float q, float r; - Set and Get functions for q and r; - Public member function (override Class B's calc()): int calc() const;

Step by step solution

01

Class B Declaration

First, let's declare the class B. This class has two integer member variables with different accessibility levels, m and n. We will declare m as private and n as protected so that it is accessible by derived classes. We will add "set" and "get" member functions for m and n. Finally, we will add the public virtual member function "calc". ```cpp class B { private: int m; // Not accessible outside the class or any derived class protected: int n; // Not accessible outside, but accessible to derived classes public: // Setters and Getters void setM(int m_value); int getM() const; void setN(int n_value); int getN() const; // Public virtual member function virtual int calc() const; }; ```
02

Class D Declaration

Now, let's declare the class D. This class is derived from class B. It has two float data members with different accessibility levels, q, and r. Both should be declared as protected so that they are accessible by derived classes. We will add "set" and "get" member functions for q and r. Lastly, we will add the public member function "calc" that overrides the base class calc function. ```cpp class D : public B { protected: float q; // Not accessible outside, but accessible to derived classes float r; // Not accessible outside, but accessible to derived classes public: // Setters and Getters void setQ(float q_value); float getQ() const; void setR(float r_value); float getR() const; // Public member function that overrides the base class calc function int calc() const override; }; ``` Now we have our two class declarations, B and D, with the required data members, set/get functions, and calc functions.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Study anywhere. Anytime. Across all devices.

Sign-up for free