Avatar of saygapro
saygapro
 asked on

More Questions about c++

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
 
     true
 --------------------------------------------------------------------------------------

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

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

true
---------------------------------------------------------------------------------------------------

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

c
---------------------------------------------------------------------------------------------

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
{
// CODE OMITTED SEE PROBLEMS BELOW! :-)
      public:
          Build& operator ,( int a){
                  l.push_back(a);  
                  return this*;
          }

     private:
         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
{
  public:
     Seq() : a(1) { }
     int operator ()()
     {
           int retval(a);
           a += a;
           return retval;
    }
     private:
          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
{
    public:
        Employee(string const& name) : name_(name) { }
        double weeklyPaycheque(double numHoursWorked) const { return 0.0; }
    private:
        string name_;
};

class UnionEmployee
{
    public:
            UnionEmployee(string const& name, double hourlyRate);
};

My answer:

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

          double weeklyPaycheque(double numOfHoursworked){  return (hourRate * numOfHoursworked) ;}        
   
   protected:
         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)
        return;
    cout << e->weeklyPaycheque(e) << endl;
}

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

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.
C++

Avatar of undefined
Last Comment
evilrix

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
evilrix

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
oleber

Are we doing homeworks in here?
evilrix

>> Are we doing homeworks in here?
The OP has already answered the questions, he is seeking advice on his answers... there is nothing about that which breaks EE rules!
SOLUTION
Infinity08

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
evilrix

>> So, I think you can leave out the "probably".
Indeed, my use of the word 'probably' was a little misplaced.

>> The intended answer was probably indeed (c).
I think both (e) and (c) are reasonable answers depending upon how you interpret the question (I hate multiple choice); however, I agree with I8 that the Q is probably trying to lead you to (c).

>> evilrix's excellent answer
Wow, thanks :)
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
saygapro

ASKER
This is not a homework assignment as I stated in my first post.

Referring to your post, evilrix
"(ii) properly initializes the name in the Employee class

You need to call the base class constructor as part of the UnionEmployee constructor initialization list and pass it name.
"
Do you mean --   UnionEmployee(string const& name, double hourlyRate)  : Employee(name) ?

I didn't think I had to call the base class constructor? I thought the base case constructor was automatically called since UnionEmployee inherited this class.



"(iii) stores the hourlyRate
You should set this in the UnionEmployee constructor initialization list
"

Do you mean --   UnionEmployee(string const& name, double hourlyRate)  : hourRate(hourlyRate);  ?

Thanks.
     
SOLUTION
evilrix

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.