Skip to article frontmatterSkip to article content

PS3: Introduction to refactoring

IUT d'Orsay, Université Paris-Saclay

Objectives

The goal of this session is to understand the following points:

Exercise 1 : Short functions

Code organization

  1. Create a short-functions/ directory inside PS3/.

  2. Create the following files inside short-functions/.

product.h
product.cpp
display-prices.h
display-prices.cpp
main.cpp
#ifndef PRODUCT_H
#define PRODUCT_H

#include <string>

class Product {
private:
    std::string mName;
    double mPrice; // in euros
    int mQuantity; // in units

public:
    Product(const std::string& name, double price, int quantity);
    std::string getName() const;
    double getPrice() const;
    int getQuantity() const;
    bool operator<(const Product& other) const;
};

#endif
  1. Compile the code with g++ -o short-functions main.cpp display-prices.cpp product.cpp (the order of .cpp files does not matter).
  1. Run ./short-functions.

Refactoring the code

  1. What does this code do?
  1. Quiz: What are the issues with the code of displayAvailableProductsByNonDecreasingPriceAndDisplayTotalPrice?

  2. Refactor the code of display-prices.cpp (by modifying the other affected files, such as the header and main).

Exercise 2: Classify it

  1. Create a directory classify-it/ (within which the code will be organized into several different files).

  2. In classify-it/, create the following file.

main.cpp
#include <iostream>
#include <string>
#include <vector>

double calculateAverage(const std::vector<double>& scores) {
    double sum = 0;
    for (double score : scores) {
        sum += score;
    }
    return sum / scores.size();
}

void printScores(const std::string& studentName, int studentId, const std::vector<double>& scores) {
    std::cout << std::endl << "Scores for " << studentName << " (Id: " << studentId << "): ";
    for (size_t i = 0; i < scores.size(); i++) {
        std::cout << "[" << i+1 << "] " << scores[i] << " ";
    }
    std::cout << std::endl;
}

void printAverage(const std::vector<double>& scores){
    std::cout << "Average: " << calculateAverage(scores) << std::endl;
}

int main() {
    std::string studentName;
    int studentId;
    std::vector<double> scores;

    studentId = 12345;
    studentName = "Alice";

    scores.push_back(11);
    scores.push_back(12.5);
    scores.push_back(14.75);
    scores.push_back(19);

    printScores(studentName, studentId, scores);

    return 0;
}
  1. What does this code do?

It is difficult to add students and their grades.
We are therefore going to create a Student class with the following three attributes:

  1. Create the Student class with the appropriate methods (including the corresponding .h and .cpp files), and add functions to calculate the average and display the scores.

  2. Modify the main.cpp directives to include student.h and the necessary libraries.

User input (Bonus)

The benefit of having a Student class is to make it easier to enter data for multiple students.
In order to input data for several students, we will write several functions to handle user input.

  1. Create a file user-input.cpp along with the associated header file.

  2. Here are some function declarations to help you:

void inputNumberOfStudents(int& number);
void inputStudentId(int& id);
void inputStudentName(Student& student);
void inputStudentScores(Student& student);
Student createStudentFromInput();
void printAllScoresAndAverages(const std::vector<Student>& students);
  1. Here is the code of inputStudentScores to help you:
void inputStudentScores(Student& student) {
    double score;
    std::cout << "Enter score (-1 to stop): ";
    std::cin >> score;

    while (score != -1) {
        student.appendScore(score);
        std::cout << "Enter score (-1 to stop): ";
        std::cin >> score;
    }
}
  1. Implement the given functions and modify main to retrieve user input and display the grades as well as the averages for each student.

Return to the objectives and check the points you have mastered. Review the points you have not yet fully understood. Ask your instructor for help if needed.