상세 컨텐츠

본문 제목

[C언어|C++] C++만들면서 배우기01 / 만들면서 배우는 C++ 학생 출력하기

C|C++

by donggyu1998 2021. 7. 18. 22:39

본문

반응형

💡 글 작성자는 vscode를 활용하여 Python을 작성하고있습니다.

 

해당글에는 QList (QT)를 활용하였습니다. 

만약 C++로만 하는 경우에는 vector로 변경하면 됩니다. 

💡 [실행화면]

💡 Main.cpp

#include <iostream> 
#include "Student.h"
#include "Subject.h"
#include "School.h"
using namespace std; 

int main() 
{  
    School *sch = new School("대학교");
    Student *donggyu = new Student("동규");
    Student *mansu = new Student("동글");

    donggyu->subList->append(new Subject("국어", 100));
    donggyu->subList->append(new Subject("수학", 100));
    donggyu->subList->append(new Subject("영어", 100));

    mansu->subList->append(new Subject("국어", 100));
    mansu->subList->append(new Subject("수학", 100));
    mansu->subList->append(new Subject("영어", 100));
    
    sch->schlist->append(donggyu);
    sch->schlist->append(mansu);

    cout << "학교: " << sch->getName() << endl;
    cout << "학생1: " << sch->schlist->at(0)->getName() << endl;
    cout << "과목: " << donggyu->subList->at(0)->getName() << " / " <<  donggyu->subList->at(0)->getNumber() << endl;
    cout << "과목: " << donggyu->subList->at(1)->getName() << " / " <<  donggyu->subList->at(1)->getNumber() << endl;
    cout << "과목: " << donggyu->subList->at(2)->getName() << " / " <<  donggyu->subList->at(2)->getNumber() << endl;
    cout << "=====================================\n";
    cout << "학생2: " << sch->schlist->at(1)->getName() << endl;
    cout << "과목: " << mansu->subList->at(0)->getName() << " / " <<  mansu->subList->at(0)->getNumber() << endl;
    cout << "과목: " << mansu->subList->at(1)->getName() << " / " <<  mansu->subList->at(1)->getNumber() << endl;
    cout << "과목: " << mansu->subList->at(2)->getName() << " / " <<  mansu->subList->at(2)->getNumber() << endl;

}

💡 School.h

#ifndef SCHOOL_H
#define SCHOOL_H

#include <iostream> 
#include <QList>
#include "Student.h"
#include "Subject.h"

#include <QString>
#include <QDebug>

using namespace std; 

class School {

    public:
        School(string name);
        ~School();
        QList<Student*> *schlist;
        string getName();

        void printInfo() 
        {
            //cout << "학생1: " << schlist->at(0)->getName() << endl;
            //cout << "학생2: " << schlist->at(1)->getName() << endl;
        }

    private:
        string m_name;
};

#endif

💡 School.cpp

#include "School.h"

School::School(string name)
{
    m_name = name;
    schlist = new QList<Student*>();
}

School::~School()
{
    
}

string School::getName()
{
    return m_name;
}

💡 Student.h

#ifndef STUDENT_H
#define STUDENT_H

#include <iostream> 
#include <QList>
#include "Subject.h"

using namespace std; 

class Student {

    public:
        Student(string name);
        ~Student();
        QList<Subject*> *subList;
        string getName();
    
    private:
        string m_name;
};

#endif

💡 Student.cpp

#include "Student.h"

Student::Student(string name)
{
    m_name = name;
    subList = new QList<Subject*>();
}

Student::~Student() 
{
    
}

string Student::getName()
{
    return m_name;
}

💡 Subject.h

#ifndef SUBJECT_H
#define SUBJECT_H
#include <iostream> 

using namespace std; 

class Subject {

    public:
        Subject(string name, int score);
        ~ Subject();

        string getName();
        int getNumber();
        
    private: 
        string m_name;
        int  m_score;
};

#endif

💡 Subject.cpp

#include "Subject.h"

Subject::Subject(string name, int score)
{
    m_name = name; 
    m_score = score;
}

Subject::~Subject()
{

}

string Subject::getName()
{
    return m_name;
}
int Subject::getNumber()
{
    return m_score;
}

 

반응형

관련글 더보기