C++

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

Hs’s Coding vlog 2023. 1. 14. 15:36

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

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

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

 

//Inkjet.cpp
#include "Inkjet.h"


    int Inkjet::getavailabeInk(){return this->availabeInk;}

    void Inkjet:: print(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;
    }
    
//Inkjet.h
#pragma once
#include "Printer.h"

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 print(int pages);
  
     
    void show();
  
    
};

//Laser.cpp
 #include "Laser.h"   
   
   
   
   
int  laser ::getavailabeToner(){return this->availableToner;}
    
void laser ::print(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;
}
 
//Laser.h
#pragma once
#include "Printer.h"
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 print(int pages);
  
    void show();
  
 

    
};

//main.cpp
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include "Inkjet.h"
#include "Laser.h"
#include "Printer.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->print(pages);
            b->show();

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

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


//Printer.cpp
#include "Printer.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;}


//Printer.h
#pragma once
#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;
    }
    virtual void print(int pages)=0;
    int getPrintedcount();
    int getavailablecount();
    
};