클래스

이동규·2024년 11월 24일

C++

목록 보기
1/16

클래스의특징

c++의 클래스의 프로퍼티는 default로 priviate이다. public으로 키워드를 변경해야한다.
동적 할당을 하면 메모리 해제를 해야된다.빌드시 컴파일러가 에러를 발생한다.
string은 객체이다. printf를 사용하려면 c.str()을 해줘야한다. &으로 가능하지만 c.str()을 사용하자

#include <string>
#include <vector>
#include <stdio.h>
#include <iostream>
using namespace std;

class Person
{
public:
    string name;
    char * title;

    Person(string name)
    {
        this->name = name;
    }
};
class Animal
{
public:
    static void hello()
    {
        printf("hello world");
    }
};

int main(int argc, char const *argv[])
{
    Person *lee = new Person("이동규");
    Person kim("김씨");

    Animal::hello();
    // Print the name of the person using the dynamically allocated object
    cout
        << lee->name << kim.name << endl;// 동적할당의 프로퍼티를 접근 할 때는 -> 정적할당의 프로퍼티를 접근 할 때는 .을 사용한다. 

    // Deallocate the memory to avoid memory leaks
    //
    delete lee;

    return 0;
}

0개의 댓글