1
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
class Person {
public:
string name;
int age;
void setPerson(string n, int a) {
name = n;
age = a;
}
void print() {
cout << "이름 : " << name << endl;
cout << "나이 : " << age << endl;
}
};
int main() {
Person myPerson;
myPerson.setPerson("김철수", 21);
myPerson.print();
return 0;
}
2
#include <iostream>
#include <string>
using namespace std;
class Computer {
public:
string name;
int RAM;
double cpu_speed;
void setComputer(string n, int R, double c_s) {
name = n;
RAM = R;
cpu_speed = c_s;
}
void print() {
cout << "이름: " << name << endl;
cout << "RAM: " << RAM << endl;
cout << "CPU 속도: " << cpu_speed << endl;
}
};
int main() {
Computer myComputer;
myComputer.setComputer("오피스컴퓨터", 8, 4.2);
myComputer.print();
return 0;
}
3
#include <iostream>
using namespace std;
class Sum {
public:
int n1, n2;
void init(int x, int y) {
n1 = x;
n2 = y;
}
int add() {
return n1 + n2;
}
};
int main() {
Sum mySum;
mySum.init(10, 20);
cout << "첫 번째 정수: " << mySum.n1 << endl;
cout << "두 번째 정수: " << mySum.n2 << endl;
cout << "연산 결과: " << mySum.add() << endl;
return 0;
}
4
#include <iostream>
#include <string>
using namespace std;
class Book {
public:
string title, author;
};
int main() {
Book myBook;
myBook.title = "Great C++";
myBook.author = "Bob";
cout << "책 이름: " << myBook.title << endl;
cout << "책 저자: " << myBook.author << endl;
return 0;
}
6
#include <iostream>
using namespace std;
class Employee {
public:
string name;
int age;
int salary;
};
int main() {
Employee Employee1;
Employee1.name = "김철수";
Employee1.age = 38;
Employee1.salary = 200000;
cout << "Employee1:" << endl;
cout << Employee1.name << endl;
cout << Employee1.age << endl;
cout << Employee1.salary << endl;
return 0;
}
7
#include <iostream>
#include <string>
using namespace std;
class CellPhone {
public:
string number;
string model;
string color;
bool state = true;
void setOn() {
if (state == true)
state = false;
else
state = true;
}
};
int main() {
CellPhone myPhone;
myPhone.number = "010-1234-5678";
myPhone.model = "Galaxy";
myPhone.color = "Black";
myPhone.state = true;
myPhone.setOn();
cout << "CellPhone1:" << endl;
cout << myPhone.number << endl;
cout << myPhone.model << endl;
cout << myPhone.color << endl;
cout << boolalpha<< myPhone.state << endl;
return 0;
}
- cout << boolalpha << myPhone.state: 0이 아닌 false로 출력함
8
#include <iostream>
using namespace std;
class Complex {
public:
double r;
double i;
void printPlus() {
cout << r << " + " << i << "i" << endl;
}
void printMinus() {
cout << r << " - " << i << "i" << endl;
}
};
int main() {
Complex myC, myC2;
myC.r = 5;
myC.i = 3;
myC.printPlus();
myC2.r = 3;
myC2.i = 4;
myC2.printMinus();
return 0;
}
9
#include <iostream>
using namespace std;
class Triangle {
public:
int b, h;
int area() {
return b * h / 2;
}
};
int main() {
Triangle myT;
myT.b = 3;
myT.h = 4;
cout << "밑변이 " << myT.b << "이고 높이가 " << myT.h <<
"인 삼각형의 면적: " << myT.area() << endl;
return 0;
}
10
#include <iostream>
using namespace std;
class BankAccount {
public:
int number;
int balance;
void deposit(int m) {
balance += m;
}
void withdraw(int m) {
balance -= m;
}
};
int main() {
BankAccount myB;
myB.balance = 1000000;
cout << "현재 잔액: " << myB.balance << endl;
myB.deposit(1000000);
cout << "after deposit(1000000) 현재 잔액: " << myB.balance << endl;
myB.withdraw(1000000);
cout << "after withdraw(1000000) 현재 잔액: " << myB.balance << endl;
return 0;
}
11
#include <iostream>
using namespace std;
class Box {
public:
double l;
double w;
double h;
double getV() {
return l * w * h;
}
};
int main() {
Box myBox;
myBox.l = 6;
myBox.w = 7;
myBox.h = 5;
cout << "[" << myBox.l << "," << myBox.w << "," << myBox.h << "]" << endl;
cout << "상자의 부피: " << myBox.getV() << endl;
return 0;
}
12
#include <iostream>
using namespace std;
class Time {
public:
int h, m, s;
void setTime(int ha, int ma, int sa) {
h = ha;
m = ma;
s = sa;
}
void print() {
cout << h << " : " << m << " : " << s << endl;
}
};
int main() {
Time myT;
myT.setTime(07, 10, 20);
myT.print();
return 0;
}
'Programming > C++' 카테고리의 다른 글
[어서와 C++는 처음이지!] CH5 Exercise (0) | 2024.01.29 |
---|---|
[C++] 5. 생성자와 접근제어 (0) | 2024.01.29 |
[C++] 4. 클래스와 객체 (1) | 2024.01.29 |
[어서와 C++는 처음이지!] CH3 Exercise (1) | 2024.01.29 |
[C++] 3. 함수와 문자열 (1) | 2024.01.28 |