05-12 11:13
Notice
Recent Posts
Recent Comments
관리 메뉴

Scientific Computing & Data Science

[C/C++] Example / Class Constructor 본문

Programming/C&C++

[C/C++] Example / Class Constructor

cinema4dr12 2014. 6. 12. 10:35

"student.h"


//
//  student.h
//  Test-001
//
//  Created by gchoi on 2014. 5. 6..
//  Copyright (c) 2014년 gchoi. All rights reserved.
//

#ifndef Test_001_student_h
#define Test_001_student_h

#include <iostream>
#include <string>

using namespace std;

class Student {
private:
    int no;
    string name;
    int eng;
    int math;
public:
    Student();
    Student(int no, string name, int eng, int math);
    void setNo(int no);
    void setName(string name);
    void setEng(int eng);
    void setMath(int math);
    int getNo();
    string getName();
    int getEng();
    int getMath();
    void showData();
};

#endif


"student.cpp"


//
//  student.cpp
//  Test-001
//
//  Created by gchoi on 2014. 5. 6..
//  Copyright (c) 2014년 gchoi. All rights reserved.
//
#include "student.h"
#include <string>

using namespace std;

Student::Student() {
    this->no = 0;
    this->name = "";
    this->eng = 0;
    this->math = 0;
}

Student::Student(int no, string name, int eng, int math) {
    this->no = no;
    this->name = name;
    this->eng = eng;
    this->math = math;
}

void Student::setNo(int no) {
    this->no = no;
}

void Student::setName(string name) {
    this->name = name;
}

void Student::setEng(int eng) {
    this->eng = eng;
}

void Student::setMath(int math) {
    this->math = math;
}

int Student::getNo() {
    return this->no;
}

string Student::getName() {
    return this->name;
}

int Student::getEng() {
    return this->eng;
}

int Student::getMath() {
    return this->math;
}


void Student::showData() {
    cout << no << endl;
    cout << name << endl;
    cout << eng << endl;
    cout << math << endl;
}


"main.cpp"


//
//  main.cpp
//

#include <iostream>
#include <string>
#include "student.h"

using namespace std;


int main( int argc , const char * argv [])
{
    Student *gchoi = new Student(15, "Geol Choi", 99, 100);
   
    //gchoi->setNo(15);
    //gchoi->setName("Geol Choi");
    //gchoi->setEng(99);
    //gchoi->setMath(100);
   
    cout << gchoi->getNo() << endl;
    cout << gchoi->getName() << endl;
    cout << gchoi->getEng() << endl;
    cout << gchoi->getMath() << endl;
   
    delete gchoi;
   
   
    return 0 ;
}

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

[C/C++] Example / Ordering - Descending Algorithms  (0) 2014.06.12
[C/C++] Example / Derived Class  (0) 2014.06.12
[C/C++] Example / Class  (0) 2014.06.12
[C/C++] Example / Pointer of Structure  (0) 2014.06.12
[C/C++] Example / Structure  (0) 2014.06.12
Comments