C++ 상속 Teacher-Student 예제

2021. 3. 19. 19:47·C++/Class

상속 Teacher-Student 예제

  • 상속을 사용하지 않은 원본 예제

  • Teacher, Student 클래스의 공통 멤버인 name_, setName, getName을 묶어서 Person이라는 클래스를 만들고 상속을 적용했다.

  • 생성자 부분은 기본예제 참고

main.cpp

#include "Student.h"
#include "Teacher.h"

int        main()
{
    using namespace std;

    Student std("Jack Jack");

    std.setName("New Jack");
    std.getName();

    cout << std.getName() << endl;

    Teacher teacher1("Dash");

    teacher1.setName("Dr. K");

    cout << teacher1.getName() << endl;

    cout << std << endl;
    cout << teacher1 << endl;
}

/* stdout
New Jack
Dr. K
New Jack 0
Dr. K
*/

Person.h

#pragma once

#include <iostream>

class Person
{
    std::string name_;

public:
    Person(const std::string & name_in)
        : name_{name_in}
    {}

    std::string    getName() const
    {
        return name_;
    }

    void    setName(const std::string& name_in)
    {
        name_ = name_in;
    }
};

Student.h

#pragma once

#include "Person.h"

class Student : public Person
{
    int        intel_;

public:
    Student(const std::string& name_in = "No Name", const int& intel_in = 0)
        : Person(name_in), intel_{ intel_in }
    {}

    void    setIntel(const int& intel_in)
    {
        intel_ = intel_in;
    }

    int        getIntel()
    {
        return intel_;
    }

    friend std::ostream& operator << (std::ostream& out, const Student& student)
    {
        out << student.getName() << ' ' << student.intel_;
        return out;
    }
};

Teacher.h

#pragma once

#include "Person.h"

class Teacher : public Person
{

public:
    Teacher(const std::string& name_in = "No Name")
        : Person(name_in)
    {}

    friend std::ostream& operator << (std::ostream& out, const Teacher& teacher)
    {
        out << teacher.getName();
        return out;
    }
};
저작자표시 (새창열림)

'C++ > Class' 카테고리의 다른 글

C++ 유도된 클래스들의 소멸 순서  (0) 2021.03.19
C++ 유도된 클래스들의 생성 순서  (0) 2021.03.19
C++ 상속 기본 예제  (0) 2021.03.19
C++ 상속 (Inheritance)  (0) 2021.03.19
C++ 의존 관계 (Dependency)  (0) 2021.03.19
'C++/Class' 카테고리의 다른 글
  • C++ 유도된 클래스들의 소멸 순서
  • C++ 유도된 클래스들의 생성 순서
  • C++ 상속 기본 예제
  • C++ 상속 (Inheritance)
Caniro
Caniro
  • Caniro
    Minimalism
    Caniro
  • 전체
    오늘
    어제
    • 분류 전체보기 (317)
      • Algorithm (13)
        • 알기 쉬운 알고리즘 (10)
        • Search (1)
        • Sort (2)
      • Arduino (0)
      • C++ (185)
        • Class (46)
        • Exception (6)
        • Library (51)
        • Overloading (10)
        • SmartPointer (5)
        • Syntax (33)
        • TBC++ (23)
        • Templates (9)
        • VisualStudio (2)
      • Embedded (1)
      • Git (4)
      • Java (5)
      • Linux (16)
        • Error (1)
        • Linux Structure (11)
      • MacOS (7)
      • OS (1)
        • Concurrency (1)
      • Python (21)
        • Class (1)
        • Function (2)
        • Syntax (17)
      • Raspberrypi (9)
      • Review (1)
      • Utility (12)
        • VSCode (5)
        • VirtualBox (3)
      • Web (8)
        • Nginx (1)
        • React (3)
        • Django (1)
      • Windows (20)
        • Registry (3)
        • WSL (1)
        • DeviceDriver (6)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    KakaoTalk
    Workspace
    dism
    SunOS 5.1
    스프링 프레임워크 핵심 기술
    windows
    맥북 카카오톡 알림 안뜸
    백기선
    vscode
    스프링
    citrix workspace
    제외
    java
    Windows 11
    알림
    spring
    logi options
    unix
    EXCLUDE
    윈도우 명령어
    mspaint
    그림판
    로지텍 마우스 제스처
    윈도우
    MacOS
    SFC
    시스템 복구
    Solaris 10
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
Caniro
C++ 상속 Teacher-Student 예제
상단으로

티스토리툴바