5779 a

#include <iostream>
using namespace std;

using namespace std;
class A {
public:
    A() { cout << "A(0)" << endl; }
    A(const A& a) { cout << "A(1)" << endl; }
};

class B {
public:
    B() : a() { cout << "B(0)" << endl; }
    B(const B& b) { cout << "B(1)" << endl; }
private:
    A a;
 };

int main() {
    B object1;
    B object2 = object1;
    return 0;
}

What is the output?

will result in, since a is a member field of B it will initialized before and the constructor will be called before Bs ctor

and

will output, since the copy ctor B(const B& b) will be called

Remember!

An object is copied when:

  1. Constructing new object from existing Copy ctor

  2. Passing parameter by value Copy ctor

  3. Returning by value. Copy ctor

  4. Assigning existing to existing. Assignment ctor

Cases 1-3 are handled by copy constructor;

Case 4 is handled by assignment operator.

part b

remove this line

And now what will be the output?

Solution

first B object1; will result in A(0) B(0) as above.

Now the copy ctor is called since it is the creation of the object2. Since a is initialized before any object it uses the default.

part c

now delete this line and return the previous line

What is the output?

Solution

first B object1; will result in A(0) B(0) as above. Then initialize a A(0) and finally call the copy ctor B(1)

Question 2 (9pts)

The following cause a compile time error

Explain the error and why does it happen

Solution

printit(B arg) receives a B object but was given an A object, it can't convert a since the ctor is explicit

Part b

fix the code above, only fix this line

Solution

We can call converting ctor explicitly

or just give a B object

Part c

After the fix what will be the output?

Solution

If we used the 1st answer above printit(B (a)) then the output would be like this

If we used the 2nd answer above printit(b) then the output would be like this

printit(B arg) will call the copy ctor, which hasn't been defined

Question 3: (9pts)

Part a:

On this the following line there is a compile time error

Explain the error, and why is it happening?

Solution

void drawTheShape(Shape s) Here a new instance of Shape is called but since Shape is pure virtual as seen here

Objects can't be created.

Part b:

fix the line void drawTheShape(Shape s) { and only this line

Solution 1:

receive s by reference.

Solution 2:

receive an implemented class type

Part c:

what is the output?

using the previous Solution 1:

This is because we first call the derived dtor and only after we call the bases dtor.

using the previous Solution 2:

Question 4: (9pts)

Part a:

Why type of error is this:

  • compilation

  • linker

  • runtime

  • logic

  • memory leak

Additionally, what caused the error?

Solution

This is a compile-time error

  • We included #include <iostream> which includes std::string

  • Also, we used using namespace std;

  • Finally, we created a class by the name of string

Now the compiler doesn't know which string to call, our string or std::string

Read more herearrow-up-right and herearrow-up-right about different types of errors

This make sense that here is a compile error since it has nothing to do typing object files together or using a library rather ambiguity.

Part b:

Fix the code by removing only 1 line of code, don't alter the main

Solution

remove this line

now the compiler will knows who to call since string refers to our class and std::string refers to the std

Part c

Here string has pointer p but string doesn't have a dtor.

Will this cause an error? If so, why type of error, if not why won't it cause an error?

Solution

This is no error, notice the following

string doesn't allocate memory there is no new or malloc it only receives a pointer to allocated memory

Question 5-6: (22pts)

Question 7: (22pts)

  • Write Money.cpp and Money.hpp

  • Allow addition of different sums with automatic conversion of units

  • The rate of each coin is set in the beginning of the code

Solution

Money.hpp

Money.cpp

Last updated