High Quality Work!

Self-Motivation!

Despite the chaos:

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

Optional but counts as 15% for final grade!

Due Tonight!

2/7

2/21

2/28

3/26

5/21

Team Selection

Client Presentations

Project Proposal

Revised Project Proposal

Final Project Documentation

No class

No class

No class

Project Presentations

5/03

No class

Implementation / Testing / Deployment

57 Days

Revised Project Proposal

Due Today!

Can we start coding?

Most important

Revised Project Proposal

Will it still change?

Yes!

You will get feedback.

Revised Project Proposal

After feedback, copy it over..

Final Project Documentation

Due 5/21!

Keep updating it while you code..

Bonus

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

#include <iostream>

int main()
{

  std::string temp;
  
  std::cout << "Please enter a temperature in F. ";
  
  std::cin >> temp;
  
  float convertedTemp = std::stof(temp) - 32 / 1.8;
  
  std::cout << "Converted to Celsius, this temperature is "
  	<< convertedTemp << "!\n";
  
}
#include <iostream>

int main()
{

  std::string temp;
  
  std::cout << "Please enter a temperature in F. ";
  
  std::cin >> temp;
  
  float convertedTemp = ( std::stof(temp) - 32 ) / 1.8;
  
  std::cout << "Converted to Celsius, this temperature is "
  	<< convertedTemp << "!\n";
  
}

Bonus

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

#include <iostream>

int main()
{

  float temp;
  
  std::cout << "Please enter a temperature in F. ";
  
  std::cin >> temp;
  
  float convertedTemp = ( temp - 32 ) / 1.8;
  
  std::cout << "Converted to Celsius, this temperature is "
  	<< convertedTemp << "!\n";
  
}

Hidden Casting!

Bonus

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

// part 2
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);
  //...

Bonus

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

// part 2
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);
  //...

Pointer to the instance!

Bonus

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

  //... 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!

Bonus

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

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

c->fromFtoC(temp);

One way to avoid wild pointers...

Templates.. (next week)

// part 2
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!

Bonus

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

  //... 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!

Bonus

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

  //... 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

Bonus

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

// part 3
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);
  //...

Bonus

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

// part 3
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!

Bonus

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

Arrays and

Vectors

Variable

Array

int carton[10];
int carton[10] = { };
int carton[10] = { 1, 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";
  
}

CS410 Lecture 20

By Daniel Haehn

CS410 Lecture 20

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

  • 72

More from Daniel Haehn