
C++에서 구조체(struct)와 클래스(class)는 데이터를 그룹화하고 객체 지향 프로그래밍(OOP)을 실현하는 핵심 요소다. 이번 글에서는 C++ 로드맵의 Structures and Classes 파트를 중심으로 구조체와 클래스의 기본 개념부터 OOP, 다형성, 상속, 그리고 Rule of 0/3/5까지 차근차근 정리해본다.
publicstruct Employee {
int id;
std::string name;
float salary;
};
Employee e1;
e1.id = 1;
e1.name = "John Doe";
e1.salary = 40000;
class Student {
int roll_no;
std::string name;
float marks;
public:
void set_data(int r, std::string n, float m) {
roll_no = r;
name = n;
marks = m;
}
void display() {
std::cout << "Roll no: " << roll_no
<< "\nName: " << name
<< "\nMarks: " << marks << '\n';
}
};
Student s1;
s1.set_data(1, "Alice", 95.0);
s1.display();
class Dog {
private:
std::string name;
int age;
public:
void setName(std::string n) { name = n; }
void setAge(int a) { age = a; }
void bark() { std::cout << name << " barks!\n"; }
};
class Animal {
public:
void breathe() { std::cout << "I can breathe\n"; }
};
class Dog : public Animal {
public:
void bark() { std::cout << "Dog barks!\n"; }
};
Dog myDog;
myDog.breathe();
myDog.bark();
class Animal {
public:
virtual void makeSound() {
std::cout << "The Animal makes a sound\n";
}
};
class Dog : public Animal {
public:
void makeSound() override {
std::cout << "Dog barks!\n";
}
};
class Cat : public Animal {
public:
void makeSound() override {
std::cout << "Cat meows!\n";
}
};
Animal* animals[2] = {new Dog, new Cat};
animals[0]->makeSound(); // Dog barks!
animals[1]->makeSound(); // Cat meows!
class Shape {
public:
virtual void draw() {
std::cout << "Drawing a shape\n";
}
};
class Circle : public Shape {
public:
void draw() override {
std::cout << "Drawing a circle\n";
}
};
Shape* shape = new Circle();
shape->draw(); // Drawing a circle
class Animal {
public:
void eat() { std::cout << "I can eat!\n"; }
};
class Mammal {
public:
void breath() { std::cout << "I can breathe!\n"; }
};
class Dog : public Animal, public Mammal {
public:
void bark() { std::cout << "I can bark!\n"; }
};
class Base {
public:
void print() { std::cout << "Base class\n"; }
};
class Derived1 : virtual public Base {};
class Derived2 : virtual public Base {};
class Derived3 : public Derived1, public Derived2 {};
Derived3 d3;
d3.print(); // Base class
struct MyResource {
std::string name;
int value;
};
class MyResource {
public:
MyResource() : data(new int[100]) {}
~MyResource() { delete[] data; }
MyResource(const MyResource& other) : data(new int[100]) {
std::copy(other.data, other.data + 100, data);
}
MyResource& operator=(const MyResource& other) {
if (this == &other) return *this;
std::copy(other.data, other.data + 100, data);
return *this;
}
private:
int* data;
};
class MyResource {
public:
MyResource() : data(new int[100]) {}
~MyResource() { delete[] data; }
MyResource(const MyResource& other) : data(new int[100]) {
std::copy(other.data, other.data + 100, data);
}
MyResource& operator=(const MyResource& other) {
if (this == &other) return *this;
std::copy(other.data, other.data + 100, data);
return *this;
}
MyResource(MyResource&& other) noexcept : data(other.data) {
other.data = nullptr;
}
MyResource& operator=(MyResource&& other) noexcept {
if (this == &other) return *this;
delete[] data;
data = other.data;
other.data = nullptr;
return *this;
}
private:
int* data;
};
C++의 구조체와 클래스는 단순히 데이터를 묶는 것에서 그치지 않고, 객체 지향적인 사고로 설계를 가능하게 해준다. 캡슐화, 상속, 다형성, 그리고 Rule of Zero, Three, Five까지 잘 익혀두면 견고하고 유연한 C++ 코드를 작성할 수 있다.