troubleshooting Question

More Questions about c++

Avatar of saygapro
saygapro asked on
7 Comments3 Solutions242 ViewsLast Modified:
Hi, these are questions taken from a book. The book does not provide solutions. I would like if someone could help me answer these questions and check over my answers. This is not for an assignment but for learning purposes. Thanks in advance.

Q1) True or False: In a C++ class one can define const and non-const versions of member functions with
the same name.
        (a) true (b) false

Q2) True or False: In a C++ class one can define const and non-const versions of constructors.
(a) true (b) false


Q3) If a C++ class were written in C, all virtual member functions would be, in general:
(a) bound at compile time (b) bound at run time represented by function pointers

b? not too sure about this
Q4) True of False: Types and integer constants are permitted as C++ template arguments:
(a) true (b) false


Q5) In C++, int a; int &i = a; would be equivalent to writing in C:
(a) int a; int *i = &a;
(b) int a; int const *i = &a;
(c) int a; int * const i = &a;
(d) int a; int const * const i = &a;
(e) none of these


If I have the following code below and I'm told to overload the << operator to output the private list<int> and I'm told "to access the private variable directly in this function",  does this mean I am supposed to ignore the fact that list variable inside Build is private for now? The reason I ask is because there is a later question that asks "Write what, if anything, you need to add to Build in order for Q12 to compile without errors (i.e., to permit access to the private variable)?"
The only way to access the private variable would be to use an accessor function correct?
In an earlier question I was told to overload the , operator so hopefully that part is correct.

using namespace std;
class Build
          Build& operator ,( int a){
                  return this*;

         list<int> l;
int main()
   Build mylist;
    mylist, 1, 5, -2, 3, 6, 8, 3;
    cout << mylist << endl;


If I have the following code below and I'm told to replace class Seq with a function called SeqFunc and to change the output function call line to this _> output(cout, SeqFunc) << endl;     then would I store the variable 'a' inside the function output? This way I could call SeqFunc and pass in the variable but also return an int to update the variable 'a'. Does this make sense? Is there any other way to do this other than having variable 'a' set as a global variable?

#include <iostream>
using namespace std;
class Seq
     Seq() : a(1) { }
     int operator ()()
           int retval(a);
           a += a;
           return retval;
          int a;

ostream& output(ostream& os, Seq& s)
      for (int i(0); i < 5; ++i)
              os << s() << (i < 4 ? ", " : "");
       return os;

int main()
        Seq s;
        output(cout, s) << endl;


Explain which solution is more flexible and better: writing Seq/SeqFunc as a class or as a function?
Hint: Consider this code:
        Seq a, b;
       for (int i(0); i<10; ++i)
               cout << ((i & 1) ? a() : b()) << endl;
       for (int i(0); i<10; ++i)
              cout << ((i & 1) ? SeqFunc() : SeqFunc()) << endl;

It looks like a() and b() is more flexible because it allows you to compute things on different objects and values inside those objects. The way SeqFunc() is shown here you can only change local variables inside the function and the other way to change variables outside the scope of SeqFunc is by using global variables.  I am not totally sure what exactly the answer is here but do I have a valid point?  Also, what does i & 1 mean?

Compare and constrast the differences between using templates and macros in a C++ program.

templates allow you to use different data types for your function. macros cannot do this. Macros are used for replacing strings with values. This is all I know really about macros. What else is there?


I have the code below and I'm told to write UnionEmployee so that it (i) inherits from Employee, (ii) properly initializes the name in the Employee class, and (iii) stores the hourlyRate.  Write the weeklyPaycheque member function for UnionEmployee. (Assume it is inside UnionEmployee.)
 I wrote my answer below the question code.

class Employee
        Employee(string const& name) : name_(name) { }
        double weeklyPaycheque(double numHoursWorked) const { return 0.0; }
        string name_;

class UnionEmployee
            UnionEmployee(string const& name, double hourlyRate);

My answer:

class UnionEmployee: public Employee{
           UnionEmployee(string const& name, double hourlyRate)  {
                        hourRate = hourlyRate;

          double weeklyPaycheque(double numOfHoursworked){  return (hourRate * numOfHoursworked) ;}        
         double hourRate;

The second part of the question from above says
"What would you do to Employee and/or UnionEmployee to ensure that this code will work" properly:
void output(Employee *e)
    if (!e)
    cout << e->weeklyPaycheque(e) << endl;

// later in main()...
UnionEmployee e(John Q. Public, 25.23);

I would make the function weeklyPaycheque inside Employee a virtual function. This would look like this
virtual double weeklyPaycheque(double numOfHoursworked)
I don't think there is anything that is needed to be changed in UnionEmployee
Is this correct?
The last question which refers to the previous question:

What would you do to Employee to ensure that this code will work properly:
Employee *e = new UnionEmployee(John Q. Public);
delete e;

I would need to add a virtual destructor?
virtual ~Employee(){ };

Is this correct?

Thanks for the help.
Senior Software Engineer (Avast)

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 3 Answers and 7 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 3 Answers and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros