c++클래스 질문좀 하겠습니다 ㅠ
찬바리
cpp파일을헤더파일, cpp파일, 클라이언트 파일로 만들어서 동시에 돌려야하는데, cpp파일을 만들었는데 클래스로 분리하면 자꾸 오류가뜨네요 ㅠㅠ
cpp파일
#include iostream
#include string
using namespace std;
class Fan
{
private:
int speed;
bool on;
double radius;
string color;
public:
Fan()
{
speed = 1;
on = false;
radius = 5;
color = string(blue);
}
int getSpeed()
{
return speed;
}
void setSpeed(int speed)
{
this-speed = speed;
}
bool isOn()
{
return on;
}
void setOn(bool trueOrFalse)
{
this-on = trueOrFalse;
}
double getRadius()
{
return radius;
}
void setRadius(double radius)
{
this-radius = radius;
}
string getColor()
{
return color;
}
void setColor(string color)
{
this-color = color;
}
};
int main()
{
cout First fan properties: endl;
Fan fan;
fan.setSpeed(3);
fan.setRadius(10);
fan.setOn(true);
fan.setColor(yellow);
cout Fan speed: fan.getSpeed() endl;
cout Fan radius: fan.getRadius() endl;
cout Fan on? fan.isOn() endl;
cout Fan color: fan.getColor() endl;
Fan fan1;
fan.setSpeed(2);
fan.setRadius(5);
fan.setOn(false);
fan.setColor(blue);
cout \nSecond fan properties: endl;
cout Fan speed: fan1.getSpeed() endl;
cout Fan radius: fan1.getRadius() endl;
cout Fan on? fan1.isOn() endl;
cout Fan color: fan1.getColor() endl;
return 0;
}
=====================================================================================================//헤더파일
#include string
using namespace std;
class Fan
{
private:
int speed;
bool on;
double radius;
string color;
public:
Fan();
Fan(int speed, bool on, double radius, string color);
int getSpeed();
double getRadius();
bool getOn();
string getColor();
};
//cpp 파일
#include Fan.h
Fan::Fan()
{
speed = 1;
on = false;
radius = 5;
color = string(blue);
}
Fan::Fan(int speed, bool on, double radius, string color)
{
this-speed = speed;
this-on = true || False;
this-radius = radius;
this-color = color;
}
int Speed::getspeed()
{
return speed;
}
double radius::getRadius()
{
return radius;
}
bool on::getOn()
{
return on;
}
string color::getColor()
{
return color;
}
//클라이언트 파일
#include iostream
#include string
#include Fan.h
using namespace std;
int main()
{
cout First fan properties: endl;
Fan fan;
fan.getSpeed(3);
fan.getRadius(10);
fan.getOn(true);
fan.getColor(yellow);
cout Fan speed: fan.getSpeed() endl;
cout Fan radius: fan.getRadius() endl;
cout Fan on? fan.getOn() endl;
cout Fan color: fan.getColor() endl;
Fan fan1;
fan.getSpeed(2);
fan.getRadius(5);
fan.getOn(false);
fan.getColor(blue);
cout \nSecond fan properties: endl;
cout Fan speed: fan1.getSpeed() endl;
cout Fan radius: fan1.getRadius() endl;
cout Fan on? fan1.getOn() endl;
cout Fan color: fan1.getColor() endl;
return 0;
}
클래스를 배운지 얼마 되지 않아서 제가 제데로 나눴는지도 모르겠네요 ㅠㅠ 수정해주시면 감사하겠습니다 ㅠㅠ
-
든해
네ㅠ 감사합니다 ㅠㅠ!!
-
캔서
나눈 cpp의 함수에 :: 앞에 각 클래스명을 붙이는 겁니다 ;; 즉.. int Speed::getspeed() 이게 아니고 int Fan::getspeed() 이 되어야죠.. 그리고 앞으로는.. 오류내용도 같이 올리면 더 다른분들이 이해하기 편하실꺼에요.
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2698855 | 카프-라빈 알고리즘 코딩 분석좀 도와주세요.. | 꽃봄 | 2025-06-20 |
2698829 | 학점계산기 (7) | MyWay | 2025-06-20 |
2698782 | 기초적인 함수 질문이요ㅠㅠㅠㅠ | 내담 | 2025-06-20 |
2698749 | 프로그램 짜던 도중 패닉입니다...ㅜ | 파랑 | 2025-06-19 |
2698719 | 조건부컴파일 질문입니다.~ (2) | 큐트 | 2025-06-19 |
2698693 | 재귀 함수 에러 | 바닐라 | 2025-06-19 |
2698673 | 고민이있는데 들어좀주세요!! (1) | 초코맛캔디 | 2025-06-19 |
2698644 | 1부터 n까지의 합을 구하는데 엄청긴숫자의 합을 구할때는 어떻게 해야하나요? (4) | 슬우 | 2025-06-18 |
2698616 | 다른 함수로 안넘어갑니다..;;; | 도1도캣 | 2025-06-18 |
2698587 | 배열하다 막혀서... (3) | WhiteCat | 2025-06-18 |
2698559 | 문자열을 비우는방법 (2) | 하늘 | 2025-06-18 |
2698528 | 착하고 친절한 선생씌구해염~ㅋㅋ (4) | 옆집언니야 | 2025-06-17 |
2698502 | 자료구조 큐 | 캔서 | 2025-06-17 |
2698477 | 실행화면 배경문의요 | 선아 | 2025-06-17 |
2698430 | 변수의 값이 저장이 않되네요;; (4) | 피네 | 2025-06-16 |
2698404 | C#을 배울려고 하는데 C나 C++을 알아야 하나요 ?? (1) | 신당 | 2025-06-16 |
2698342 | 프로그램 질문점녀 (4) | 데빌의눈물 | 2025-06-16 |
2698318 | 파일 입출력 질문입니다~ (2) | 꽃 | 2025-06-15 |
2698291 | 문자 출력 함수 : putchar, fputc에 관하여. | 으뜸 | 2025-06-15 |
2698261 | 씨언어 (1) | 마리 | 2025-06-15 |