#include <iostream>usingnamespacestd;classBase{public:Base(){}voidPrint(){ cout <<" I am base "<<endl;}Base&operator=(constBase&b){returnthis;}};classDerived:publicBase{public:Derived():Base(){}voidPrint(){ cout <<" I am derived "<<endl;}Derived&operator=(constDerived&){returnthis;}};intmain(){ Derived d; Base b; b=d;b.Print();return0;}
Part a
There are 2 compile time error, what are they and how can we fix them?
Solution
1st error
Here
and here, we should return a reference but returned a pointer
Fix: this instead of *this (asterisk this)
this is pointer, that is why we see code like this->x
In the code there is 1 compile-time error, , what is and how can it be fixed without changing the main?
Solution
d=b tries to convert from Based to Derived to type but the = operator receives a Derived type.
In C++, a derived class object can be assigned to a base class object, but the other way is not possible.
Tip: remember we call the = operator since b has already been created
Fix:
Part d
What is the output?
Solution
Remember = operator didn't really do anything and return *this
Question 2:
We get the following 10 times
Why type of error is this:
compilation
linker
runtime
logic
memory leak
Additionally, what caused the error?
Solution
logic
Since Player::playisn'tvirtual we callPlayer::play instead of SmartPlayer::play
Part b:
Fix: add virtual
Part c:
What is the output now?
Question 3
Part A:
Is there a compilation or any other type or error? If so, explain the error.
Solution
There is no compilation error
There is a warning
The compiler is warning us that usually we increment by 4 bytes lets say ++ip but here it can be something else and we may be leaving the memory space we wanted.
Part b
Remember ip = (int *)(++j); that here ip incremented by 1 and ip = 0x1
Then (++ip) will increment by the size of an int which is 4 bytes to the next address.
Question 4:
output:
What type of error is this?
Why does the error point to a file that we didn't write
Why after the error-message there is a note that points to code that we did write?
Solution
Compilation error
std::sort compares between objects using > operator
We didn't implement a > operator for Person
Usually template instantiations are created on compile time. Usually template error are very long
Part b
Add before the main function code so that sorted_by_age will contain the people sorted by age (youngest to oldest)
Solution 1: adding to global scope
Solution 2: adding to Person
Part C
add here so that sorted_by_age will contain the people sorted by age (youngest to oldest)
Solution
We will add a lambda function as so: [](Person a, Person b){return a.age<b.age;}
or like this
or this
Question 7: (22 pts)
Write a class binop that its ctor receives 3 parameters
2 container (iterables) with the same length
Binary functor (or lambda)
The binop returns a container (iterable) from the binary function on both containers.
for example:
Note: If the lengths are different the result is undefined
Here is the beginning of binop.hpp
Assume that range.hpp is already written correctly
Only finishbinop.hpp
Possible Solution
Lets remember how a for loop looks with an iterator
Things we must implement
ctor
iterator
operator*
operator++
operator==
operator!=
begin()
end()
5778 moed-a
Why type of error is this:
compilation
linker
runtime
logic
memory leak
Additionally, what caused the error?
Solution
runtime error
Now the operator will return garbage- or basically an invalid reference
cout << t returns garbage, then << endl will try to write to an invalid address throwing an exception
Part b
fix the code by adding 1 line of code
Solution
Question 2:
The code works but runs slow, what is the bug?
Solution
Here m is passed by value
So the copy ctor is called
The copy ctor will copy 1,000,000 values which takes time
#include <iostream>
using namespace std;
class Base {
public:
Base(){}
void Print( ){
cout <<" I am base "<<endl;
}
Base& operator=(const Base & b){
return this;
}
};
class Derived : public Base {
public:
Derived():Base(){}
void Print( ){
cout <<" I am derived "<<endl;
}
Derived& operator=(const Derived &){
return this;
}
};
int main() {
Derived d;
Base b;
b=d;
b.Print();
d=b;
d.Print();
return 0;
}
class Derived : public Base {
public:
Derived():Base(){}
...
Derived& operator=(const Derived &){
return *this;
}
};
int main() {
Derived d;
Base b;
b=d;
b.Print();
d=b; // < ==== here we try to convert from Based to Derived
d.Print();
return 0;
}
Derived& operator=(const Base &){
return *this;
}
I am base
I am derived
#include <iostream>
#include <string>
using namespace std;
class Player {
public:
void play() {
cout << "Error: Player.play is not defined" << endl;
}
};
class SmartPlayer: public Player {
public:
void play() {
cout << "Smart Move" << endl;
}
};
class SimplePlayer: public Player {
public:
void play() {
cout << "Simple Move" << endl;
}
};
void game(Player* player1, Player* player2, int turns) {
for (int i=0; i<turns; ++i) {
player1->play();
player2->play();
}
}
int main() { // a demo program
game(new SmartPlayer(), new SimplePlayer(), 5);
}
Error: Player.play is not defined
class Player {
public:
void play() { // < ========= Should be virtual
cout << "Error: Player.play is not defined" << endl;
}
};
class SmartPlayer: public Player {
public:
void play() {...}
};
class Player {
public:
virtual void play() {
cout << "Error: Player.play is not defined" << endl;
}
};