728x90
접근제어 속석을 생략할 경우
- struct는 public이 기본
- class는 private이 기본
class Integer{ // Integer라는 이름의 class
private: // 속성
int val; // 멤버변수, private 속성
public: //이 줄 이하는 public속성
int getVal(); // 멤버함수, 출력, getter
int setVal(); // 멤버함수 , 입력, setter
Integer Val2; // 객체를 만드는 방법
class명? : Interger
객체명? : val
- 클래스 멤버의 접근 권한
- 멤버 변수와 멤버 함수를 선언하기 전에 그들의 속성(멤버의 액세스 권한)을 지정
- 클래스 외부에서 멤버에 접근할 수 있는 권한
- private : 해당 클래스 내부에서만 접근할 수 있다.
디폴트 속성으로 생략 가능 - public : 어디에서나 접근할 수 있다.
- protected : private이지만 자식에게는 접근할 수 있도록 한다.
- private : 해당 클래스 내부에서만 접근할 수 있다.
private
public
클래스 정의
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;
}
};
#include <iostream>
int main()
{
Man han;
han.setAge(15);
han.setWeight(40.5);
std::cout << han.getAge() << " " << han.getWeight() << std::endl;
}
클래스 다이어그램
멤버함수를 클래스 외부에서 정의
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;
}
#include <iostream>
int main()
{
Man han;
han.setAge(15);
han.setWeight(40.5);
std::cout << han.getAge() << " " << han.getWeight() << std::endl;
}
전역변수
#include <iostream>
using std::cout;
int a=3; //전역변수
int main()
{
int a=10; //지역변수
a=a+10; cout<<a<<","; //지역변수 a, 20
::a=::a+3;cout<<::a; //전역변수 a, 6
return 0;
}
namespace
//aa.h
namespace AA
{
int add(int x, int y)
{
return x + y;
}
}
//bb.h
namespace BB
{
int add(int x, int y)
{
return x + y + 1;
}
}
#include <iostream>
#include "aa.h"
#include "bb.h"
int add(int x, int y) { return x + y + 2; }
int main()
{
std::cout << AA::add(1, 2) << std::endl;
std::cout << BB::add(1, 2) << std::endl;
std::cout << ::add(1, 2);//전역 namespace
return 0;
}
std namespace
inline 함수
- C++에서는 함수 선언이나 정의 앞에 inline 이라는 키워드를 사용하여 매크로 함수의 부작용을 없애면서 같은 기능을 수행
- inline 함수는 컴파일러에 의해 처리되며 텍스트가 아닌 함수 코드 블록의 복사본인 기계어 코드가 직접 삽입된다.
- 함수이므로 매개변수의 데이터형을 확인할 수 있다.
- 함수를 호출하고 값을 반환하는데 드는 시간상의 지체(overhead)를 줄일 수 있다. (하지만 여러번 호출하는 경우 프로그램 크기가 커져 실행 속도가 늦어지는 단점이 있다.)
#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 함수가 된다.
실습
#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;
}
수정 전
728x90
'C++' 카테고리의 다른 글
9-2 디폴트 인자(매개변수) (0) | 2023.11.15 |
---|---|
9-1 함수중첩 (0) | 2023.11.15 |
5-2 객체지향 프로그래밍 (0) | 2023.10.12 |
5-1 구조체 (2) | 2023.10.11 |
4-2 함수(funtion)2 (0) | 2023.09.28 |