러닝센터 7강 복습 - 생성자, 오버로드

===[ 생성자 ]============================================================

#include 
#include 
using namespace std;

class ItemInfo{

private:
 string _name;
 int _damage;
 int _price; 

public :

 // 생성자 : 객체가 생성 되면서 호출되는 메소드(함수) 
 // 생성자는 클래스명과 같아야 한다.
 // 생성자는 시작과 동시에 호출되어 포함된 코드를 실행한다.(단 1회)
 ItemInfo(string name, int damage, int price){
  // 설정함수 호출
  SetInfoItem(name, damage, price); // main에서 생성자 선언과 동시에 해당값을 넣어 줄 수 있음.
 }
 
 // 설정함수. 
 void SetInfoItem(string name, int damage, int price){
  _name = name;
  _damage = damage;
  _price = price;
 }

 void ShowItemInfo(){
  cout << " 아이템명 : " << _name << endl;
  cout << " 데미지 : " << _damage << endl;
  cout << " 가격 : " << _price << endl;
 }
};

void main(){
 // 생성자를 호출 하면서 객체를 생성함
 ItemInfo* iteminfo = new ItemInfo("불타는검", 200, 1000); 
 // 출력 함수 호출
 iteminfo->ShowItemInfo();
 cout << endl;

 // 설정함수 통하여 기존 맴버변수 값 재설정
 iteminfo->SetInfoItem("영롱한지팡이", 5000, 50000);
 // 출력 함수 호출
 iteminfo->ShowItemInfo();

 // *생성자 함수는 최초 프로그램 실행시 맴버변수의 초기값을 설정 하는데 쓰인다.

/*
+------------------------------------------------------------------------------------------------------

       [ new 키워드를 사용하지 않고 객체 호출하는 코드 ]
        ItemInfo iteminfo = ItemInfo("불타는검", 500, 5000);
 iteminfo.ShowItemInfo();

 cout << endl;
 iteminfo.SetItemInfo("망자의 검", 700, 75500);
 iteminfo.ShowItemInfo();

+------------------------------------------------------------------------------------------------------
*/

}


===[ 오버로드 ]===========================================================
#include <iostream> #include <string> using namespace std; class OverLoad{ private: string _name; string _color; int _damage; int _price; int _length; public: void SetInfo(string name, string color, int damage, int price, int length){ _name = name; _color = color; _damage = damage; _price = price; _length = length; } // 오버로드는 같은 이름의 메소드를 매개변수만 다르게 하여 호출하는 방식이다. void Shot(string name, int damage){ cout << name << " 의 위력은 " << damage << " 이다." << endl; } void Shot(string name, int damage, string color){ cout << name << " 의 위력은 " << damage << " 이고, " << color << " 이다. " << endl; } void Shot(string name, int damage, string color, int price){ cout << name << " 의 위력은 " << damage << " 이고, " << color << " 이다. 가격은" << price << "이다" <<endl; } }; void main(){ OverLoad overload; overload.SetInfo("머신건", "황금색", 500, 150, 200); //매개변수에 따른 같은 이름의 메소드가 호출된다. overload.Shot("머신건", 500); overload.Shot("머신건", 500,"황금색"); overload.Shot("머신건", 500, "은색",700); }

===[ 연습 다른 클래스에서 객체 생성 후 접근 ]======================================

#include <iostream>
#include <string>
using namespace std;


class MouseInfo{

private:
 string _name;
 int _dpi;
 int _price;

public:
 void SetMouseInfo(string name, int dpi, int price){
  _name = name;
  _dpi = dpi;
  _price = price;
 }

 void ShowInfo(){
  cout << " 마우스 제품명 : " << _price << endl;
  cout << " 감도 : " << _dpi << endl;
  cout << " 가격 : " << _price << endl;
 }
};


class Show{
 // 객체성성
 MouseInfo info;
public:
 // 생성한 객체를 매개변수로 받아와 반복문 실행
 void ShowInfo(int num, MouseInfo info[]){
  for (int i = 0; i < num; i++)
  {
   info[i].ShowInfo();
   cout << endl;
  }
 }
};



void main(){
 MouseInfo mouse[3];
 mouse[0].SetMouseInfo("핑거", 250, 3500);
 mouse[1].SetMouseInfo("휠레", 150, 1500);
 mouse[2].SetMouseInfo("휴나", 550, 7500);
 
 //객체 반복 실행.
 Show show;
 show.ShowInfo(3, mouse);
}

댓글

이 블로그의 인기 게시물

날짜 시간 시간차 시간 계산 하기

코루틴에서 CallBack 함수 적용하기

C++ 언어 퍼센트 구하는 방법 / 기본 언어 퍼센트 구하는 방법