기본 콘텐츠로 건너뛰기

C++ A4 commented header file

// Try to not put any special characters in your file names (+, spaces or such)
// For class files (header and cpp file), it is a good to give them the same name as the Class (here Account.h and Account.cpp)

// Think about ALWAYS putting the header code into these things, as it will protect your code from double/multi inclusion (VERY IMPORTANT!!! EVEN MORE SO IN BIGGER PROJECTS!!!)
#ifndef __ACCOUNT_HPP__
#define __ACCOUNT_HPP__
#include <iostream>
// BAD PRACTICE!!! AND ESPECIALLY NOT IN A HEADER PLEASE! There are A LOT of things in std, so if for example another library has a function with the same name, it can cause important problems!!
// See cpp file for an alternative form
//using namespace std;
// Always begins with a Capital Letter!!!
// Also, prefer to put your code in the appropriated cpp files, it is a lot cleaner, readable and maintainable
class Account {
private :
 int cstNum; // cst = customer
 int cstBal;//balance
 int cstChrg;//charge
 int cstCredit;
 int cstCreditLimit;
 // unused variable (even my IDE says it!)
 int cstNewBalance;
public :
 // Try to always define at least the constructor or destructor, even if they don't have anything in them (even though it is a good practive to initialize your variables there)
 Account() {
  cstNum = 0;
  cstBal = 0;
  cstChrg = 0;
  cstCredit = 0;
  cstCreditLimit = 0;
  cstNewBalance = 0;
 }
 // Destructor (always called when deinitializing the object), I don't think we spoke about it in the class, so don't worry too much about it for now
 ~Account() {}
 // camelCase is prefered, but that is just because it is in the general coding convention, so it is not so bad if you don't do it, but it is still better :P
 void setNum(int num) {
  cstNum = num;
 }
 // you have to take doubles as parameters, else you won't be able to get decimals values
 void setBal(double bal) {
  cstBal = static_cast<int>(bal * 100);
 }
 void setChrg(double chrg) {
  cstChrg = static_cast<int>(chrg * 100);
 }
 void setCredit(double credit) {
  cstCredit = static_cast<int>(credit * 100);
 }
 void setCreditLimit(double creditLimit) {
  cstCreditLimit = static_cast<int>(creditLimit * 100);
 }
 // not a very good way to do it, just make it a getter in that case, because if the user forgots to set the variable (like you did), it is not gonna work correctly
 // unused function (IDE says it!)
 void setNewBalance() {
  // gives out a warning: values of type double may not fit into an integer
  cstNewBalance = (cstBal + cstCredit - cstChrg) * 0.01;
 }
 // in our case, a getter probably does the job too, and so we don't need to store it in a separate field.
 double getNewBalance() const {
  // wrong way to calculate it!! here corrected version:
  return (cstBal + cstChrg - cstCredit) * 0.01;
 }
 // you can add const behind the function, as we don't really need to set anything in this function
 void getExceed() const {
  double newBalance  = getNewBalance();
  double creditLimit = cstCreditLimit / 100.0;
  // be careful, you forgot to divide cstCreditLimit by 100 (as you multiplied it by 100 at the beginning), or to not divide cstNewBalance by 100
  // here I just calculated once the new balance and got the result, and then calculated the double version of the creditLimit, you can also do it the other way around if you want
  if (newBalance > creditLimit) {
   std::cout << "new balance is" << newBalance << ", " << cstNum << "'s credit limit is exceed" << std::endl;
  } else {
   std::cout << "new balance is" << newBalance << std::endl;
  }
  // another to do it in another way, which one you want to use is up to you, I just wanted to show you it is possible
  std::cout << "new balance is" << newBalance; // <-- no newline so we can continue to put stuff behind it
  if (newBalance > creditLimit) {
   std::cout << ", " << cstNum << "'s credit limit is exceed"; // <-- put something behind it
  }
    std::cout << std::endl; // <-- finish our line
 }
};
#endif // __ACCOUNT_HPP__

댓글

이 블로그의 인기 게시물

JS 5.5 task6 Create an extendable calculator

<HTML>   <body>   </body>   <script> function Calculator() {   let methods = {     "-" : (a, b) => a - b,     "+" : (a, b) => a + b   };   //methods is an object which keeps key for operators   //and value to return the actual operation values   //each returns the result of operation that key(operator) does   this.calculate = function (str){     //calculate is one element in the function Calculator     //it takes the string and returns the value     //in the function element list is delimeted by , not ;     let split = str.split(" "),     a = +split[0],     op = split[1],     b = split [2]     if(!methods[op] || isNaN(a) || isNaN(b)) {       return NaN; // error handling     }     return methods[op](a,b);   }   this.addMethod = function(name, func){     methods[name] = func;     //this is how to add new key and ele to object   } } let powerCalc = new Calculator; powerCalc.addMethod("*&

JS 5.7 task5 Store read dates

<HTML>   <body>   </body>    <script>    let messages = [        {text: "Hello", from: "John"},        {text: "How goes?", from: "John"},        {text: "See you soon", from: "Alice"}    ];    let readMap = new WeakMap();    alert(readMap.size);    readMap.set(messages[0], new Date(2019, 3, 5));   </script> </HTML> <!-- task4 needed weakSet to save simply readmessage, this task needs to save THE TIME IT WAS READ along with the message itself the message out of the set or map means it hasn't been read I kinda feel good and bad at the same time to happen to read the solution but I do get to think more about the difference with tasks and be more available to understand the main contents so I think, its good? -->

How to set base url when deployed in Heroku? : base url and axios

https://stackoverflow.com/questions/47164330/axios-api-calls-in-heroku/47165888 baseUrl = process.env.baseURL || "http://localhost:5000" Even more stable way https://stackoverflow.com/questions/52129849/how-to-get-the-base-url-variable-on-a-deployed-heroku-node-app const production  = 'https://examplePage.com'; const development = 'http://localhost:3000/'; const url = (process.env.NODE_ENV ? production : development); process.env.NODE_ENV will resolve to undefined if you are running on localhost production mode. and return production if you have deployed the app production mode.