본문 바로가기
OLD/[C++]개념정리

[개탱][C++][상속][inheritance][사원관리 프로그램][행렬][자료구조][수정필요]

by 개탱 2018. 1. 2.
728x90

//상속관계를 이용한//

//행렬을 이용한 사원별 차량판매 프로그램//

아래와 같이 부모클래스로 쓸 메트릭스를 만들어준뒤 (내부소스는 생략하겠습니다. 직접 짜보길 !)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Matrix  
{
protected:
    double **m_ptr; //행과 열을 동적으로 사용
    int m_row;
    int m_col;
    int m_precision;
    void copy(Matrix &m);
    void freeMemory();
    int getMaxDataWidth();
public:
    Matrix();
    Matrix(int row,int col);
    Matrix(Matrix &m);
    virtual ~Matrix();
    int row();
    int column();
    void setPrecision(int x);
    double get(int i,int j);
    void set(int i,int j,double v);
    //Operator overloading
    //http://blog.naver.com/nuckly/221134548594
    Matrix& operator=(Matrix &m);
    Matrix operator+(Matrix &m);
    Matrix operator-(Matrix &m);
    Matrix operator*(Matrix &m);
    Matrix operator*(double x);
    friend ostream& operator<<(ostream& outs,Matrix &m);
    friend istream& operator>>(istream& ins,Matrix &m);
};
cs


아래와 같이 Matrix 클래스를 상속받는 ProductMatrix 클래스를 만들어줍니다.

1
2
3
4
5
6
7
8
9
10
#pragma once //Header File중복정의 방지용!
#include "matrix.h"
class ProductMatrix :
    public Matrix
{
public:
    ProductMatrix(void);
    ProductMatrix(int row, int col);
    ~ProductMatrix(void);
};
cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "ProductMatrix.h"
 
ProductMatrix::ProductMatrix(void)
    : Matrix()// Super Class의 생성자를 실행시켜줌
{
}
ProductMatrix::ProductMatrix(int row, int col)
    : Matrix(row, col) //이미 부모클래스에서 있는 함수를 또 적을필요 없음!
{
}
 
ProductMatrix::~ProductMatrix(void)
{
}
cs



그 뒤 아래와 같은 Main 을 실행시켜도 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <stdlib.h>
#include "Matrix.h"
#include "ProductMatrix.h"
using namespace std;
//상속관계를 이용한//
//행렬을 이용한 사원별 차량판매 프로그램//
 
void main()
{
    
    int m;
    int n = 3// 차량 종류 수
    cout << "사원 수 ? : ";
    cin >> m;
    cout << "사원 판매 수량 ? : \n";
    
 
    ProductMatrix A(m, n);
    cin >> A;
    cout << endl << " == 판매 결과 == " << endl << A;
 
 
 
    Matrix B(n, 1);
    B.set(00100);
    B.set(1050);
    B.set(2010);
 
    Matrix C;
 
    C = A * B;
 
    cout << "사원별 매출액" << endl;
    for(int i = 0; i < C.row(); i++) {
        cout << i + 1 << "번 사원 : ";
        cout << C.get(i,0<< "(원)" << endl;
    }
}
 
cs

 

위와 같이 따로 이상없이 잘 돌아가게 됩니다!


하지만 판매수량에서 data[0][0] 이런식이 아닌

벤츠 몇대.. 람보 몇대.. 이런식으로 적고싶으니 부모클래스의 function 을 재정의 해주도록 합시다!

이를 function overriding (함수 오버라이딩)이라고 합니다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "ProductMatrix.h"
 
ProductMatrix::ProductMatrix(void)
    : Matrix()// Super Class의 생성자를 실행시켜줌
{
}
ProductMatrix::ProductMatrix(int row, int col)
    : Matrix(row, col) //이미 부모클래스에서 있는 함수를 또 적을필요 없음!
{
}
ostream& operator<<(ostream& outs, ProductMatrix &m)
{
    int row = m.m_row;
    int col = m.m_col;
    double **= m.m_ptr;
    cout << "        벤쯔    람붤    압떼" << endl;
    for(int i = 0; i < row; i++) {
        cout << i+1 << "번 사원";
        cout << "    " << p[i][0<< "대";
        cout << "    " << p[i][1<< "대";
        cout << "    " << p[i][2<< "대" << endl;;
    }
    return outs;
}
istream& operator>>(istream& ins, ProductMatrix &m)
{
    int row = m.m_row;
    int col = m.m_col;
    double **= m.m_ptr;
    for(int i = 0; i < row; i++) {
        cout << i + 1 << "번째 사원이 ... " << endl;
        cout << " 벤쯔 판매  ?: "cin >> p[i][0];
        cout << " 람붤 판매  ?: "cin >> p[i][1];
        cout << " 압떼 판매  ?: "cin >> p[i][2];
    }
    return ins;
}
ProductMatrix::~ProductMatrix(void)
{
}
cs



댓글