C언어로 잘 만들어진 구조체와 관련 함수들을 클래스로 변경하는 것 자체는 크게 어렵지 않습니다.
Complex라는 구조체와 관련 함수를 4번의 변화로 클래스로 변경해 보겠습니다.
1. 구조체와 일반 함수로 구현된 Version1
#pragma once
#include <cstdio>
struct Complex
{
double real;
double imag;
};
extern void setComplex(Complex &c, double r, double i) {
real = r;
imag = i;
}
extern void readComplex(char* msg = " 복소수 =");
extern void printComplex(char* msg = "복소수 = ");
extern void addComplex(Complex a, Complex b);
Complex.h
#include "Complex.h"
Complex readComplex( char* msg ) {
Complex c;
printf(" %s ", msg);
scanf("%lf%lf", &c.real, &c.imag);
}
coid printComplex(Complext c, char* msg ) {
printf(" %s %4.2f + %4.2fi\n", msg, c.real, c.imag);
}
Complex addComplex( Complex a, Complex b ) {
Complex c;
c.real = a.real + b.real;
c.imag = a.imag + b.imag;
return c;
}
Complex.cpp
#include "Complex.h"
void main()
{
Complex a, b, c;
a = readComplex ( "A =" );
b = readComplex ( "B =" );
c = addComplex (a, b);
printComplex(a, " A = ");
printComplex(b, " B = ");
printComplex(c, " A+B = ");
}
Main.cpp
2. 복소수 클래스로의 전환 Version2
#pragma once
#include <cstdio>
class Complex
{
double real;
double imag;
public:
void setComplex( double r, double i ) {
real = r;
imag = i;
}
void readComplex( char* msg = " 복소수 =" );
void printComplex( char* msg = "복소수 = " );
void addComplex ( Complex a, Complex b );
};
Complex.h
#include "Complex.h"
void Complex::readComplex( char* msg ) {
printf(" %s ", msg);
scanf("%lf%lf", &real, &imag);
}
void Complex::printComplex( char* msg ) {
printf(" %s %4.2f + %4.2fi\n", msg, real, imag);
}
void Complex::addComplex( Complex a, Complex b ) {
real = a.real + b.real;
imag = a.imag + b.imag;
}
Complex.cpp
#include "Complex.h"
void main()
{
Complex a, b, c;
a.readComplex ( "A =" );
b.readComplex ( "B =" );
c.addComplex (a, b);
a.printComplex( " A = ");
b.printComplex( " B = ");
c.printComplex( " A+B = ");
}
Main.cpp
3. 멤버 함수 이름의 단순화 Version3
#pragma once
#include <cstdio>
class Complex
{
double real;
double imag;
public:
void set( double r, double i ) {
real = r;
imag = i;
}
void read( char* msg = " 복소수 입력 =" );
void print( char* msg = "복소수 = " );
void add( Complex a, Complex b );
};
Complex.h
#include "Complex.h"
void Complex::read( char* msg )
{
printf(" %s ", msg);
scanf("%lf%lf", &real, &imag);
}
void Complex::print( char* msg )
{
printf(" %s %4.2f + %4.2fi\n", msg, real, imag);
}
void Complex::add( Complex a, Complex b )
{
real = a.real + b.real;
imag = a.imag + b.imag;
}
Complex.cpp
#include "Complex.h"
void main()
{
Complex a, b, c;
a.read ( "A =" );
b.read ( "B =" );
c.add (a, b);
a.print ( " A = ");
b.print ( " B = ");
c.print ( " A+B = ");
}
Main.cpp
4. 모든 멤버 함수의 inline 구현 Version4
클래스 안에선 inline을 생략해도 됩니다.
#pragma once
#include <cstdio>
class Complex
{
double real;
double imag;
public:
void set( double r, double i ) {
real = r;
imag = i;
}
void read( char* msg = " 복소수 입력 =" ) {
printf(" %s ", msg);
scanf("%lf%lf", &real, &imag);
}
void print( char* msg = "복소수 = " ) {
printf(" %s %4.2f + %4.2fi\n", msg, real, imag);
}
void add( Complex a, Complex b ) {
real = a.real + b.real;
imag = a.imag + b.imag;
}
};
Complex.h
#include "Complex.h"
void main()
{
Complex a, b, c;
a.read ( "A =" );
b.read ( "B =" );
c.add (a, b);
a.print ( " A = ");
b.print ( " B = ");
c.print ( " A+B = ");
}
Main.cpp
'C++ 프로그래밍 > 클래스' 카테고리의 다른 글
객체의 복사와 복사 생성자 (0) | 2022.07.28 |
---|---|
객체 생성되는 것과 안 되는 것 (0) | 2022.07.25 |
멤버 초기화 리스트 (0) | 2022.07.25 |
생성자와 멤버 함수의 유사점과 차이점 (0) | 2022.07.25 |
클래스의 멤버 변수나 멤버 함수를 static으로 처리하는 경우 (0) | 2022.07.21 |