Dr. Rudolph Pienaar

3/21 11:00a

Remote Guest Lecture

Hands-on Day! (C++ Functions and Classes)

Optional but counts as 15% for final grade!

Due Wednesday Night!

High Quality Work!

Self-Motivation!

Despite the chaos:

2/7

2/21

2/28

3/25

5/20

Team Selection

Client Presentations

Project Proposal

Revised Project Proposal

Final Project Documentation

No class

No class

No class

Project Presentations

5/02

No class

Implementation / Testing / Deployment

61 Days

Talk to the TAs!

Standup or Staff Meeting!

Wednesday or Friday

using namespace std;
#include <iostream>


int main()
{
    
  string name;
  cout << "What is your name? ";
  cin >> name;
  cout << "Hello, " << name << "!\n";
  
  int howmany;
  
  cout << "How many numbers do you want to see?" << "\n";
  cin >> howmany;
  
  for (int i=0; i<howmany; i++) {
      
      
      cout << i << "\n";
      
      
  }
  
  
}
using namespace std;
#include <iostream>


int main()
{
    
  
//   cout << sizeof(theanswer);
  
  //float, double, char, bool, short, int
   
  short i;
  cout << "Short " << sizeof(i) << "\n";
  
  float j;
  cout << "Float " << sizeof(j)<< "\n";
  
  char c;
  cout << "Char " << sizeof(c)<< "\n";
  
  char d;
  cout << "Press a character!" << "\n";
  cin >> d;
  
  if (d == 68) {
   
    cout << "D was pressed!";
      
  }
  
}
using namespace std;
#include <iostream>


int main()
{
    
  int whichN;
  cout << "n! Factorial, please enter N! ";
  cin >> whichN;
  
  // 5!
  // 5 * 4 * 3 * 2 * 1
  
  int theanswer = 1;
  
//   cout << sizeof(theanswer);
  
  //float, double, char, bool, short, int
  
  for (whichN; whichN>0; --whichN) {
      
      theanswer *= whichN;
      
  }
  
  cout << theanswer;
  
  
}
using namespace std;
#include <iostream>


// usually in a converter.h
class Converter {
 
  public:
    float fromFtoC( float );
    
};

// usually in a converter.cc
float Converter::fromFtoC( float temperatureInF ) {
 
  return ( temperatureInF - 32 ) * 5/9;   
    
};



int main()
{
   
   float temp;
   cout << "Temp in F? \n";
   cin >> temp; 
   
   Converter *converter = new Converter();
   cout << converter->fromFtoC( temp );
   
   return 0; 
  
}

class Converter {
    
    public:
        float fromFtoC(float);
    
};

float Converter::fromFtoC(float temp) {
    
  return ((temp-32)/1.8);
  
};
  //... in main
  Converter c;
  
  float convertedTemp = c.fromFtC(temp);
  //...
  //... in main
  Converter *c = new Converter;
  
  float convertedTemp = c->fromFtoC(temp);
  //...

Pointer to the instance!

  //... in main
  Converter *c;
  
  float convertedTemp = c->fromFtoC(temp);
  //...

Wild pointer!

std::unique_ptr<Converter> c(new Converter());    

c->fromFtoC(temp);

One way to avoid wild pointers...

Templates.. (next week)

class Converter {
    
    public:
        float fromFtoC(float);
    
};

float Converter::fromFtoC(float temp) {
    
  return ((temp-32)/1.8);
  
};
  //... in main
  Converter *c = new Converter;
  
  float convertedTemp = c->fromFtoC(temp);
  //...
  //... somewhere else in the code
  Converter *c = new Converter;
  
  float convertedTemp = c->fromFtoC(temp);
  //...
  //... somewhere else again
  Converter *c = new Converter;
  
  float convertedTemp = c->fromFtoC(temp);
  //...

3 instances!

  //... Everywhere!
  SingletonConverter *c = SingletonConverter::getInstance()
  float convertedTemp = c->fromFtC(temp);
  //...
  //... in main
  Converter *c = new Converter;
  
  float convertedTemp = c->fromFtoC(temp);
  //...
  //... somewhere else in the code
  Converter *c = new Converter;
  
  float convertedTemp = c->fromFtoC(temp);
  //...
  //... somewhere else again
  Converter *c = new Converter;
  
  float convertedTemp = c->fromFtoC(temp);
  //...

Always use the same instance!

  //... in main
  Converter *c = new Converter;
  
  float convertedTemp = c->fromFtoC(temp);
  //...
  //... somewhere else in the code
  Converter *c = new Converter;
  
  float convertedTemp = c->fromFtoC(temp);
  //...
  //... somewhere else again
  Converter *c = new Converter;
  
  float convertedTemp = c->fromFtoC(temp);
  //...

3x Converter

  //... in main
  SingletonConverter *c = SingletonConverter::getInstance()
  float convertedTemp = c->fromFtC(temp);
  //...
  //... somewhere else in the code
  SingletonConverter *c = SingletonConverter::getInstance()
  float convertedTemp = c->fromFtC(temp);
  //...
  //... somewhere else again
  SingletonConverter *c = SingletonConverter::getInstance()
  float convertedTemp = c->fromFtC(temp);
  //...

1x Converter

class SingletonConverter
{
   public:
       static SingletonConverter* getInstance( ) {
       
            return instance;
       
       };
       ~SingletonConverter( );
        float fromFtoC(float temp) {
            return ((temp-32)/1.8);
        };
   private:
       SingletonConverter( );
       static SingletonConverter* instance;
};
  //... in main
  SingletonConverter *c = SingletonConverter::getInstance()
  float convertedTemp = c->fromFtC(temp);
  //...

class SingletonConverter
{
   public:
       static SingletonConverter* getInstance( ) {
       
       	    static SingletonConverter* instance;
            return instance;
       
       };
       ~SingletonConverter( );
        float fromFtoC(float temp) {
            return ((temp-32)/1.8);
        };
   private:
       SingletonConverter( );
};
  //... in main
  SingletonConverter *c = SingletonConverter::getInstance()
  float convertedTemp = c->fromFtC(temp);
  //...

Lazy Loading!

Functions, Classes

Arrays, Vectors

Templates

Cython

Run our C++ code in Python using Cython

and compare timing against NumPy

Analyze a bunch of numbers and calculate min, max, mean, stddev.

Arrays and

Vectors

Variable

Array

int carton[10];
int carton[10] = { };
int carton[10] = { 1, 3, 3, 7 };

Array is full..

Larger Array...

It will be full eventually...

What about a dynamic container?

Vector

Let's try it!

#include <iostream>

class SingletonConverter
{
   public:
       static SingletonConverter* getInstance( ) {
       
            return instance;
       
       };
       ~SingletonConverter( );
        float fromFtoC(float temp) {
            return ((temp-32)/1.8);
        };
   private:
       SingletonConverter( );
       static SingletonConverter* instance;
};

int main()
{

  std::string temp;
  std::cout << "Please enter a temperature. ";
  std::cin >> temp;
  
  std::cout << "Converted to Celsius, this temperature is " << SingletonConverter::getInstance()->fromFtoC(std::stof(temp)) << "!\n";
  
}

Q & A

lecture related

project related

CS related

class Circle:
    
    instance = None
        
    @staticmethod
    def getInstance():
        
        if Circle.instance == None:
            Circle.instance = Circle()
            
        return Circle.instance
        
public final class Circle {

    private static final Circle INSTANCE = 
      new Circle();

    public static Circle getInstance() {
        return INSTANCE;
    }
}
public final class Circle {

    private static final Circle INSTANCE = null;

    public static Circle getInstance() {
    
    	if (!INSTANCE) {
        	INSTANCE = new Circle();
        }
    
        return INSTANCE;
    }
}

runs at Application Start

runs on demand

Circle.getInstance();

Lazy Loading!

class Singleton
{
   public:
       static Singleton* getInstance( ) {
       
            return instance;
       
       };
       ~Singleton( );
   private:
       Singleton( );
       static Singleton* instance;
};

No Lazy Loading

C++ Bonus

class Singleton
{
   public:
       static Singleton* getInstance( ) {
       
           static Singleton* instance;
           return instance;
       
       };
       ~Singleton( );
   private:
       Singleton( );
       
};

Lazy Loading!

CS410 Lecture 19

By Daniel Haehn

CS410 Lecture 19

Slides for CS410 Software Engineering at UMass Boston. See https://cs410.net!

  • 538