본문 바로가기

숙제

C++ 숙제(SOLID원칙을 적용한 Student 클래스 구현)

[출력 예시]

 

학생 이름: 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);
}