생성자 호출 관련 질문
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Teacher {
std::string m_name;
public:
Teacher(const std::string& name_input = "No name")
: m_name(name_input) {
std::cout << "Teacher" << std::endl;
}
~Teacher() {}
void setName(const std::string& name_in) {
m_name = name_in;
}
std::string getName() {
return m_name;
}
friend std::ostream& operator<<(std::ostream& os, const Teacher& teacher) {
os << teacher.m_name << std::endl;
return os;
}
};
class Student {
std::string m_name;
int m_intel;
public:
Student(const std::string& name_in = "No name", const int& intel_in = 0)
: m_name(name_in)
, m_intel(intel_in) {
std::cout << "Student" << std::endl;
}
~Student() {}
void setName(const std::string& name_in) {
m_name = name_in;
}
void setIntel(const int& intel_in) {
m_intel = intel_in;
}
int getIntel() {
return m_intel;
}
friend std::ostream& operator<<(std::ostream& os, const Student& student) {
os << student.m_name << " " << student.m_intel;
return os;
}
};
class Lecture {
std::string m_name;
Teacher m_teacher;
std::vector<Student> m_students;
public:
Lecture(const std::string& name_in = "No Name")
: m_name(name_in) {
std::cout << "Lecture" << std::endl;
}
~Lecture() {
//do Not delete teacher, students
}
void assignTeacher(const Teacher& teacher) {
m_teacher = teacher;
}
void registerStudent(const Student& const student) {
m_students.push_back(student);
}
void study() {
std::cout << m_name << " Study " << std::endl << std::endl;
for (auto& element : m_students)
element.setIntel(element.getIntel() + 1);
}
friend std::ostream& operator<<(std::ostream& os, const Lecture& lecture) {
os << "Lecture name : " << lecture.m_name << std::endl;
os << lecture.m_teacher;
for (auto& element : lecture.m_students)
os << element << std::endl;
return os;
}
};
int main() {
using namespace std;
Lecture lec1("Introduction to Computer Programming");
lec1.assignTeacher(Teacher("Prof. Hong"));
lec1.registerStudent(Student("A", 1));
lec1.registerStudent(Student("B", 2));
lec1.registerStudent(Student("C", 3));
Lecture lec2("Computational Thinking");
lec2.assignTeacher(Teacher("Prof. Good"));
lec2.registerStudent(Student("A", 0));
//TODO. implement Aggregation Relationship
//test
{
cout << lec1 << endl;
cout << lec2 << endl;
lec2.study();
cout << lec1 << endl;
cout << lec2 << endl;
}
}
Lecture를 생성하면 Teacher도 같이 생성됩니다
그런데 Lecture 생성자 안에 Teacher와 관련된건 없을텐데 생성되니깐 궁금해서 질문합니다
답변 1
변수가 메모리에 저장되는 것을 알려주는 강의가 어떤강의였죠
1
461
1
메모리 주소 10진수로 출력
1
650
1
클래스 템플릿 특수화에서 boolalpha로 표현된 리턴값에 대해 질문이 있습니다.
1
496
1
여러가지 리턴 타입에 관한 강의가 어떤 걸까요?
1
529
1
메모리 주소에 관한 질분
0
676
1
인터페이스 클래스에서 reportError의 매개변수에 대해 궁금한 것이 있습니다.
0
545
1
형변환 오버로딩에서 const 관련 질문이 있습니다.
0
439
1
Digit 뒤에 reference를 사용하는 이유
0
504
1
4.2 전역 변수, 정적 변수, 내부 연결, 외부 연결
0
319
1
dat파일이...
0
534
1
TODO:대입 연산자 오버로딩에 대한 소스코드입니다.
0
640
1
복사 생성자 관련 질문이 있습니다.
0
450
1
수업 중 궁금한점이 있습니다.
1
386
1
라이브러리자체가 이해가 되지 않습니다.
0
557
1
마지막 예제 질문
0
299
1
증감연산자 위치에 따른 수행 순서 질문입니다.
0
371
1
단항 연산자 오버로딩에서 return 부분에 질문이 있습니다.
1
408
1
friend함수 관련 질문이 있습니다.
0
308
1
operator+ 정의부분에서 궁금한 것이 있습니다.
0
443
1
3분 17초 질문
0
346
1
함수에 값을 대입한다는 개념이 이해가 되지 않습니다.
0
443
1
int getvalue() const에서 const는 왜 뒤에 붙는건가요?
0
440
2
const Something &st에서 const를 빼면 안되나요?
0
296
1
friend함수는 다른 클래스의 멤버함수로 쓸 수 없나요??
1
489
1





