본문 바로가기
Programming/C++

[어서와 C++는 처음이지!] CH5 Exercise

by Lizardee 2024. 1. 29.
1
#include <iostream>
using namespace std;

class Book {
public:
	string title;
	string author;

	// 생성자
	Book(string t, string a) {
		title = t;
		author = a;
	}
};

int main() {
	Book myBook("Great C++", "Bob");

	cout << "책 이름: " << myBook.title << endl;
	cout << "책 저자: " << myBook.author << endl;

	return 0;
}

 

2
#include <iostream>
#include <string>
using namespace std;

class Airplane {
public:
	string name;
	int capacity;
	int speed;

	Airplane(string n, int c, int s) {
		name = n;
		capacity = c;
		speed = s;
	}

	void print() {
		cout << "비행기의 이름: " << name << endl;
		cout << "비행기의 용량: " << capacity << endl;
		cout << "비행기의 속도: " << speed << " Km/h" << endl;
		cout << endl;
	}
};

int main() {
	Airplane plane1{ "보잉 787", 900, 300 };
	Airplane plane2{ "에어버스 350", 400, 1000 };

	cout << "비행기 #1" << endl;
	plane1.print();

	cout << "비행기 #2" << endl;
	plane2.print();

	return 0;
}

 

3: 생성자 중복 정의
#include <iostream>
#include <string>
using namespace std;

class Box {
public:
	int length, width, height;

	// 생성자 중복 정의
	Box() {
		length = 0;
		width = 0;
		height = 0;
	}

	Box(int l, int w, int h) {
		length = l;
		width = w;
		height = h;
	}

	int getVolume() {
		return length * width * height;
	}

	void print() {
		cout << "상자의 길이: " << length << endl;
		cout << "상자의 너비: " << width << endl;
		cout << "상자의 높이: " << height << endl;
		cout << "상자의 부피: " << getVolume() << endl;
		cout << endl;
	}
};

int main() {
	Box box1;
	cout << "상자 #1" << endl;
	box1.print();

	Box box2(3, 2, 4);
	cout << "상자 #2" << endl;
	box2.print();
}

 

4
#include <iostream>
using namespace std;

class Movie {
public:
	string title;
	string director;
	double rating;

	Movie() {
		title = "idk";
		director="idk";
		rating = 0;
	}

	Movie(string t, string d, int r) {
		title = t;
		director = d;
		rating = r;
	}

	void print() {
		cout << "영화 제목: " << title << endl;
		cout << "영화 감독: " << director << endl;
		cout << "영화 평점: " << rating << endl;
		cout << endl;
	}
};

int main() {
	Movie m1("타이타닉", "제임스 카메론", 9.5);
	cout << "영화 #1" << endl;
	m1.print();

	Movie m2("지오스톰", "딘 데블린", 8.34);
	cout << "영화 #2" << endl;
	m2.print();

	return 0;
}

 

5
#include <iostream>
using namespace std;

class Complex {
public:
	int r, i;

	Complex() {
		r = 0;
		i = 0;
	}

	Complex(int _r, int _i) {
		r = _r;
		i = _i;
	}
};

Complex add(Complex a, Complex b) {
	Complex result;

	result.r = a.r + b.r;
	result.i = a.i + b.i;

	return result;
}

int main() {
	Complex com1(5, 3);
	Complex com2(3, -4);

	cout << "(" << com1.r << "+" << com1.i << "i)+(" << com2.r << com2.i << "i)=(" <<
		add(com1, com2).r << add(com1, com2).i << "i)" << endl;

	return 0;
}

 

6
#include <iostream>
#include <string>
using namespace std;

class SMS {
public:
	string sender, receiver, text;

	SMS(string s, string r, string t) {
		sender = s;
		receiver = r;
		text = t;
	}

	void print() {
		cout << "발신자: " << sender << endl;
		cout << "수신자: " << receiver << endl;
		cout << "내용: " << text << endl;
		cout << endl;
	}
};

int main() {
	SMS mySMS1("010-1234-5678", "010-1234-5656", "C++ 공부는 잘 돼가나요?");
	mySMS1.print();

	SMS mySMS2("010-1234-6767", "010-9876-5432", "네 5장까지는 문제 없네요.");
	mySMS2.print();

	return 0;
}

 

7
#include <iostream>
#include <string>
using namespace std;

class Character {
public:
	int x, y, HP;

	Character(int _x, int _y, int _HP) {
		x = _x;
		y = _y;
		HP = _HP;
	}

	void move() {
		x += 10;
	}

	void print() {
		cout << "x: " << x << " ";
		cout << "y: " << y << " ";
		cout << "HP: " << HP << endl;
	}
};

int main() {
	Character myCh{ 0,0,100 };

	cout << "캐릭터 #1" << endl;
	while (myCh.x <= 90) {
		myCh.print();
		myCh.move();
	}

	return 0;
}