###### tags: `Code` `note`
# 物件導向程式設計功課
:::info
:man: 老師:戴文凱
:100: 學分:3
:::
# Week1 Feb 23 ,2021
## [ComputeSQRT]()
- main.cpp
```c=
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main() {
double n, guess, last_guess;
cin >> n;
last_guess = 0;
while (!cin.eof()) {
guess = n / 2.0;
while (!(abs(guess - last_guess) < 0.01)) {
last_guess = guess;
guess = (guess + n / guess) / 2;
}
cout << fixed << setprecision(2) << guess << endl;
cin >> n;
}
}
```
# Week2 Mar 2 ,2021
# Week3 Mar 9 ,2021
# Week4 Mar 16 ,2021
# Week5 Mar 23 ,2021
## [Hot dog stand](https://github.com/jimmy1010ww/NTUST_109-2_ODD/tree/main/CS3005301HW05/TS0501)
- main.cpp
```c=
#include <iostream>
#include "HotDogStand.h"
using namespace std;
int main() {
HotDogStand stand1("Stand1", 0);
HotDogStand stand2("Stand2", 100);
HotDogStand stand3("Stand3");
stand1.justSold();
stand2.justSold();
stand3.justSold();
stand1.print();
stand2.print();
stand3.print();
std::cout << "Total Sold : " << HotDogStand::allStandSoldAmount() << std::endl;
return 0;
}
```
- HotDogStand.h
``` c=
#ifndef HotDogStand_H
#define HotDogStand_H
#include <iostream>
#include <string>
using namespace std;
class HotDogStand
{
public:
HotDogStand(const char *id); //Construct a HotDogStand with the given ID and set the sales volume to 0.
HotDogStand(const char *id, int amount); //Construct a HotDogStand with the given ID and sales volume.
void justSold(); //Increase the hot dog sales volume by 1.
void print(); //Print the ID and sales volume of the store separated by spaces.
int thisStandSoldAmount(); //Return the sales volume of the store.
static int allStandSoldAmount(); //Return the total sales volume of all stores.
private:
char *standId; //The ID of the store.
int hotDogSellAmount; //The hot dog sales volume of the store.
static int totalSellAmount; //The total hot dog sales volume of all stores.
};
#endif
```
- HotDogStand.cpp
```c=
#include "HotDogStand.h"
int HotDogStand::totalSellAmount = 0;
HotDogStand::HotDogStand(const char * id)
{
HotDogStand::hotDogSellAmount = 0;
HotDogStand::standId = (char*)id; //remove const
}
HotDogStand::HotDogStand(const char * id, int amount)
{
HotDogStand::hotDogSellAmount = amount;
HotDogStand::totalSellAmount += amount;
HotDogStand::standId = (char*)id; //remove const
}
void HotDogStand::justSold()
{
HotDogStand::hotDogSellAmount++;
HotDogStand::totalSellAmount++;
}
void HotDogStand::print()
{
cout << HotDogStand::standId << " " << HotDogStand::hotDogSellAmount << endl;
}
int HotDogStand::thisStandSoldAmount()
{
return HotDogStand::hotDogSellAmount;
}
int HotDogStand::allStandSoldAmount()
{
return HotDogStand::totalSellAmount;
}
```
## [Design Month Class](https://github.com/jimmy1010ww/NTUST_109-2_ODD/tree/main/CS3005301HW05/TS0502)
- main.cpp
```c=
#include <iostream>
#include "Month.h"
using namespace std;
int main(void)
{
Month month1, month2(2), month3('M','a','r'), month4, month5, month6;
month4 = month3.nextMonth();
month5.inputInt();
month6.inputStr();
cout << "Month1 = ";
month1.outputInt();
cout << ' ';
month1.outputStr();
cout << endl;
cout << "Month2 = ";
month2.outputInt();
cout << ' ';
month2.outputStr();
cout << endl;
cout << "Month3 = ";
month3.outputInt();
cout << ' ';
month3.outputStr();
cout << endl;
cout << "Month4 = ";
month4.outputInt();
cout << ' ';
month4.outputStr();
cout << endl;
cout << "Month5 = ";
month5.outputInt();
cout << ' ';
month5.outputStr();
cout << endl;
cout << "Month6 = ";
month6.outputInt();
cout << ' ';
month6.outputStr();
cout << endl;
return 0;
}
```
- Month.h
```c=
#ifndef Month_H
#define Month_H
#define MONTH_LEN 12 //one year have 12 months
#define MONTH_ABBREVIATION_LEN 3 //abbreviation month lenth
#include <iostream>
#include <string>
using namespace std;
class Month
{
public:
Month();
Month(char first, char second, char third);
Month(int monthInt);
void inputInt();
void inputStr();
void outputInt();
void outputStr();
Month nextMonth();
private:
int month;
};
#endif // !Month_H
```
- Month.cpp
```c=
#include "Month.h"
const string MONTH_NAME[MONTH_LEN] = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
Month::Month()
{
Month::month = 1;
}
Month::Month(char first, char second, char third)
{
if (first == 'J' && second == 'a' && third == 'n')
Month::month = 1;
else if (first == 'F' && second == 'e' && third == 'b')
Month::month = 2;
else if (first == 'M' && second == 'a' && third == 'r')
Month::month = 3;
else if (first == 'A' && second == 'p' && third == 'r')
Month::month = 4;
else if (first == 'M' && second == 'a' && third == 'y')
Month::month = 5;
else if (first == 'J' && second == 'u' && third == 'n')
Month::month = 6;
else if (first == 'J' && second == 'u' && third == 'l')
Month::month = 7;
else if (first == 'A' && second == 'u' && third == 'g')
Month::month = 8;
else if (first == 'S' && second == 'e' && third == 'p')
Month::month = 9;
else if (first == 'O' && second == 'c' && third == 't')
Month::month = 10;
else if (first == 'N' && second == 'o' && third == 'v')
Month::month = 11;
else if (first == 'D' && second == 'e' && third == 'c')
Month::month = 12;
else
Month::month = 1;
}
Month::Month(int monthInt)
{
if (monthInt < 1 || monthInt > 12)
{
Month::month = 1;
}
else
{
Month::month = monthInt;
}
}
void Month::inputInt()
{
int x = 0;
cin >> x;
if (x < 1 || x > 12)
{
Month::month = 1;
}
else
{
Month::month = x;
}
}
void Month::inputStr()
{
char inputMonth[MONTH_ABBREVIATION_LEN];
for (size_t i = 0; i < MONTH_ABBREVIATION_LEN; i++)
{
cin >> inputMonth[i];
}
int count = 0;
for (size_t i = 0; i < MONTH_LEN; i++)
{
for (size_t j = 0; j < MONTH_ABBREVIATION_LEN; j++)
{
if (inputMonth[j] == MONTH_NAME[i][j])
{
count++;
}
}
if (count == 3)
{
Month::month = ((i == 0) ? 1 : i + 1);
}
count = 0;
}
}
void Month::outputInt()
{
cout << Month::month;
}
void Month::outputStr()
{
cout << MONTH_NAME[(Month::month - 1) == 0 ? 0 : (Month::month - 1)];
}
Month Month::nextMonth()
{
int nextmonth = Month::month + 1;
if (Month::month + 1 == 13)
{
return Month(1);
}
else
{
return Month(nextmonth);
}
}
```