C++/디딤돌 C++

[C++ 소스] 의존(DEPENDENCY) 관계, 공장과 상품

언제나휴일 2016. 12. 18. 22:32
반응형

[C++ 소스] 의존(DEPENDENCY) 관계, 공장과 상품


Factory.cpp

Factory.h

Product.cpp

Product.h

Program.cpp

의존 관계 클래스 다이어그램





//Product.h

#pragma once

#include <iostream>

#include <string>

using namespace std;

class Product

{

    string name;

    int price;

    const int pnum;

public:

    Product(string name,int price,int pnum);

    void View()const;

};


//Product.cpp

#include "Product.h"

 

Product::Product(string name,int price,int pnum):pnum(pnum)

{

    this->name = name;

    this->price = price;

}

void Product::View()const

{

    cout<<"일련번호:"<<pnum<<" 제품명:"<<name<<" 가격:"<<price<<endl;

}


//Factory.h

#pragma once

#include "Product.h"

#define MAX_PRODUCTS      10

class Factory

{

    Product *products[MAX_PRODUCTS];

    int sellcnt;

public:

    Factory(void);

    ~Factory(void);

    Product *Order(string name,int price);

};


//Factory.cpp

#include "Factory.h"

 

Factory::Factory(void)

{   

    sellcnt = 0;

}

 

Factory::~Factory(void)

{

    for(int i = 0; i < sellcnt ; i++)

    {

        delete products[i];

    }

}

Product *Factory::Order(string name,int price)

{

    Product *product  = new Product(name,price,sellcnt+1);

    products[sellcnt] = product;

    sellcnt++;

    return product;

}


//Program.cpp

#include "Factory.h"

 

int main()

{

    Factory *factory = new Factory();

    Product *product = factory->Order("디딤돌C언어",5000);

    product->View();

    Product *product2 = factory->Order("디딤돌CPP",5000);

    product2->View();

   

    delete factory;

    return 0;

}


실행 결과

일련번호:1 제품명:디딤돌C언어 가격:5000

일련번호:2 제품명:디딤돌CPP 가격:5000



본문

[디딤돌 C++] 66. 의존(DEPENDENCY) 관계




반응형