Skip to content

Instantly share code, notes, and snippets.

@geekskick
Created March 1, 2017 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geekskick/873d3e80c76c736877fe25c5c5cb3c65 to your computer and use it in GitHub Desktop.
Save geekskick/873d3e80c76c736877fe25c5c5cb3c65 to your computer and use it in GitHub Desktop.
Lab Exerise 2
#include <iostream>
#include <vector>
#include "carbon_footprint.h"
#include "car.h"
#include "building.h"
#include "bicycle.h"
int main ()
{
{
std::cout << "--- --- Hello, World! Method 1" << std::endl;
/* this method means I have 3 distinct objects created beforehand, then i put pointers to them
* as their base type (carbon_footprint). This is less maintainable, i think, because if you want to
* add in an extra object you have to make you sure create iterator_pointer, then add iterator_pointer to the vector
*/
car c;
building build;
bicycle bike;
// ------ VECTORS 1 -------
// this is a way of making a vector, but you can choose any of the options below,
std::vector< carbon_footprint* > make_inplace( { &c, &build, &bike } );
// ------ VECTORS 2 -------
std::vector< carbon_footprint* > made_using_pushback;
made_using_pushback.push_back( &c );
made_using_pushback.push_back( &build );
made_using_pushback.push_back( &bike );
// ------ VECTORS 3 -------
// although iterator_pointer looks more complex this is actually probably the best way of doing iterator_pointer - references are like
// safe pointers cause they can never be NULL and you dont hve to worry about deferencing etc
std::vector<std::reference_wrapper<carbon_footprint>> contains_references( { c, build, bike } );
// ----- Iterating over vectors 1 ------
// vectors have iterators in c++ which are really powerful!
std::cout<< "--- Accessing using iterator" << std::endl;
std::vector<carbon_footprint*>::iterator iterator_pointer;
for( iterator_pointer = make_inplace.begin(); iterator_pointer != make_inplace.end(); iterator_pointer++ )
{
// iterator_pointer is a pointer to something in the vector,
// so iterator_pointer needs to be dereferenced to access the .get_carbon_footprint() method
std::cout << typeid(*iterator_pointer).name() << ": " << (*iterator_pointer)->get_carbon_footprint() << std::endl;
}
// ----- Iterating over vectors 2 ------
std::cout<< "--- Accessing like an array" << std::endl;
// but you could do iterator_pointer like this too, like a normal array
for( int i = 0; i < made_using_pushback.size(); i++ )
{
std::cout << typeid(made_using_pushback[i]).name() << ": " << made_using_pushback[i]->get_carbon_footprint() << std::endl;
}
std::vector<std::reference_wrapper<carbon_footprint>>::iterator iterator_referenes;
//finally using the references
std::cout<< "--- Accessing using iterator, over the references" << std::endl;
for( iterator_referenes = contains_references.begin(); iterator_referenes != contains_references.end(); iterator_referenes++ )
{
// as its the reference we dont need to de-reference the pointer!
std::cout << typeid(*iterator_referenes).name() << ": " << iterator_referenes->get().get_carbon_footprint() << std::endl;
}
}
{
std::cout << "--- --- Hello, World! Method 2" << std::endl;
// new makes it allocated on the heap rather than the program's stack, similar to malloc/calloc in C
// useful if you have loads and loads of objects. Also leads to easy to change code since you create the object
// and add to the vector in the same move.
std::vector< carbon_footprint* > things( { new car(), new building(), new bicycle() } );
std::vector<carbon_footprint*>::iterator it;
std::cout<< "--- Accessing using iterators over 'new' objects" << std::endl;
for( it = things.begin(); it != things.end(); it++ )
{
std::cout << typeid( *it ).name() << ": " << (*it)->get_carbon_footprint() << std::endl;
}
// because i have used 'new' you need to delete it afterwards
// this is just one method of doing that
std::cout<< "--- Deleting 'new' objects" << std::endl;
for( it = things.begin(); it != things.end(); it++ )
{
things.erase(it);
}
}
{
std::cout << "--- --- Hello, World! Method 3" << std::endl;
bicycle b;
carbon_footprint& ref_b = b; //reference to the bike ( pointer to the bike )
std::cout << b.get_spoke_num() << std::endl;
/* when you try to build this will give an error. The ref_b is as a carbon_footprint - so as far as the
* complier is aware it has the properties ( functions and variables ) of the carbon_footprint class
*/
std::cout << ref_b.get_spoke_num() << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment