24.12.23 (월)

2024. 12. 23. 15:21·내배캠/TIL

배터리 관리 클래스

*BatteryController.h

더보기
#ifndef BatteryController_H_
#define BatteryController_H_

class Battery
{
public:
	Battery(int DefalutChargeLevel = 100);

	int GetCurrentLevel();
	void UseBattery();
	void ChargeBattery();

private:
	int CurrentBatteryLevel;
	int UseLevel;
	int ChargeLevel;
};

#endif //!BatteryController_H_

 

*BatteryController.cpp

더보기
#include "BatteryController.h"
#include <iostream>

using namespace std;

Battery::Battery(int DefalutChargeLevel)
{
	CurrentBatteryLevel = DefalutChargeLevel;
	UseLevel = 5;
	ChargeLevel = 7;

	cout << "Initail charge: " << GetCurrentLevel() << "%" << endl;
}

int Battery::GetCurrentLevel()
{
	return CurrentBatteryLevel;
}

void Battery::UseBattery()
{
	CurrentBatteryLevel -= UseLevel;
	if (CurrentBatteryLevel < 0)
	{
		CurrentBatteryLevel = 0;
	}

	cout << "Battery used. Current Charge: " << GetCurrentLevel() << "%" << endl;
}

void Battery::ChargeBattery()
{
	CurrentBatteryLevel += ChargeLevel;
	if (CurrentBatteryLevel > 100)
	{
		CurrentBatteryLevel = 100;
	}

	cout << "Battery charged. Current charge: " << GetCurrentLevel() << "%" << endl;
}

 

*Solution.cpp

더보기
#include "BatteryController.h"
#include <iostream>

using namespace std;


int main()
{
	Battery A;

	A.UseBattery();
	A.UseBattery();

	A.ChargeBattery();

	A.UseBattery();
	
	return 0;
}

 

*실행결과

 

 

생성자는 객체가 생성될 때 호출되는 함수이다 보니, 생성자 내부에는 같은 클래스 내에 구현된 멤버 함수를 사용하지 못할 것으로 짐작하고 있었는데 결과적으로 호출이 가능했다.

 

객체가 생성되면, 생성자는 해당 객체의 초기화를 담당하는데 이 시점에서 클래스의 멤버 변수와 멤버 함수는 이미 메모리에 로드되어 있고, 사용할 수 있는 상태이다.

따라서 생성자 내부에서 멤버 함수를 호출하면, 생성된 객체를 기준으로 해당 함수가 실행된다.

 

 

 

 

 

 

두 분수의 곱셉을 기약 분수로 반환하는 클래스

*Fraction.h

더보기
#ifndef Fraction_H_
#define Fraction_H_

class Fraction
{
public:
	Fraction(int DefNumerator = 0, int DefDenorminator = 1);

	void InputNumber();
	void DisplayFaction();
	void Simplify();
	int GCD();
	void FractionMultiply(const Fraction& Other);

private:
	int Numerator; 
	int Denorminator; 

};

#endif //!Faction_H_

 

*Fraction.cpp

더보기
#include "Fraction.h"
#include <iostream>	

using namespace std;

Fraction::Fraction(int DefNumerator, int DefDenorminator)
{
	Numerator = DefNumerator;
	Denorminator = DefDenorminator;
}

void Fraction::InputNumber()
{
	cin >> Numerator >> Denorminator;
}

void Fraction::DisplayFaction()
{
	cout << Numerator << "/" << Denorminator;
}

void Fraction::Simplify()
{
	int Divisor = GCD();

	Numerator /= Divisor;
	Denorminator /= Divisor;
}

int Fraction::GCD()
{
	int a = Numerator; int b = Denorminator;
	while (b != 0)
	{
		int temp = b;
		b = a % b;
		a = temp;
	}

	return a;
}

void Fraction::FractionMultiply(const Fraction& Other)
{
	Numerator *= Other.Numerator;
	Denorminator *= Other.Denorminator;
}

 

*Solution.cpp

더보기
#include "Fraction.h"
#include <iostream>

using namespace std;


int main()
{
	Fraction F1(1, 2);
	Fraction F2(3, 4);

	F1.FractionMultiply(F2);
	F1.Simplify();

	cout << "곱한 결과: "; 
	F1.DisplayFaction();

	
	return 0;
}

 

*실행결과

 

두 분수를 곱하고 기약 분수 상태로 만들기 위해서 최대공약수를 알 필요가 있었고, 최대공약수를 효율적으로 구할 수 있는 유클리드 알고리즘에 대해 알게 되었다.

유클리드 알고리즘에 대한 정리:https://dong-grae.tistory.com/51

 

유클리드 알고리즘

유클리드 알고리즘두 정수의 최대공약수(GCD, Greatest Common Divisor)를 구하는 효율적인 방법이다. 기본 원리는 두 수의 최대공약수는, 두 수의 모듈러 값과 작은 숫자의 최대 공약수와 같다는 성질

dong-grae.tistory.com

 

 

 

 

 

Quick Sort에 대한 공부

https://dong-grae.tistory.com/54

 

Quick Sort

#include #include #include using namespace std;string solution(string s) { sort(s.begin(), s.end(), greater()); return s;}이와 같이 헤더의 sort함수를 사용해 쉽게 값을 구할 수 있었지만, 내부 구현이 어떻게 되는지 궁금하

dong-grae.tistory.com

 

'내배캠 > TIL' 카테고리의 다른 글

24.12.25 (수)  (1) 2024.12.25
24.12.24 (화)  (0) 2024.12.24
24.12.21 (토)  (2) 2024.12.21
24.12.20 (금)  (0) 2024.12.20
24.12.19 (목)  (0) 2024.12.19
'내배캠/TIL' 카테고리의 다른 글
  • 24.12.25 (수)
  • 24.12.24 (화)
  • 24.12.21 (토)
  • 24.12.20 (금)
동그래님
동그래님
  • 동그래님
    개발자 동그래
    동그래님
  • 전체
    오늘
    어제
    • 분류 전체보기 (210)
      • 공부 (51)
        • Code Cata (50)
      • 내배캠 (151)
        • TIL (50)
        • C++ (37)
        • Unreal Engine (48)
        • GAS(Gameplay Ability System.. (16)
      • Project (7)
        • Gunfire Paragon (5)
        • Arena Fighters (1)
  • 블로그 메뉴

    • 링크

    • 공지사항

    • 인기 글

    • 태그

    • 최근 댓글

    • 최근 글

    • hELLO· Designed By정상우.v4.10.3
    동그래님
    24.12.23 (월)
    상단으로

    티스토리툴바