모닝의 컴퓨터 스토리 :: [c++] 구조체와 클래스 (tistory.com)
[c++] 구조체와 클래스
구조체(Struct) c언어나 다른언어에서도 많이 사용되는건데 저번 포스팅에서도 나왔을듯이 사용방법과 형태는 다양하게 활용할 수 있다. 이 코드에서는 TV구조체에 전원,채널,볼륨을 만들었고 구
morning97.tistory.com
내 코드(구조체 // tv)
#include <iostream>
using namespace std;
struct tv {
public:
int ch;
int vol;
int MaxCh = 100;
int MinCh = 1;
int MaxVol = 100;
int MinVol = 0;
void volp(int vol);
void volm(int vol);
void chp(int ch);
void chm(int ch);
void initMem(int ch, int vol);
void show_Info();
void power(bool t);
};
void tv::power(bool t)
{
if (t == true)
cout << "=====전원 on=====" << endl;
else
cout << "=====전원 off======" << endl;
}
void tv::initMem(int ch, int vol)
{
for (;;)
{
if (ch>=0&&ch<=100 &&vol<=100 && vol>= 0)
{
this->ch = ch;
this->vol = vol;
break;
}
else
{
cout << "범위를 벗어난 볼륨이거나 채널입니다." << endl;
cout << "ch: ";
cin >> ch;
cout << "vol: ";
cin >> vol;
return initMem(ch, vol);
}
}
}
void tv::volp(int vol)
{
this->vol +=vol;
if (this->vol >= MaxVol)
{
this->vol = MaxVol;
}
}
void tv::volm(int vol)
{
this->vol -= vol;
if (this->vol <= MinVol)
{
this->vol = MinVol;
}
}
void tv::chp(int ch)
{
this->ch += ch;
if (this->ch > MaxCh)
this->ch = MinCh;
}
void tv::chm(int ch)
{
this->ch -= ch;
if (this->ch < MinCh)
this->ch = MaxCh;
}
void tv::show_Info()
{
cout << "ch: " << ch << endl;
cout << "vol: " << vol << endl;
}
int main(void)
{
tv remote;
remote.power(true);
remote.initMem(1, 150);
remote.show_Info();
remote.chp(10);
remote.volp(50);
remote.show_Info();
remote.volm(1000);
remote.show_Info();
remote.power(false);
system("pause");
}
내 코드 (클래스 /// TV)
#include<iostream>
using namespace std;
class rm_controller {
private:
int vol;
int ch;
bool on_off;
int MaxVol = 100;
int MinVol = 0;
int MaxCh = 100;
int MinCh = 1;
public:
rm_controller(int vol, int ch);
~rm_controller() { cout<< "생성자 소멸" << endl; } // 왜 안나올까? 자동으로 나와야하는거아님?
void volP(int vol);
void volM(int vol);
void chP(int ch);
void chM(int ch);
void Power(bool on_off);
void showInfo();
};
rm_controller::rm_controller(int vol, int ch)
{
cout << "생성자 생성" << endl;
this->vol = vol;
this->ch = ch;
}
void rm_controller::volP(int vol)
{
this->vol += vol;
if (this->vol > 100)
this->vol = 100;
}
void rm_controller::volM(int vol)
{
this->vol -= vol;
if (this->vol < 0)
this->vol = 0;
}
void rm_controller::chP(int ch)
{
this->ch += ch;
if (this->ch > MaxCh&&this->ch<=1000)
this->ch = this->ch/100; // 나머지로 101 -> 1번 ,102 -> 2번.... 1000 -> 100번
}
void rm_controller::chM(int ch)
{
this->ch -= ch;
if (this->ch < MinCh && this->ch > -100)
{
this->ch = -this->ch; // -로 갈 시 + 변환,
}
}
void rm_controller::Power(bool on_off)
{
this->on_off = on_off;
if (this->on_off == 1)
{
cout << "TV ON" << endl;
}
else
cout << "TV OFF" << endl;
}
void rm_controller::showInfo()
{
cout << "************************" << endl;
cout << "tv 상태" << endl;
cout << "=======================" << endl;
cout << "켜짐 여부: ";
rm_controller::Power(on_off);
cout << "=======================" << endl;
cout << "ch: " << ch << endl;
cout << "=======================" << endl;
cout << "vol: " << vol << endl;
cout << "************************" << endl;
}
int main(void)
{
rm_controller remot(10, 10);
remot.Power(true);
remot.chM(20);
remot.showInfo();
remot.chP(100);
remot.volP(5);
remot.showInfo();
remot.Power(false);
remot.showInfo();
system("pause");
}
소멸자 자동으로 실행되는거 아닌가?