[출력 예시]
학생 이름: John Doe
학생 나이: 20
Student
// Student.h
#pragma once
#include <iostream>
class Student
{
private:
std::string name;
int age;
public:
// 생성자
Student(std::string studentName, int studentAge) : name(studentName), age(studentAge)
{
}
std::string getName();
int getAge();
std::string getinfo();
};
// Student.cpp
#include "Student.h"
#include <string>
using namespace std;
int Student::getAge()
{
return age;
}
string Student::getName()
{
return name;
}
string Student::getinfo()
{
return "학생 이름: " + name + "\n학생 나이: " + to_string(age);
}
StudentPrinter
// StudentPrinter.h
#pragma once
#include <iostream>
#include "Student.h"
class StudentPrinter
{
public:
void print(Student& student);
};
// StudentPrinter.cpp
#include "StudentPrinter.h"
using namespace std;
void StudentPrinter::print(Student& student)
{
cout << student.getinfo() << endl;
}
main
// main.cpp
#include "Student.h"
#include "StudentPrinter.h"
using namespace std;
int main()
{
Student s("JIN", 18); // name에 JIN, age에 18
StudentPrinter p;
p.print(s);
}
'숙제' 카테고리의 다른 글
C++ 3번 과제(인벤토리 시스템 구현) (8) | 2025.08.25 |
---|---|
C++ 숙제 (영화 데이터를 관리하는 프로그램) (0) | 2025.08.21 |
C++ 숙제(반복자를 활용하여 각 컨테이너를 순회하기) (0) | 2025.08.20 |
C++ 숙제(오버로딩 된 함수 템플릿으로 변경하기) (1) | 2025.08.19 |
C++ 숙제(메모리 누수 발생 코드 분석하고 보완하기, 스마트 포인터를 활용한 로그분석기 구현) (0) | 2025.08.19 |