#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:
Constructing new object from existing Copy ctor
Passing parameter by value Copy ctor
Returning by value. Copy ctor
Assigning existing to existing. Assignment ctor
Cases 1-3 are handled by copy constructor;
Case 4 is handled by assignment operator.
remove this line
And now what will be the output?
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.
now delete this line and return the previous line
What is the output?
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
printit(B arg) receives a B object but was given an A object, it can't convert a since the ctor is explicit
fix the code above, only fix this line
We can call converting ctor explicitly
or just give a B object
After the fix what will be the output?
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)
On this the following line there is a compile time error
Explain the error, and why is it happening?
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.
fix the line void drawTheShape(Shape s) { and only this line
receive s by reference.
receive an implemented class type
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)
Why type of error is this:
Additionally, what caused the error?
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 here and here 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.
Fix the code by removing only 1 line of code, don't alter the main
remove this line
now the compiler will knows who to call since string refers to our class and std::string refers to the std
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?
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
Money.hpp
Money.cpp