C++ 10.캡슐화 실습(복소수)

-캡슐화 실습(복소수)

//Complex.h

#pragma once

#include <iostream>

#include <string>

using namespace std;

 

class Complex

{

        double image;

        double real;

public:

        Complex(double image = 0, double real = 0);

        double GetImage();

        double GetReal();

        void SetImage(double image);

        void SetReal(double real);

        const void View();

};

 

 

//Complex.cpp

#include "Complex.h"

 

Complex::Complex(double real , double image )

{

        SetReal(real);

        SetImage(image);

}

double Complex::GetImage()

{

        return this->image;

}

double Complex::GetReal()

{

        return this->real;

}

void Complex::SetImage(double image)

{

        this->image = image;

}

void Complex::SetReal(double real)

{

        this->real = real;

}

const void Complex::View()

{

        if((real != 0) && (image != 0))

        {

               if(image > 0)

               {

                       cout<<real<<"+"<<image<<"i"<<endl;

               }

               else

               {

                       cout<<real<<image<<"i"<<endl;

               }

        }

        else//실수부나 허수부 중에 최소 하나는 0

        {

               if(image != 0) //허수부가 0 아닐때(실수부는 0)

               {

                       cout<<image<<"i"<<endl;

               }

               else

               {

                       cout<<real<<endl;

               }

        }

}

 

 

//Program.cpp

#include "Complex.h"

 

int main(void)

{

        Complex *c1 = new Complex();

        Complex *c2 = new Complex(1);

        Complex *c3 = new  Complex(2.3,5.4);

 

        c1->View();

        c2->View();

        c3->View();

 

        delete c1;

        delete c2;

        delete c3;

 

        return 0;

}

 

728x90

'C++ > 설명' 카테고리의 다른 글

C++ 12. 캡슐화 실습(학생)  (0) 2016.04.19
C++ 11. 캡슐화 실습(학생 성적)  (0) 2016.04.19
C++ 9.복사 생성자  (0) 2016.04.19
C++ 7.생성자와 소멸자  (0) 2016.04.19
C++ 6.접근 지정자  (0) 2016.04.19

이 글을 공유하기

댓글

Designed by JB FACTORY