[c++_8. 정리 (생성자, 소멸자, const, 동적메모리 할당]

·2022년 8월 8일
0

C++_Study

목록 보기
15/25

#8-1. c++문법 정리

(생성자, 소멸자 const, 동적메모리 할당)

#include <iostream>
#include <string>
#include <string.h>

//클래스 이름은 대문자! 
class Cat {
private:            //생략가능 (멤버변수는 감춰놓음)
    int age;
    char name[20];
    //const char* name;             //A 
public:
//생성자
Cat(int age, const char* n) {
      //this -> 는 현재 객체 주소를 이야기함
    this -> age = age;
    strcpy(name, n); //name=n;   	//A
    std::cout << name << "고양이 객체가 만들어졌어요.\n" << std::endl;
}  
// 소멸자: 객체를 만들었으면 소멸시키는 것 까지 해야함
~Cat(){std::cout << name << "객체바이\n" << std::endl; } ;
int getAge();
const char* getName();
void setAge(int age);
void setName(const char* pName);
void meow();
};

// getter(return하는 것) setter 
int Cat::getAge() const{		// age는 integer일 것/int값 변경 x
    return age;
}
void Cat::setAge(int age){		// return 값이 없어서 void 
    // 포인터이기 떄문에
    this -> age = age;
}
void Cat::setName(const char* pName){
    strcpy(name, pName);
    //strcpy (대상주소, 원본주소);
    //strcpy_s (대상주소, 대상의 길이, 원본주소);
    //name=pNmae;                           //A
}
const char* Cat::getName(){
    return name;
}
void Cat::meow(){
    std::cout << name << "고양이가 울어요\n" << std::endl;
}

int main(){
    Cat nabi(1,"나비"),yaong(1,"야옹"), * pNabi;
    std::cout << nabi.getName() << "출생 나이는" << nabi.getAge() << "살이다.\n" << std::endl;
    std::cout << yaong.getName() << "출생 나이는" << yaong.getAge() << "살이다.\n" << std::endl;
    pNabi = &nabi;
    std::cout << pNabi ->getName() << "출생 나이는" << pNabi->getAge() << "살이다.\n" << std::endl;
    nabi.setName("Nabi");
    //set으로 nabi 나이 셋팅 
    nabi.setAge(3);
    //get으로 nabi 나이 가져오기
    std::cout << nabi.getName() << "나이는" << nabi.getAge() << "살이다.\n" << std::endl; 
    yaong.meow(); 
    nabi.meow();
    return 0; 
}

// 결과
// 나비고양이 객체가 만들어졌어요.
// 야옹고양이 객체가 만들어졌어요.
// 나비출생 나이는1살이다.
// 야옹출생 나이는1살이다.
// 나비출생 나이는1살이다.
// Nabi나이는3살이다.
// 야옹고양이가 울어요
// Nabi고양이가 울어요
// 야옹객체바이
// Nabi객체바

// 동적 메모리할당 예제 (string 앞에는 std::string)

class Cat {
private:                        //생략가능
    int age;
    std::string name;
public:
//생성자
Cat(int age, std::string n) {
    // this -> 는 현재 객체 주소를 이야기함
    this -> age = age;
    name=n;
    std::cout << name << "고양이 객체가 만들어졌어요.\n" << std::endl;
}  
// 디폴트생성자, 동적 할당 
Cat() { age=1; name= "냥이"; }     
// 소멸자
~Cat(){std::cout << name << "객체바이\n" << std::endl; } ;
// int getAge() const; -> const를 하면 값이 고정되서 변할 수 없음
int getAge();
std::string getName();
void setAge(int age);
void setName(std::string pName);
void meow();
};
int Cat::getAge() {        
    return age;
}
void Cat::setAge(int age){
    this -> age = age;
}
void Cat::setName(std::string pName){
    name = pName;
}
std::string Cat:: getName(){
    return name;
}
void Cat::meow(){
    std::cout << name << "고양이가 울어요\n" << std::endl;
}

int main(){
    Cat nabi(1,"나비"),yaong(1,"야옹"), * pNabi; 
    int num, pAge;
    // setName()과 getName() 셋팅하고 get으로 받아오기
    std::cout << nabi.getName() << "출생 나이는" << nabi.getAge() << "살이다.\n" << std::endl;
    std::cout << yaong.getName() << "출생 나이는" << yaong.getAge() << "살이다.\n" << std::endl;
    // 나비의 주소로-! 
    pNabi = &nabi;
    std::cout << pNabi ->getName() << "출생 나이는" << pNabi->getAge() << "살이다.\n" << std::endl;
    nabi.setName("Nabi");
    // setAge() 와 getAge() 셋팅하고 get으로 받아오기 
    nabi.setAge(3);
    std::cout << nabi.getName() << "나이는" << nabi.getAge() << "살이다.\n" << std::endl; 
    yaong.meow(); 
    nabi.meow();
    
    std::cout << "\n동적으로 생성할 고양이 수==" << std::endl; 
    std::cin >> num; 
    // ★★★★★★★★★★★★★★★★★★★★★★★★★ 객체 배열을 동적으로 메모리 할당, default 생성자 만들어야 함 //
    Cat* pCat = new Cat[num];  
    
    // null이 아닌지 확인하는 부분
    if(!pCat) { std::cout << "동적으로 메모리 할당이 되지 않았습니다."; exit(1); }
    for (int i=0; i < num; i++) {
        std::cout << "pCat[" << i << "]" << "객체의 나이를 입력하십시오: " << std::endl;
        std::cin >> pAge; pCat[i].setAge(pAge);   
    }
    for (int i=0; i < num; i++)
        std::cout << "pCat[" << i << "]" << "객체의 나이는" << pCat[i].getAge() << "입니댜.\n" << std::endl;
        delete[]pCat;   //할당받은 메모리 해제, 배열로 할당받은 경우 delete 다음에 반드시 []써야한다. 
    return 0; 
}
profile
Hakuna Matata

0개의 댓글

관련 채용 정보