과제모음
[C++]소멸자2(Destructor)
Ethical Hacker
2010. 1. 22. 15:37
>>Rand.h<<
#ifndef _Dest_H_
#define _Dest_H_
class Rand
{
public:
Rand(int n);
void SavRnd();
void Output();
void findCnt();
~Rand();
private:
int size;
char *pAC;
char find;
int Cnt;
};
#endif
>>main.cpp<<
#include <iostream>
#include "Dest.h"
using std::cin; using std::cout;
int main()
{
int alphaNum;
cout << "알파벳 개수 입력 : ";
cin >> alphaNum;
Rand alcnt(alphaNum);
alcnt.SavRnd();
alcnt.Output();
alcnt.findCnt();
return 0;
}
>>Rand.cpp<<
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <iomanip>
#include "Dest.h"
using std::cin; using std::cout; using std::endl; using std::setw;
Rand::Rand(int n)
{
size = n;
pAC = new char [size];
cout << "동적 메모리 생성\n";
}
void Rand::SavRnd()
{
srand(time(NULL));
for(int i=0; i<size; i++)
{
pAC[i] = rand()%26+65;
}
}
void Rand::Output()
{
cout << "\n\n*** 랜덤 알파벳 출력 ***\n\n";
for(int i=0; i<size; i++)
{
cout << pAC[i] << setw(5);
}
}
void Rand::findCnt()
{
cout << "\n\n\n검색 알파벳 입력 : ";
cin >> find;
if (find >=97 || find <=122)
{
find -=32;
}
Cnt=0;
for(int i=0; i<size; i++)
{
if(pAC[i] == find)
Cnt++;
}
cout << "\n[" << find <<"]문자의 개수는 [" << Cnt << "]개 입니다.\n";
}
Rand::~Rand()
{
cout << "동적 메모리 소멸\n";
delete [] pAC;
}