The University of Iowa
The College of Liberal Arts and Sciences
Department of Computer Science
Programming Languages and Tools:
CS:3210:0001
Lecture/Lab #16
Programming with C++
Destructors, std::vector
Warm up
-
In your own words, define run-time polymorphism
-
How do we mark a member function as polymorphic?
-
How do we override a polymorphic function in a derived class?
Destructors
struct car
{
car() { std::cout << "car constructed\n"; }
~car() { std::cout << "car destructed\n"; }
};
int main() {
car c;
}
-
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
-
The "inverse" of a constructor
- Has a predefined name and signature: the name of the class prefixed by a tilde (~); has no return value and takes no parameters
Destructors (cont.)
-
There is always only one destructor for a given class (i.e. destructor cannot be overloaded)
-
The body of the destructor is executed right before the object is destroyed.
-
Data members and base classes are destroyed in reverse order from the order in which they were initialized.
-
Members of user-defined types are destroyed by running the member’s own destructor (if any).
-
Data members of the built-in types are left as-is.
Destructors are called when:
-
Variables are destroyed when they go out of scope.
-
Members and base objects (if any) of an instance of a user-defined type are destroyed when the object of which they are a part is destroyed.
-
Temporary objects are destroyed at the end of the full expression in which the temporary was created.
if ( true ) {
car c;
} // c is destroyed
draw( point(0, 0), point(10, 10) );
// both temporary points are
// destroyed now
std::vector
-
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.
v[0]
v[1]
v[v.size()-1]
...
v.capacity()
v.size()
std::vector operations
#include <vector>
int main() {
std::vector<int> v;
assert( v.empty) );
assert( v.size() == 0 );
// v[0] = 100; // uh-oh, undefined behavior
v.push_back( 101 );
std::cout << v[0] << "\n"; // prints 101
}
v.empty()
v.size()
v.push_back( x )
v[n]
for ( auto e: v ) ...
Returns true if v is empty, false otherwise
Returns number of elements in v
Adds a (copy of) element x to the end of v
Returns a reference to the n-th element in v
Iterates through v's elements in linear order
std::vector vs Python's list
numbers = [ 1, 5, -7, 0, 15, 99 ]
print( 'size:', len( numbers ) )
numbers.append( 110 )
print( 'size:', len( numbers ) )
numbers[0] = -100
for n in numbers:
print( n )
std::vector<int> numbers =
{ 1, 5, -7, 0, 15, 99 };
std::cout << "size: "
<< numbers.size() << "\n";
numbers.push_back( 110 );
std::cout << "size: "
<< numbers.size() << "\n";
numbers[0] = -100;
for ( auto n: numbers )
std::cout << n << "\n";
Python
C++
Exercise 1
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 "Lab16 exercises" poll in #general
Programming with C++, Spring 2020, Lecture #16
By Aleksey Gurtovoy
Programming with C++, Spring 2020, Lecture #16
Destructors, std::vector
- 531