반응형
const 멤버함수
int GetX() const; // const 멤버함수 선언
- 클래스 내에서만 선언 가능
- 동일 클래스에 선언 된 멤버변수 값 변경 불가능
- const 객체는 cont 함수만 호출 가능
생성자
생성자 : 객체가 생성될 때 자동으로 호출되는 함수
: 클래스와 이름은 동일하지만 return 값이 없다 (오버로딩 가능)
: 생성자가 하나라도 있으면 디폴트 생성자 기능 사라짐
이니셜라이저
생성자에서만 사용가능
생성하고 대입하는데 아니라
인자있는 형태로 생성되게 한다
Rectangle::Rectangle(const int& x1, const int& y1, const int& x2, const int& y2)
:upLeft(x1,x2),loRight(x2,y2)
{}
멤버변수도 초기화 가능하다. (참조자도)
이니셜라이저는 선언과 동시에 초기화를 해주는 것이기 때문
class A
{
private:
int num;
public:
A(int n) : num(n){} // 상수 초기화
}
private 생성자
멤버 함수 내에서만 객체 생성가능
소멸자
소멸자 : 객체가 소멸될 때 자동으로 호출되는 함수
: 생성하지 않으면 자동 생성
: 동적할당 메모리 소멸 공간으로 적합
<실습>
//lec8_lab_ point.h
#pragma once
class Point {
private:
int x;
int y;
public:
~Point(); // 소멸자
Point();
Point(const int& X, const int& Y);
void init(const int& X, const int& Y);
void setXY(const int& X, const int& Y);
void showPoint() const;
void move(const int& dist);
int getX()const;
int getY()const;
};
//lec8_lab_ point.cpp
#include "lec8_lab_ point.h"
#include <iostream>
using namespace std;
Point::~Point()
{
cout << "포인트 소멸자" << endl;
}
Point::Point()
:x(0), y(0)
{
cout << "디폴트 생성자" << endl;
}
Point::Point(const int& X, const int& Y)
:x(X),y(Y)
{
cout << "인자있는 생성자" << endl;
};
void Point::init(const int& X, const int& Y) {
x = X;
y = Y;
}
void Point::setXY(const int& X, const int& Y) {
x = X;
y = Y;
};
void Point::showPoint() const {
cout << x << " , " << y << endl;
};
void Point::move(const int& dist) {
x += dist;
y += dist;
};
int Point::getX() const {
return x;
};
int Point::getY() const {
return y;
};
//lec8_Rectangle.h
#pragma once
#include "lec8_lab_ point.h"
class Rectangle {
private:
Point LT;
Point RB;
public:
Rectangle();
~Rectangle();
Rectangle(const int& x1, const int& y1, const int& x2, const int& y2);
void init(const int& x1, const int& y1, const int& x2, const int& y2);
void showRectangle()const;
int area(const Point <,const Point&RB);
Point getLT() ;
Point getRB() const;
};
//lec8_Rectangle.cpp
#include"lec8_Rectangle.h"
#include<iostream>
using namespace std;
Rectangle::~Rectangle(){
cout << "R 소멸자" << endl;
}
Rectangle::Rectangle()
:LT(100, 100),RB(200, 200) // Pint 인자가 있는 생성자 호출
{
cout << "R 디폴트 생성자" << endl;
}
Rectangle::Rectangle(const int& x1, const int& y1, const int& x2, const int& y2)
:LT(x1, y1), RB(x2, y2)
{
cout << "R 인자 있 생성자" << endl;
}
void Rectangle::init(const int& x1, const int& y1, const int& x2, const int& y2) {
LT.init(x1, y1);
RB.init(x2, y2);
};
void Rectangle::showRectangle()const {
LT.showPoint();
RB.showPoint();
};
int Rectangle::area(const Point& LT, const Point& RB) {
return (RB.getX() - LT.getX()) * (LT.getY() - RB.getY());
};
Point Rectangle::getLT() {
return LT;
};
Point Rectangle::getRB() const {
return RB;
};
//lec8_RectangleMain.cpp
#include"lec8_Rectangle.h"
#include<iostream>
void move(Point& pt, const int& dist) {
pt.move(dist);
}
int main() {
Rectangle rect(100, 100, 200, 200);
}
주사위 게임 실습
// lec8_Dice.h
#pragma once
#include<cstdlib>
class Dice {
private:
int faceValue;
public:
void roll();
int getFaceValue() const;
};
//lec8_Dice.cpp
#include "lec8_Dice.h"
void Dice::roll() {
faceValue = rand() % 6 + 1;
}
int Dice::getFaceValue() const {
return faceValue;
}
//lec8_Player.h
#pragma once
#include <string>
#include "lec8_Dice.h"
using namespace std;
class Player {
private:
string name;
int total;
public:
void setName(const string& name);
string getName();
void roll(Dice& dice , Dice& dice2);
int getTotal();
};
//lec8_Dice.cpp
#include "lec8_Player.h"
void Player::setName(const string& name) {
this->name = name;
}
string Player::getName() {
return name;
}
void Player::roll(Dice& dice1, Dice& dice2) {
dice1.roll();
dice2.roll();
total = dice1.getFaceValue() + dice2.getFaceValue();
}
int Player::getTotal() {
return total;
}
// 메인
#include "lec8_Player.h"
#include<iostream>
using namespace std;
int main() {
Dice dice1;
Dice dice2;
Player player1;
Player player2;
player1.setName("해");
player2.setName("나");
player1.roll(dice1, dice2);
int one = player1.getTotal();
player2.roll(dice1, dice2);
int two = player2.getTotal();
if (one > two) {
cout << player1.getName() << "승";
}
else if (one == two) {
cout << "비겼";
}
else {
cout << player2.getName() << "승";
}
}
반응형
'학부내용 예습 > [ 2021 겨울 ] C++' 카테고리의 다른 글
[ C++ ] 10강. 깊은 복사, mutable, friend, static 멤버변수/함수 (0) | 2022.02.05 |
---|---|
[ C++ ] 9강. 실습, 얕은 복사 (0) | 2022.02.05 |
[ C++ ] 7강. 클래스, 객체지향 프로그래밍 (0) | 2022.01.30 |
[ C++ ] 6강. 동적할당 (0) | 2022.01.28 |
[ C++ ] 5강. 포인터 배열, 문자열 배열, 참조자, 함수 (0) | 2022.01.26 |