Menu
CoddyTech

클래스와 인스턴스

Coddy의 JavaScript 개발자를 위한 C++ 코스 레슨. 48개 중 32번째.

JavaScript 클래스 인스턴스는 항상 참조를 통해 접근하며, 속성은 열려 있어 어떤 코드든 생성 후에 새 속성을 추가할 수 있습니다. 인스턴스를 다른 변수에 할당하면 동일한 객체를 공유합니다. C++ 객체는 정적으로 선언된 레이아웃과 값 동작을 가집니다. C++ 객체는 값으로 직접 저장할 수 있습니다. 멤버 함수는 해당 객체에서 동작하며, 클래스와 구조체는 기본 접근 지정자가 다릅니다.

JavaScript

class Parcel {}
const parcel = new Parcel();
parcel.label = "sample";
const alias = parcel;
alias.label = "changed";
console.log(parcel.label);

C++

#include <iostream>
#include <string>
#include <vector>
#include <array>
#include <map>
#include <set>
#include <algorithm>
#include <memory>
#include <sstream>
#include <stdexcept>
#include <utility>
struct Badge {std::string name;std::string label()const{return "@"+name;}};
int main(){
Badge b{"Ada"};Badge c=b;c.name="Bo";std::cout<<b.label()<<"\n";
}

각 프로그램을 독립적으로 추적하세요. 이 예제들은 과제와 다른 연산이나 데이터를 사용해 개념을 보여 주므로, 소스와 대상의 출력이 같다고 가정하지 마세요.

challenge icon

챌린지

쉬움

입력: 0 이상의 정수 너비와 높이. Rectangle은 자체 치수를 저장하며, area는 두 값의 곱을 반환합니다. 드라이버는 검사에서 서로 독립적인 객체 두 개를 생성합니다.

편집 가능한 모듈: Rectangle.cpp, Rectangle.hpp. 필요한 각 형식에는 이름이 지정된 헤더와 구현 파일이 각각 있습니다. 모든 공개 시그니처를 그대로 유지하세요. 잠긴 main.cpp는 stdin을 읽고 API를 직접 호출합니다. 또한 명시된 경우 공개 API의 동작을 독립적으로 확인합니다. API 내부에서 출력하지 말고 값을 반환하세요. 정수 입력값과 결과는 signed int 범위에 들어갑니다. 별도 명시가 없으면 텍스트는 ASCII입니다.

직접 해보기

#include "Rectangle.hpp"
#include <iostream>
int main(){
{Rectangle a(2,3),b(4,5);if(a.area()!=6||b.area()!=20||a.area()!=6)return 21;}
std::vector<std::string> rows; std::string line; while(std::getline(std::cin,line)) rows.push_back(line);
std::istringstream in(rows.empty()?"":rows[0]);int w,h;in>>w>>h;Rectangle r(w,h);std::cout<<r.area()<<"\n";
}
quiz icon실력 점검

이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.

JavaScript 개발자를 위한 C++의 모든 레슨

직접 연습해 보세요: 온라인 C++ 컴파일러