Effective Analysis Programming Part 2

From Gridkaschool
Revision as of 15:38, 29 August 2013 by Jmeyer (talk | contribs)
Jump to navigationJump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Gridka School slides



download

  • Code for hands-on exercises:

Download this tarball: Gks2013cpp.tar.gz

wget http://wiki.scc.kit.edu/gridkaschool/upload/a/ac/Gks2013cpp.tar.gz
tar xvfz Gks2013cpp.tar.gz


Polymorphism

exercise 1: virtual functions

#include<iostream>
#include<cmath>
#include<vector>

using namespace std;

class Shape {

public:
  virtual double getArea() const =0;

};

class Circle: public Shape {
public:
  Circle() : radius(1.) {
  }

  explicit Circle(double r) : radius(r) {
  }

  virtual double getArea() const {
    cout<<"Circle::getArea"<<endl;    
    return M_PI*radius*radius; 
  }

private:
  double radius;
};

class Square: public Shape {
public:
  Square() : length(1.) {
  }

  explicit Square(double l) : length(l) {
  }

  virtual double getArea() const {
    cout<<"Square::getArea"<<endl;    
    return length*length; 
  }
  
private:
  double length;
};


int main() {
  vector<Shape*> myShapes;
  myShapes.push_back(new Circle(2.));  
  myShapes.push_back(new Square(3.));  

  cout<<myShapes[0]->getArea()<<endl;
  cout<<myShapes[1]->getArea()<<endl;

  delete myShapes[0];
  delete myShapes[1];
}
  • Run the code to see which versions of getArea() get called. Remove the keyword virtual in the base class and run the code again.
  • Recall why non-virtal destructors are dangerous?
  • Why are not all destructors virtual by default? The next execise will help you to answer this question.

exercise 2

#include<iostream>

struct A {
  void foo() {
  }
};

struct B {
  virtual void foo() {
  }
};

int main() {
  A a;
  B b;
  std::cout<<sizeof(a)<<std::endl;
  std::cout<<sizeof(b)<<std::endl;
}
  • Can you explain the output?

C++ Templates

exercise 0: shapes

#include<iostream>
#include<cmath>

using namespace std;

class Circle {
public:
  Circle() : radius(1.) {
  }

  explicit Circle(double r) : radius(r) {
  }

  double getArea() const {
    cout<<"Circle::getArea"<<endl;
    return M_PI*radius*radius;
  }

private:
  double radius;
};


class Square {
public:
  Square() : length(1.) {
  }

  explicit Square(double l) : length(l) {
  }

  double getArea() const {
    cout<<"Square::getArea"<<endl;
    return length*length;
  }

private:
  double length;
};

template <typename Shape>
void PrintArea(Shape s) {
  cout<<s.getArea()<<endl;
}

int main() {
  Circle c(2.);
  Square s(3.);
  PrintArea(c);
  PrintArea(s);
}
  • Compare the code with version using virtal functions?
  • What are the differences/pros/cons of templates vs. dynamic polymorphism?

exercise 1: pair

std::pair is a simple template class to store a pair of values.

exercise 2: read paramerters

The library readparamertes can be used to read values from a text file like:

# GKS 2013
answer 42
parser_test "\\\"" "#" #should result in \" and #
useful yes

The following code is an example how to use readparameters:

#define MAIN_CPP

#include <iostream>
#include <exception>
#include "readparameters.h"

using namespace std;

int main(int argc,char **argv){ 
  //check arguments
  if (argc!=2) {
    cerr<<"Usage: ./params <paramfile>"<<endl;
    return 1;
  } 
  
  //get input filename from parameter file
  int answer;
  vector<string> v;
  try { 
      readparameters rp(argv[1]);
      answer = rp.get<int>("answer");
      v=rp.get_vector<string>("parser_test");
      cout<<answer<<endl;
      rp.print_tabular(); //debug output
      rp.get<long>("Atlantis"); //exception
      
  }
  catch (exception& e) {
      cerr<<e.what()<<endl;
      return 1;
  }

  return 0;
}
  • Run the code, add more parameters to the text file, read a boolean parameter.
  • Read the code of the class readparamerters (you may skip the parsing part in the constructor).
    • get and get_vector are template functions. Why are there specializations for bool and string?
    • Why is the map tabular mutual?
    • What are the requirements for types to work with readparameters?
    • Why is the default constructor privat?

Evaluation

If you liked the course please fill this evaluation form: http://surveys.gridka.de/limesurvey/index.php?sid=36984&lang=en