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() 이 되어야죠.. 그리고 앞으로는.. 오류내용도 같이 올리면 더 다른분들이 이해하기 편하실꺼에요.
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2658192 | C언어기초3문제 (3) | 해샘찬 | 2024-06-13 |
2658165 | 로또게임질문 ㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠ | 내꽃 | 2024-06-13 |
2658136 | 구조체 부분에서 막혔습니다.. (3) | 난슬 | 2024-06-13 |
2658106 | c언어 간단한 질문입니다 (4) | 아잉누님 | 2024-06-13 |
2658077 | security가 붙은 함수 관련 질문 | 횃대비 | 2024-06-12 |
2658050 | 기초 c언어 프로그램 짜는데 어려워서 글올려요 | 꺆잉 | 2024-06-12 |
2658021 | #define 문 정말 간단한 질문있습니다. | 심플포텐 | 2024-06-12 |
2657962 | IPv6에서 RAS 사용요 | 상1큼한렩 | 2024-06-11 |
2657935 | 도와주세요 배열및 함수관련. (4) | 하루키 | 2024-06-11 |
2657885 | 두개의 소문자입력뒤 비트연산자로 대문자오바꾸기 | 풀잎 | 2024-06-11 |
2657830 | 경우의 수 구하기.. (1) | 흰우유 | 2024-06-10 |
2657806 | 연산자 질문요 (1) | 네코 | 2024-06-10 |
2657782 | 콘솔창 꽉채우기 | 미쁘다 | 2024-06-10 |
2657754 | 유니코드 기반으로 문자 입출력받는데 안됩니다 | 어리버리 | 2024-06-09 |
2657728 | 포인터 배열도 동적할당(malloc)를 통해서 생성이 가능한가요? | 우미 | 2024-06-09 |
2657700 | 오류 해석을 못하겠어요. | 해길 | 2024-06-09 |
2657642 | 출력부분에서.. | 앨버트 | 2024-06-08 |
2657620 | 파일 입출력 함수 반복문 .. | 서리서리 | 2024-06-08 |
2657594 | 인수의 존재 여부를 검사 (1) | 다이 | 2024-06-08 |
2657566 | 서비스팩을 설치하더니 잘되는데요.... | 감추어왔던 | 2024-06-08 |