7주차_C++프로그래밍
C++에서 많이 사용하는 구조체
struct Man {
int age;
double weight;
};
#include <iostream>
int main()
{
Man han; //c++에서는 struct안써도 됨
han.age = 10;
han.weight = 20.5;
std::cout << han.age << " " << han.weight << std::endl;
}
struct을 class으로 바꾸면 오류가 발생한다.
class Man {
int age;
double weight;
};
#include <iostream>
int main()
{
Man han; //c++에서는 struct안써도 됨
han.age = 10; //'Man::age': private 멤버('Man' 클래스에서 선언)에 액세스할 수 없습니다.
han.weight = 20.5;
std::cout << han.age << " " << han.weight << std::endl;
}
class밑에 public을 작성하면 오류가 해결된다.
class Man {
public:
int age;
double weight;
};
#include <iostream>
int main()
{
Man han; //c++에서는 struct안써도 됨
han.age = 10; //'Man::age': private 멤버('Man' 클래스에서 선언)에 액세스할 수 없습니다.
han.weight = 20.5;
std::cout << han.age << " " << han.weight << std::endl;
}
구조체에서 struct은 public이 기본, class는 private이 기본이다. 기본이라는 것은 생략이 가능
- class명 : Integer
* class는 새로운 자료형을 만드는 것. class 다음에 있는 것이 class의 이름
- 객체명 : Val1(현재는 잘 쓰지 않음)과 Integer Val2
클래스 멤버의 접근 권한
- 멤버변수와 멤버함수를 선언하기 전에 그들의 속성(멤버의 엑세스 권한)을 지정
- 클래스 외부에서 멤버에 접근할 수 있는 권한
- 전용 (private)
: 해당 클래스 내부에서만 접근할 수 있다.
: 디폴트 속성으로 생략 가능
- 범용 (public)
: 어디에서나 접근할 수 있다.
- 보호 (protected)
: private이지만 자식에게는 접근할 수 있도록 한다.
노란 하이라이트 친 부분은 기본 접근 속성으로 생략 가능하다.
* C++에서는 class 안에 아무것도 안쓰면 private이 기본
- 캡슐화 : 자료를 외부로부터 은폐하여 외부로부터의 잘못된 조작이나 사용에서 보호받기 위한 방법 제공
감추고 싶으면 private 선언
public은 누구나 접근할 수 있다.
protected는 동일한 클래스의 멤버함수와 현재 클래스를 상속받아 생성된 파생(자식)클래스의 멤버함수만 직접 접근가능
즉, 자신과 자식의 멤버함수만 접근 가능하다.
#include <iostream>
class Man {
private:
int age;
double weight;
public:
int getAge() {
return age;
}
void setAge(int a) {
age = a;
}
};
int main()
{
Man han; //c++에서는 struct안써도 됨
han.setAge(5);
std::cout << han.getAge() << std::endl;
}
#include <iostream>
class Man {
private:
int age;
double weight;
public:
int getAge() {
return age;
}
void setAge(int a) {
age = a;
}
double getWeight() {
return weight;
}
void setWeight(double w) {
weight = w;
}
};
int main()
{
Man han; //c++에서는 struct안써도 됨
han.setAge(5);
han.setWeight(30.5);
std::cout << han.getAge() << " " << han.getWeight() << std::endl;
}
멤버함수를 클래스 안에서 정의
class diagram 그리기 : 위에 소스를 기준으로
<class 이름> Man |
<멤버 변수> -age : int -weight : double |
<멤버 함수> +getAge() +setAge() +getWeight() +setWeight() |
- : private
+ : public
- 함수 정의
: 함수 만들기
: 이름, 매개변수, 리턴형, 기능
- 함수 호출
: 함수 사용하기
: 이름, 매개변수
- 함수 선언
: 함수의 사용법
: 이름, 매개변수, 리턴형
: 컴파일러에게 함수에 대한 정보를 미리줌
* 문장 끝에 ; (세미콜론) 사용하면 " 선언 "
* 함수 정의 과정에서 오류가 나는 소스
#include <iostream>
class Man {
private:
int age;
double weight;
public:
int getAge();
void setAge(int a);
double getWeight();
void setWeight(double w);
};
int getAge() {
return age;
}
void setAge(int a) {
age = a;
}
double getWeight() {
return weight;
}
void setWeight(double w) {
weight = w;
}
int main()
{
Man han; //c++에서는 struct안써도 됨
han.setAge(5);
han.setWeight(30.5);
std::cout << han.getAge() << " " << han.getWeight() << std::endl;
}
#include <iostream>
//함수 선언
class Man {
private:
int age;
double weight;
public:
int getAge();
void setAge(int a);
double getWeight();
void setWeight(double w);
};
// 함수 정의
int Man::getAge() {
return age;
}
void Man::setAge(int a) {
age = a;
}
double Man::getWeight() {
return weight;
}
void Man::setWeight(double w) {
weight = w;
}
// 함수 호출
int main()
{
Man han; //c++에서는 struct안써도 됨
han.setAge(5);
han.setWeight(30.5);
std::cout << han.getAge() << " " << han.getWeight() << std::endl;
}
* 오류 수정 : 함수를 정의할 때 Man::을 작성하여, 소속을 밝혀준다.
범위 지정 연산자 (scope resolution operator) ' :: '
- 멤버 함수가 어느 클래스에 포함되어 있는지
- 함수 안에서 전역변수에 접근 시
변수 앞에 ::을 사용하면, 지역변수가 아닌 전역변수임을 나타낸다.
using과 namespace
* 선호하는 방법
#include <iostream>
int main()
{
std::cout << "소프트웨어" << std::endl;
return 0;
} //std동네에 있는 cout
#include <iostream>
using std::cout;
using std::endl;
int main()
{
std::cout << "소프트웨어" << std::endl;
return 0;
} //std동네에 있는 cout
* 비선호하는 방법 (namespace는 잘 사용하지 않음)
#include <iostream>
using namespace std;
//네임스페이스로 std 사용, 잘 쓰지 않음
int main()
{
cout << "소프트웨어" << endl;
return 0;
}
std namespace
std는 표준 동네로 cin, cout 과 같은 애들이 산다.
inline 함수
#include <iostream>
using std::cout
;
#define sum(i, j) i + j // 매크로함수
inline int iSum(int i, int j) // inline 함수
{
return i + j
;
}
int add(int i, int j) // 일반 함수
{
return i + j;
}
int main()
{
cout << sum(10, 20) / 2 << ","; //10+20/2, 매크로함수의 부작용
cout << iSum(10, 20) /2 << ","; //(10+20) /2
cout << add(10, 20) /2; //(10+20) /2
return 0;
}
자동 inline 함수
멤버함수를 클래스 내부에 정의를 하면 앞에 inline함수를 안써도 자동 inline함수가 된다.
객체의 멤버 호출
#include <iostream>
using std::cout;
class Dog {
private:
int age;
public:
int getAge();
void setAge(int a);
};
int Dog::getAge()
{
return age;
}
void Dog::setAge
(int a)
{
age = a;
}
int main()
{
Dog happy; // Dog class의 happy객체 정의
Dog.age=2; //① Dog는 class
happy.age=3; //② age는 private멤버로 클래스 밖에서 접근 불가
cout << happy.age; //③ age는 전용멤버로 접근 불가
return 0;
}
위 소스는 int main 이후 부분에 오류가 발생한다.
#include <iostream>
using std::cout;
class Dog {
private:
int age;
public:
int getAge();
void setAge(int a);
};
int Dog::getAge()
{
return age;
}
void Dog::setAge
(int a)
{
age = a;
}
int main()
{
Dog happy; // Dog class의 happy객체 정의
happy.setAge(3); //② age는 private멤버로 클래스 밖에서 접근 불가
cout << happy.getAge(); //③ age는 전용멤버로 접근 불가
return 0;
}
오류가 발생한 소스를 위와 같이 수정했다. int main 이후의 부분을 이렇게 수정하면 재사용하기 쉬운 소스가 된다.