본문 바로가기

과제모음

[C++]소멸자2(Destructor)

반응형


>>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;
}

반응형

'과제모음' 카테고리의 다른 글

[C++]Const & this & Static 변수  (0) 2010.01.22
[C++]상수(Const)  (0) 2010.01.22
[C++]소멸자(Destructor)  (0) 2010.01.22
[C++]생성자(Constructor)  (0) 2010.01.22
[C++]객체지향2  (0) 2010.01.22