The University of Iowa
The College of Liberal Arts and Sciences
Department of Computer Science
Lecture/Lab #14
Destructors, std::vector
struct moveable {
virtual void move_by( point const& offset ) {}
};
struct streamable {
virtual void write( std::ostream& ) const {}
};
struct alive {
virtual bool has_energy() const { return true; }
virtual int max_energy() const { return 0; }
};
struct shape : moveable, streamable {};
struct organism : moveable, streamable, alive {};
void move_around( moveable& );
void move_and_record( shape& );
int main() {
organism o;
move_around( o );
move_and_record( o );
}
Fix the compilation issues and use your best judgement to make the program pass the tests
Open the exercise template
Write your code, press Run to test
When you're done, grab your Repl's link and send it as a direct message to me (agurtovoy)
Click on the corresponding option in the "Lab14 exercises" poll in #general
struct car
{
car() { std::cout << "car constructed" << std::endl; }
~car() { std::cout << "car destructed" << std::endl; }
};
int main() {
car c;
}
A special member function that is called right before the object is destroyed (e.g. when the object goes out of scope)
The primary purpose is to free the resources that the object may have acquired during its lifetime
#include <vector>
int main() {
std::vector<point> v = {
point{ 0, 0 },
point{ 10, 17 },
point{ 5, -7 }
};
std::cout << v.size() << std::endl;
}
A linear collection of objects, all of which have the same type
Often referred to as a container because it “contains” other objects.
Similar to list in Python / ArrayList in Java
Every object in vector has an associated index, which gives access to that object.
Write a program that asks user for a number N, then asks user to enter N integers, then prints the space-separated sequence of integers to the console
Open the exercise template
Write your code, press Run to test
When you're done, grab your Repl's link and send it as a direct message to me (agurtovoy)
Click on the corresponding option in the "Lab14 exercises" poll in #general