수다닷컴

  • 해외여행
    • 괌
    • 태국
    • 유럽
    • 일본
    • 필리핀
    • 미국
    • 중국
    • 기타여행
    • 싱가폴
  • 건강
    • 다이어트
    • 당뇨
    • 헬스
    • 건강음식
    • 건강기타
  • 컴퓨터
    • 프로그램 개발일반
    • C언어
    • 비주얼베이직
  • 결혼생활
    • 출산/육아
    • 결혼준비
    • 엄마이야기방
  • 일상생활
    • 면접
    • 취업
    • 진로선택
  • 교육
    • 교육일반
    • 아이교육
    • 토익
    • 해외연수
    • 영어
  • 취미생활
    • 음악
    • 자전거
    • 수영
    • 바이크
    • 축구
  • 기타
    • 강아지
    • 제주도여행
    • 국내여행
    • 기타일상
    • 애플
    • 휴대폰관련
  • 프로그램 개발일반
  • C언어
  • 비주얼베이직

C++ 클래스문에 접근제한에 대한 질문입니다^^ 소스해결좀.

사과

2023.04.01

#include iostream//헤더파일
using namespace std;//namepace로 특정영역에서 std사용
//--------------Account.h-----------------
const int NAME_LEN = 20;//입력받을 이름 20으로 제한
const int PHONE_LEN = 20;//입력받을 전화번호 20으로 제한
class Account//클래스 함수 선언
{
private://클래스 내부접근만 허용하겠다는 의미
int id;//id값 저장할 변수 선언
int balance;//금액을 위한 변수 선언
char* name;//이름을 위한 변수 선언
char* phone;//전화번호를 위한 변수 선언
public://클래스 내부,외부 접근을 모두 허용
Account(){}
Account(int id, char* name, char* phone, int balance);//생성자 함수 선언과 동시에 정의
Account(const Account& p);//깊은 복사생성자 선언과 동시에 정의
Account& operator=(const Account& p);
~Account();//소멸자 선언과 동시에 정의
int getid() const;
int getbalance() const;
const char* getname() const;
const char* getphone() const;
void Addmoney(int val);
void Minmoney(int val);
};
//--------------Account.cpp-----------------
Account::Account(int id, char* name, char* phone, int balance)//생성자 함수 선언과 동시에 정의
{
this-id=id;//id변수 자기 참조
this-balance=balance;//balance변수 자기 참조
this-name=new char[strlen(name)+1];//메모리 동적할당
strcpy(this-name, name);//문자형 복사 name변수 자기 참조
this-phone=new char[strlen(phone)+1];//메모리 동적할당
strcpy(this-phone, phone);//문자형 복사 phone변수 자기 참조
}
Account::Account(const Account& p)//깊은 복사생성자 선언과 동시에 정의
{
this-id=p.id;//id변수 자기 참조
this-balance=p.balance;//balance변수 자기 참조
this-name=new char[strlen(p.name)+1];//메모리 동적할당
strcpy(this-name, p.name);//문자형 복사 name변수 자기 참조
this-phone=new char[strlen(p.phone)+1];//메모리 동적할당
strcpy(this-phone, p.phone);//문자형 복사 phone변수 자기 참조
}
Account& Account::operator=(const Account& p)
{
delete[] name;
name=new char[strlen(p.name)+1];
id=p.id;
balance=p.balance;
strcpy(name, p.name);
strcpy(phone, p.phone);
return *this;
}
Account::~Account()//소멸자 선언과 동시에 정의
{
delete []name;//name 메모리 동적할당 해제
delete []phone;//phone 메모리 동적할당 해제
}
int Account::getid() const{//아이디값을 리턴하는 상수화된 함수
return id;
}
int Account::getbalance() const{//잔액값을 리턴하는 상수화된 함수
return balance;
}
const char* Account::getname() const{//이름값을 리턴하는 상수화된 함수
return name;
}
const char* Account::getphone() const{//전화번호값을 리턴하는 상수화된 함수
return phone;
}
void Account::Addmoney(int val){//입금의 명령을 수행하는 함수
balance+=val;//balance와 val값을 더해서 balance에 대입
}
void Account::Minmoney(int val){//출금의 명령을 수행하는 함수
balance-=val;//balance와 val값을 빼서 balance에 대입
}
Account *pArray;
//--------------Account2.h-----------------
class Account2
{
Account* pArray[100];//100개 배열로 된 객체 선언
int index;//index의 값을 0으로 선언
public:
Account2(){//생성자
index=0;//인덱스 값을 0으로 초기화
}
void PrintMenu();//메뉴를 출력해주는 함수
void MakeAccount();//계좌를 개설하는 함수
void Deposit();//계좌를 찾아 입금하는 함수
void Withdraw();//계좌를 찾아 출금하는 함수
void Inquire();//잔액조회 함수
void Inquire(int id);//계좌id로 잔액조회하는 함수
void Inquire(char *name, char *phone);//이름과 전화번호로 으로 잔액조회하는 함수
void Inquireall();//천체고객을 잔액 조회하?조회하는 함수
void Inquiremenu(int i);//조회 결과를 보여주는 함수
enum{MAKE = 1, DEPOSIT, WITHDRAW, INQUIRE, EXIT};//MAKE=1 DEPOSIT=2 WITHDRAW=3 INQUIRE=4 EXIT=5
};
//--------------Account2.cpp-----------------
void Account2::PrintMenu()// 메뉴 출력 함수
{
coutendl ━━━━ 순천향 은행 ━━━━ endl;//내용을 모니터에 출력
cout ① 계좌 개설 endl;//내용을 모니터에 출력
cout ② 입 금 endl;//내용을 모니터에 출력
cout ③ 출 금 endl;//내용을 모니터에 출력
cout ④ 잔액 조회 endl;//내용을 모니터에 출력
cout ⑤ 종 료 endl;//내용을 모니터에 출력
}
void Account2::MakeAccount()//계좌 개설 함수
{
int id;//개설할 정수형id 선언
char name[NAME_LEN];//사용자가 입력한 문자의 길이만큼 배열 선언
char phone[PHONE_LEN];
int balance;//입금액 선언
cout ━━━━ 계좌 개설 ━━━━ endl;//내용을 모니터에 출력
cout 계좌 ID : ; cinid;//아이디값 입력
cout 이 름 : ; cinname;//이름 입력
cout 전화번호 : ; cinphone;//전화번호 입력
cout 입 금 액 : ; cinbalance;//입금액 입력
cout━━계좌개설 완료━━endl;//내용을 모니터에 출력
cout계좌 id : idendl;//계좌 아이디 모니터에 출력
cout이 름 : nameendl;//이름 모니터에 출력
cout전화번호 : phoneendl;//전화번호 모니터에 출력
pArray[index++] = new Account(id, name, phone, balance);
}
void Account2::Deposit()//입급 함수
{
int money;//변수 선언
int id;//변수 선언
cout 계좌ID : ;cinid;//아이디값 입력
cout 입금액 : ;cinmoney;//입금액 입력
for(int i = 0; iindex; i++)//0부터 1씩증가되면서 인덱스값보다 작을때까지 반복문
{
if(pArray[i]-getid() == id)//아이디가 일치하다면 밑의 것을 수행
{
pArray[i]-Addmoney(money);//입력받은 입금액을 잔액에 더하고 저장
cout 잔 액 : pArray[i]-getbalance()endl;//입금하고 남은잔액 출력
cout 입금 완료 endl;//내용을 모니터에 출력
return;//메인함수로 이동
}
}
cout 유효하지 않은 ID입니다. endl;//잘못된 아이디나 없는 아이디일시 출력
}
void Account2::Withdraw()//출금
{
int money;//변수 선언
int id;//변수 선언
cout 계좌ID : ;cinid;//아이디값 입력
cout 출금액 : ;cinmoney;//출금액 입력
for(int i = 0; iindex; i++)//0부터 1씩증가되면서 인덱스값보다 작을때까지 반복문
{
if(pArray[i]-getid() == id)//아이디가 일치하다면 밑의 것을 수행
{
if(pArray[i]-getbalance() money)//잔액보다 출금액이 크면 밑의 것을 수행
{
cout잔액 부족 endl;//내용을 모니터에 출력
return;//메인함수로 이동
}
pArray[i]-Minmoney(money);//입력받은 출금액을 잔액에서 빼고 저장
cout 잔 액 : pArray[i]-getbalance()endl;//출금후에 남은잔액 출력
cout 출금 완료 endl;//내용을 모니터에 출력
return;//메인함수로 이동
}
}
cout 유효하지 않은 ID입니다.endl;//잘못된 아이디나 없는 아이디일시 출력
}
void Account2::Inquire()//잔액 조회
{
int choice;//변수 선언
coutendl━━━━ 잔 액 조 회 ━━━━endl;//내용을 모니터에 출력
cout ① 이름,전화번호로 조회endl;//내용을 모니터에 출력
cout ② 계좌 id로 조회 endl;//내용을 모니터에 출력
cout ③ 전체고객 계좌조회endl;//내용을 모니터에 출력
cout 선택 : ;cinchoice;//잔액조회 메뉴 선택
if(choice==1)//1번 입력시 실행
Inquire(name,phone);//이름조회 함수 호출
else if(choice==2)//2번 입력시 실행
Inquire(1);//아이디로 조회 함수 호출
else if(choice==3)//3번 입력시 실행
Inquireall();//전체조회 함수 호출
else//다른번호 입력시 실행
cout번호를 잘못 입력하였습니다.endl;//내용을 모니터에 출력
}
void Account2::Inquire(char *name, char *phone)//이름으로 잔액 조회하는 함수
{
char name_a[NAME_LEN];//변수 선언
coutendl 이 름 : ; //내용을 모니터에 출력
cinname_a;//조회할 이름 입력

char phone_a[PHONE_LEN];//변수 선언
cout 전화번호 : ; //내용을 모니터에 출력
cinphone_a;//조회할 전화번호 입력

for(int i = 0; iindex; i++)//0부터 1씩 증가되면서 인덱스값보다 작을때까지 반복문
{
if(!strcmp(pArray[i]-getname(),name_a))//입력한 이름이 같을때 민의것을 실행
{
if(!strcmp(pArray[i]-getphone(),phone_a))//입력한 전화번호가 같을때 밑의것을 실행
{
Inquiremenu(i);//조회 결과함수 호출
return;//메인함수로 이동
&nbsbsp;}
}
}
cout 유효하지 않은 이름이나 전화번호입니다.endl;//입력한 이름이 없을때 출력
}
void Account2::Inquire(int id)//아이디로 잔액 조회하는 함수
{
coutendl 계좌ID : ;cinid;//조회할 아이디 입력
for(int i = 0; iindex; i++)//0부터 1씩 증가되면서 인덱스값보다 작을때까지 반복문
{
if(pArray[i]-getid() == id)//입력한 아이디와 같을때 밑의것을 실행
{
Inquiremenu(i);//조회 결과함수 호출
return;//메인함수로 이동
}
}
cout 유효하지 않은 ID입니다. endl;//입력한 아이디가 없을때 출력
}
void Account2::Inquireall()//전체 잔액 조회 함수
{
for(int i=0; iindex; i++)//0부터 1씩 증가되면서 인덱스값보다 작을때까지 반복문
{
Inquiremenu(i);//조회 결과 함수 호출
}
}
void Account2::Inquiremenu(int i)//조회 결과 함수
{
coutendl 계 좌 ID : pArray[i]-getid()endl;//잔액조회한 아이디 출력
cout 이 름 : pArray[i]-getname()endl;//잔액조회한 이름 출력
cout 전화번호 : pArray[i]-getphone()endl;//잔액조회한 전화번호 출력
cout 잔 액 : pArray[i]-getbalance()endl;//잔액조회한 잔액 출력
cout 조회 완료 endl;//내용을 모니터에 출력
}
//--------------Data.h-----------------
class Data
{
int data;
public:
Data(int d){
data=d;
}
void ShowData(){
coutdataendl;
}
};
//--------------Container.h-----------------
typedef Data* Element;
class Container
{
private:
Element* arr;
int length;
int aIndex;
public:
Container(int len=50);
~Container();
void Insert(Element data);
Element Remove(int idx);

Element GetItem(int idx);
int GetElemSum() {return aIndex;}
};
//--------------Container.cpp-----------------
Container::Container(int len):aIndex(0)
{
if(len=0)
len=50;
length=len;
arr=new Element[length];
}
Container::~Container()
{
delete[] arr;
}
void Container::Insert(Element data)
{
if(aIndex==length)
{
cout저장할 공간이 없습니다!endl;
return;
}
arr[aIndex++]=data;
}
Element Container::Remove(int idx)
{
if(idx0 || idx=aIndex)
{
cout존재하지 않는 요소 입니다!endl;
return NULL;
}
Element del=arr[idx];

for(int i=idx; iaIndex-1; i++)
arr[i]=arr[i+1];
aIndex--;

return del;
}
Element Container::GetItem(int idx)
{
if(idx0 || idx=aIndex)
{
cout존재하지 않는 요소입니다!endl;
return NULL;
}
return arr[idx];
}
//--------------strings.h-----------------
class strings
{
int len;
char* str; //---------------------------------------------요기요
public:
strings(const char* s=NULL);
strings(const strings& s);
~strings();
strings& operator=(const strings& s);
strings& operator+=(const strings& s);
bool operator==(const strings& s);
strings operator+(const strings& s);

friend ostream& operator(ostream& os, const strings& s);//-----------friend 선언
friend istream& operator(istream& is, strings& s);
};
//--------------strings.cpp-----------------
strings::strings(const char* s)
{
len=(s!=NULL ? strlen(s)+1 : 1);
str=new char[len];

if(s!=NULL)
strcpy(str, s);
}
strings::strings(const strings& s)
{
len=s.len;
str=new char[len];
strcpy(str, s.str);
}
strings::~strings()
{
delete []str;
}
strings& strings::operator=(const strings& s)
{
delete []str;
len=s.len;
str=new char[len];
strcpy(str, s.str);
return *this;
}
strings& strings::operator+=(const strings& s)
{
len=len+s.len-1;
char* tStr=new char[len];
strcpy(tStr, str);
delete []str;

strcat(tStr, s.str);
str=tStr;
return *this;
}
bool strings::operator==(const strings& s)
{
return strcmp(str, s.str)? false:true;
}
strings strings::operator+(const strings& s)
{
char* tStr=new char[len+s.len-1];
strcpy(tStr, str);
strcat(tStr, s.str);

strings temp(tStr);
delete []tStr;
return temp;
}
ostream& operator(ostream& os, const strings& s) //-------전역함수로 정의(strings클래스에 firend로 선언 함)
{
oss.str; //---------------------------------- str변수에 접근이 불가능하다는 에러발생
returnos;
}
istream& operator(istream& is, strings& s){
char str[100];
isstr;

s=strings(str);
return is;
}
//--------------main.cpp-----------------
int main(void)//메인 함수
{
Account2 p;//자료 선언
int choice;//정수형 choice변수 선언
int size=100;//size의 값을 100으로 초기화
pArray=new Account[size];//배열의 동적 할당
while(1)//반복문
{
p.PrintMenu();//메뉴를 출력하는 함수
cout 선택 : ;//내용을 모니터에 출력
cinchoice;//choice값을 입력
switch(choice)//메뉴 선택문
{
case p.MAKE ://메뉴에서 1번 입력시 실행
p.MakeAccount();//계좌개설 함수 호출
break;//switch문을 빠져나감
case p.DEPOSIT ://메뉴에서 2번 입력시 실행
p.Deposit();//입금 함수 호출
break;//switch문을 빠져나감
case p.WITHDRAW ://메뉴에서 3번 입력시 실행
p.Withdraw();//출금 함수 호출
break;//switch문을 빠져나감
; case p.INQUIRE ://메뉴에서 4번 입력시 실행
p.Inquire();//잔액조회 함수 호출
break;//switch문을 빠져나감
case p.EXIT ://메뉴에서 5번 입력시 선택
return 0;//프로그램 종료
default ://메뉴에서 잘못 입력시 선택
cout Illegal selection.. endl;//내용을 모니터에 출력
break;//switch문을 빠져나감
}
}
delete []pArray;//할당된 메모리 소멸
return 0;//프로그램 종료
}이게 전체 소스인데요.. 마지막쯤에
strings 클래스에서요 str 변수를 privat으로 선언해주고..
ostream& operator(ostream& os, const strings& s) 함수를 string 클래스에 friend선언 해주었는데
ostream& operator(ostream& os, const strings& s)함수를 정의할때 str접근이 불가능하다고
에러가 발생하네요 ... 어디가 문제인지 찾아주세요.ㅠㅠ
friend 선언해주면 privat에선언되있는것도 접근이 가능하지 않나요???

신청하기





COMMENT

댓글을 입력해주세요. 비속어와 욕설은 삼가해주세요.

  • 블1랙캣

    응?? 나만 이상없이 잘 돌아가는건가.ㅡㅡ??;;

번호 제 목 글쓴이 날짜
2696504 플래시 위에 div 올리기 (5) 큰꽃늘 2025-05-30
2696458 제가 만든 소스 한번 봐주시고 수정 할 꺼 있으면 말해주세요. (실행은 되지만 깜빡거리네요) 이플 2025-05-29
2696434 퍼센트 레이아웃 질문인데요.. 나츠 2025-05-29
2696372 %=open_main%, %=open_sub% 가 뭘까요? (9) 행복녀 2025-05-29
2696347 콘솔 프로그램 질문 상큼한캔디 2025-05-28
2696320 c언어 scanf 함수를 이요해 문자열 입력 받을 시 질문 있습니다. 슬아라 2025-05-28
2696292 익스플로러9이상에서만 이상한 보더가 보이는데 삭제할수 있나요? 망고 2025-05-28
2696263 프로그래밍 공부시작 질문 (6) 진이 2025-05-28
2696206 SK2의 플래시를 밴치마킹하려고하는데요.. (1) 비내리던날 2025-05-27
2696179 ie7에서 사라지지가 않네요. (2) 빛길 2025-05-27
2696150 div에 스크롤 생기게 하려면... (2) 에드가 2025-05-27
2696123 자료구조론 공부중인데 김자영 2025-05-26
2696094 exe 파일 제철 2025-05-26
2696043 제이쿼리 .scroll() 관련 질문드립니다 이거이름임 2025-05-26
2695984 마크업상으로 하단에 있으나 우선적으로 이미지파일을 다운로드받는 방법 (1) 들꿈 2025-05-25
2695934 tr 속성값 (9) 새 2025-05-25
2695905 ASP로 개발됐을 때 css가 달라져요 ㅠㅠ (4) 슬아라 2025-05-24
2695878 form을 이용한 다른 페이지로 넘기는 방법을 알려주세요 (1) 핫파랑 2025-05-24
2695844 저기 암호화 및 복호화 프로그램.. 만들어볼려는대 (2) 한빛 2025-05-24
2695814 [질문] PDA에서 애플릿이 가능한가요? (1) 봄시내 2025-05-24
<<  이전  1 2 3 4 5 6 7 8 9 10  다음  >>

수다닷컴 | 여러분과 함께하는 수다토크 커뮤니티 수다닷컴에 오신것을 환영합니다.
사업자등록번호 : 117-07-92748 상호 : 진달래여행사 대표자 : 명현재 서울시 강서구 방화동 890번지 푸르지오 107동 306호
copyright 2011 게시글 삭제 및 기타 문의 : clairacademy@naver.com