C++

[C++] 명품 C++ 프로그래밍 실습문제 8장 8번

Hs’s Coding vlog 2023. 1. 13. 16:46

[C++]명품 C++ 프로그래밍 실습문제 8장 8번

[C++]명품 C++ programming 실습문제 8장 8번

명품 C++ 프로그래밍실습문제/연습문제 /C++

헤더파일과 cpp 파일을 따로 만들어 두었습니다. 잘활용하시길 바랍니다~

//header.cpp

#include <iostream>
#include <string>
#include "header.h"
bool Printer::isValid(int value)
{
        if(value>0) return true; // 0보다 크면 트루를 리턴
        else
            return false; // 0보다 작작으면 false 리턴
}
int Printer::getPrintedcount(){return this->printedcount;};
int Printer::getavailablecount(){return this->availablecount;}

 

//header.h

#ifndef HEADER_H
#define HEADER_H
#include <iostream>
#include <string>
using namespace std;
class Printer{ //원시 클래스
public:
    string model;
    string manufacterer;
    int printedcount;
    int availablecount;
    
    bool isValid(int value);
 
    Printer( string model,string manufacterer,int printedcount,int availablecount){ //생성자
        this->model = model;
        this-> manufacterer = manufacterer;
        this-> printedcount = printedcount;
        this-> availablecount = availablecount;
    }
    
    int getPrintedcount();
    int getavailablecount();
    
};









#endif

//header1.cpp

#include <iostream>
#include <string>
#include "header.h"
#include "header1.h"



int Inkjet::getavailabeInk(){return this->availabeInk;}
void Inkjet::printInkjet(int pages){
        if(isValid(this->availablecount - pages)==true){ // 계산해주기
            this-> printedcount = this->printedcount + pages;
            this-> availablecount = this->availablecount - pages;
            this-> availabeInk = this->availabeInk -pages;
            cout<< "프린트했습니다" << endl;
            cout << this->model << this-> manufacterer << "남은 종이" << this->availablecount <<" 장"<<
            "남은 잉크 " << availabeInk << endl;
        }
        else{
            cout<< "프린트하지 못합니다" << endl;
            cout << this->model << this-> manufacterer << "남은 종이" << this->availablecount <<" 장"<<
            "남은 잉크 " << availabeInk << endl;
        }
}
void Inkjet::show(){
        cout << this->model << this-> manufacterer << "남은 종이" << this->availablecount <<" 장"<<
        "남은 잉크 " << availabeInk << endl;
}

//header1.h

#ifndef HEADER1_H
#define HEADER1_H
#include <iostream>
#include <string>
using namespace std;

class Inkjet : public Printer{
    int availabeInk;
public:
    Inkjet(string model,string manufacterer,int printedcount,int availablecount,int availableInk): Printer(model,manufacterer,printedcount,availablecount){
        this->availabeInk = availableInk;
    }
    int getavailabeInk();

    void printInkjet(int pages);

    void show();
  

    
};
#endif

 

//header2.cpp

#include <iostream>
#include <string>
#include "header.h"
#include "header2.h"



int laser ::getavailabeToner(){return this->availableToner;}

void laser ::printLaser(int pages)
{
        if(isValid(this->availablecount - pages)==true){ // 계산해주기
            this-> printedcount = this->printedcount + pages;
            this-> availablecount = this->availablecount - pages;
            this-> availableToner  = this->availableToner -pages;
            cout<< "프린트했습니다" << endl;
            cout << this->model << this-> manufacterer << "남은 종이" << this->availablecount <<" 장"<<
            "남은 잉크 " << availableToner << endl;
        }
        else{
            cout << "프린트할수 없습니다" << endl;
            cout << this->model << this-> manufacterer << "남은 종이" << this->availablecount <<" 장"<<
            "남은 잉크 " << availableToner << endl;
        }
}
void laser ::show()
{
        cout << this->model << this-> manufacterer << "남은 종이" << this->availablecount <<" 장"<<
        "남은 토너 " << getavailabeToner() << endl;
}

//header2.h

#ifndef HEADER2_H
#define HEADER2_H
#include <iostream>
#include <string>
using namespace std;

class laser : public Printer{
    int availableToner;
public:
    laser(string model,string manufacterer,int printedcount,int availablecount,int availableToner): Printer(model,manufacterer,printedcount,availablecount){
        
        this->availableToner = availableToner;
    }
    int getavailabeToner();
    void printLaser(int pages);

    void show();
 
 

    
};

#endif

//main.cpp

#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include "header.h"
#include "header1.h"
#include "header2.h"



using namespace std;



int main() {
    int printN;
    int pages;
    string YN;
    
    cout << "현재 작동중인 2대의 프린터는 아래와 같다." << endl;
    cout << "잉크젯 : Offficejet V40,HP, 남은 종이 5장 , 남은 잉크 10" << endl; //print a
    Inkjet *a = new Inkjet("OfficejetV40","HP",0,5,10);
    
    cout << "레이저 : SCX-6x45 ,삼성전자, 남은 종이 3장 , 남은 토너 20" << endl;  //print b
    laser *b = new laser("SCX-6x45","삼성전자",0,3,20);
    
    while(1){
        cout << "프린타(1:잉크젯 , 2:레이저) 와 매수입력>>";
        cin >> printN >> pages;
        if(printN == 1){
            a->printInkjet(pages);
            b->show();

         
        }
        else if(printN == 2){
           
            b->printLaser(pages);
            
             a->show();

        
        }
        
        cout << "keep printing????(y/n)" ;
        cin >> YN;
        if( YN == "y")
            continue;
        else
            break;
    }
    cout << endl;
    return 0;

}