생성자 - Juice class

Juice Class

실습 예제

ch07ex8_주스클래스.cpp

주스는 과일을 받아 생성한다. 과일은 최대 2개까지 넣을 수 있다. 이를 클래스로 구현해 보자.

#include <iostream>
#include <string>

using namespace std;

class Juice
{
public:
	Juice():Juice("바나나") {}
	Juice(string m1)
	{
		material1 = m1;
	}
	Juice(string m1, string m2)
	{
		material1 = m1;
		material2 = m2;
	}
	void showJuice() const
	{
		cout << material1 << material2 << "주스" << endl;
	}

private:
	string material1 = "";
	string material2 = "";
};
 
int main()
{
	Juice j1;
	j1.showJuice();
	
	Juice j2("딸기");
	j2.showJuice();
	
	Juice j3("아보카도", "블루베리");
	j3.showJuice();

	return 0;
}

실습 문제

ch07lab7_Car.cpp

다음 조건을 만족하는 Car 클래스를 생성자를 이용해 작성하시오.


1. 멤버 변수 (private)

  • string color : 차량 색상

  • int speed : 차량 속도

  • int door : 차량 문 수


2. 생성자 (public)

  • 전달받은 _color 값을 color에 저장

  • 전달받은 _speed 값을 speed에 저장

  • 전달받은 _door 값을 door에 저장

  • 만약 speed가 음수이거나 300 초과이면 0으로 보정

  • door 값이 2~7 범위를 벗어나면 4로 보정


3. 멤버 함수: 메서드(method)

void display() const


4. main()

Last updated