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() 이 되어야죠.. 그리고 앞으로는.. 오류내용도 같이 올리면 더 다른분들이 이해하기 편하실꺼에요.
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2694450 | while문질문입니다. (1) | 허리품 | 2025-05-11 |
2694420 | C언어 질문할게요(유니코드,자료형,버퍼,캐스트연산자) | 은새 | 2025-05-11 |
2694370 | 내일까진데 함수호출 제발 도와주세요!!!!!!!!!11 | 들찬 | 2025-05-10 |
2694339 | putchar()의 괄호 안에 int c=10;로 전에 선언된 c를 넣으면 안되는 이유에서 제가 생각한 것이 그 이유가 되는지 확인하고 싶습니다. (3) | 미르 | 2025-05-10 |
2694316 | 이 코드 어디가 잘못되었는지 고수분들 ㅠㅠ (2) | 나빛 | 2025-05-10 |
2694285 | 언어 공부하는 과정 좀 추천해주세요! (1) | 아빠몬 | 2025-05-09 |
2694258 | 카운터.. 질문입니다. (4) | 하늘빛눈망울 | 2025-05-09 |
2694229 | 단순한 질문이요 (8) | 여름 | 2025-05-09 |
2694202 | 용돈을 가지고 할 수 있는 일을 여러가지로 출력하는 방법 좀 알려주세요! (2) | 미나 | 2025-05-09 |
2694145 | 화면깜빡임을 없애고 싶은데요... (1) | 어서와 | 2025-05-08 |
2694069 | unsigned 질문입니다. | 힘차 | 2025-05-07 |
2694012 | 전공 비전공자 개발자 (10) | 말글 | 2025-05-07 |
2693984 | 오버로딩이 무엇인가요? (2) | 헛매질 | 2025-05-07 |
2693956 | PlaySound재생이 안됩니다!(C에 음악넣기) | 지존 | 2025-05-06 |
2693928 | &와 *의 사용에 관한 명확한 이해 | 제나 | 2025-05-06 |
2693903 | 반복문 설명좀요 ㅠㅠ (2) | 란새 | 2025-05-06 |
2693869 | stdio.h 는 왜 쓰는건가요? (1) | 큰꽃들 | 2025-05-06 |
2693842 | 포인터 변수의 주소값끼리 더하는 것에 대해서 질문드립니다. (1) | 진솔 | 2025-05-05 |
2693811 | 소수 출력;;;; | 화이트캣 | 2025-05-05 |
2693788 | 이런 함수는 없나요? (3) | 앤드류 | 2025-05-05 |