#ifndef
XXX β βif XXX is not defined yetβ
#define
XXX
#endif
: #ifndef μ pairλ‘ κ΅¬μ±λ¨// prevent multiple inclusions of header file
#ifndef TIME_H
#define TIME_H
// Time class definition
class Time
{
public:
Time(); // constructor
void setTime(int, int, int); // set hour, minute and second
void printuniversal(); // print time in universal-time format
void printStandard(); // print time in standard-time format
private:
int hour; // 0-23 (24-hour clock format)
int minute; // 0 - 59
int second; // 0 - 59
};
#endif // !TIME_H
void Time::setTime(...)
ν΄λμ€ μ€μ½ν μΈλΆμμ
λ©€λ² λ³μμ κ°μ μ΄λ¦μ μ§μ λ³μκ° λ©€λ² ν¨μμ μ μλλ€λ©΄?
::
μ κ·Όμλ₯Ό ν΅ν΄μ μ κ·Όν΄μΌ ν¨
Time::hour
μ (dot) λ©€λ² μ ν μ°μ°μ (.
)
sunset.printUniversal( );
->
)
timePtr->printUniversal( )
Time(int=0, int=0, int=0); // default constructor
#pragma once
class Time {
public:
Time();
void setTime(int, int, int);
void printUniversal();
void printStandard();
private:
int hour;
int minute;
int second;
};
~
μ ν΄λμ€μ μ΄λ¦μ λΆμ
~Time
void
νλ μλ)globalκ³Ό staticμ μμΉκ° κ°μ νΉμ±μ΄ βλΉμ·β
#pragma once
#include <string>
using std::string;
class CreateAndDestory {
public:
CreateAndDestory(int, string);
~CreateAndDestory();
private:
int objectID;
string message;
};
#include <iostream>
using std::cout;
using std::endl;
#include "Create.h"
void create(void); // prototype
CreateAndDestory Temp(2, "Temp");
CreateAndDestory first(1, "(global before main)"); // global object
int main()
{
cout << "\nMAIN FUNCTION: EXECUTION BEGINS" << endl;
CreateAndDestory second(2, "(local automatic in main)");
static CreateAndDestory third(3, "(local static in main)");
create(); // call function to create objects
cout << "\nMAIN FUNCTION: EXECUTION RESUMES" << endl;
CreateAndDestory fourth(4, "(local automatic in main)");
cout << "\nMAIN FUNCTION: EXECUTION ENDS" << endl;
return 0;
}
void create(void)
{
cout << "\nCREATE FUNCTION: EXECUTION BEGINS" << endl;
CreateAndDestory fifth(5, "(local automatic in create)");
static CreateAndDestory sixth(6, "(local static in create)");
CreateAndDestory seventh(7, "(local automatic in create)");
cout << "\nCREATE FUNCTION: EXECUTION ENDS" << endl;
}
#include <iostream>
using std::cout;
using std::endl;
#include "Create.h"
CreateAndDestory::CreateAndDestory(int ID, string msg)
{
objectID = ID;
message = msg;
cout << "Object " << objectID << " constructor runs " << message << endl;
}
CreateAndDestory::~CreateAndDestory()
{
// output newline for certain objects; helps readability
cout << (objectID == 1 || objectID == 6 ? "\n" : "");
cout << "Object " << objectID << " destructor runs "
<< message << endl;
}
1 constructor runs (global before main)
MAIN FUNCTION: EXECUTION BEGINS
2 constructor runs (local automatic in main)
3 constructor runs (local static in main)
CREATE FUNCTION: EXECUTION BEGINS
5 constructor runs (local automatic in create)
6 constructor runs (local static in create)
7 constructor runs (local automatic in create)
CREATE FUNCTION: EXECUTION ENDS
7 destructor runs (local automatic in create)
5 destructor runs (local automatic in create)
MAIN FUNCTION: EXECUTION BEGINS
4 constructor runs (local automatic in main)
MAIN FUNCTION: EXECUTION ENDS
4 destructor runs (local automatic in create)
2 destructor runs (local automatic in create)
6 destructor runs (local static in create)
3 destructor runs (local static in main)
1 destructor runs (global before main)
=
)
const
ν€μλλ₯Ό μ΄μ©
const a = 10;
a = 20; // error!
const
ν€μλλ₯Ό μ΄μ©const
λ‘ μ μΈλ λ©€λ² ν¨μλ λ°μ΄ν° λ©€λ²λ₯Ό μμ ν μ μμconst
νμΌλ‘ μ§μ ν΄μΌ ν¨const
νμΌλ‘ μ§μ ν μ μμν¨μλͺ
λ€μ βconstβκ° κ°μ§λ μλ―Έ
ν΄λΉ ν¨μμμ λ©€λ²λ³μλ₯Ό μ½κΈ°μ μ©(RDONLY)μΌλ‘ μ¬μ©νκ² λ€λ νμμ΄λ€.
μ¦, βμ½κΈ°βλ§ ν λΏ βμ°κΈ°βλ νμ§μκ² λ€λ μλ―Έμ΄λ€.
μμ ν¨μ 맀κ°λ³μμμ const
κ° κ°μ§λ μλ―Έ
βcall by referenceβλ‘ λ³΅μ¬ μ€λ²ν€λμμ΄ μ°Έμ‘°νλ λ³μλ₯Ό λ§μ°¬κ°μ§λ‘ μ½κΈ°μ μ©(RDONLY)μΌλ‘ μ¬μ©νκ² λ€λ νμμ΄λ€.
ν¨μ λ°ννμ
μμ βconstβκ° κ°μ§λ μλ―Έ
ν¨μμ λ°νκ°μ μ½κΈ°μ μ©(RDONLY)μΌλ‘ μ¬μ©νκ² λ€λ νμμ΄λ€.
ν¨μκ° λ°ννλ κ°μ right-hand-sideμ ν΄λΉνλ€. μ¬κΈ°μ μ΄ κ°μ &&
(r-value μ°Έμ‘°)λ‘ λ°μλ²λ¦¬λ©΄ μμ ν μ¬μ§κ° μκΈ΄λ€. μ΄λ¬ν κ°λ₯μ±μ μ κ±°νκ³ βν΄λΉ ν¨μκ° λ°ννλ κ°μ λμ
μ°μ°μλ₯Ό ν΅ν΄ 볡μ¬ν΄μ μ¬μ©νλΌβλ μλ―ΈλΌ μκ°λλ€.
const char* c_ptr;
char s1[] = "hello";
char s2[] = "bonjour";
const char* c_ptr = s1;
c_ptr = s2; // μ£Όμ λ³κ²½ κ°λ₯
c_ptr[0] = 'a'; // κ° λ³κ²½ λΆκ°
char* const ptr;
char s1[] = "hello";
char s2[] = "bonjour";
char* const c_ptr = s1;
c_ptr = s2; // μ£Όμ λ³κ²½ λΆκ°
c_ptr[0] = 'a'; // κ° λ³κ²½ κ°λ₯
const char* const c_ptr;
char s1[] = "hello";
char s2[] = "bonjour";
const char* const c_ptr = s1;
c_ptr = s2; // μ£Όμ λ³κ²½ λΆκ°
c_ptr[0] = 'a'; // κ° λ³κ²½ λΆκ°
λ©€λ² μ΄κΈ°νκΈ° λͺ©λ‘ (Member initializer list)
μ΄λμ λΌμ΄μ μ μ€νμ ν¬ν¨ν κ°μ²΄ μμ±μ κ³Όμ
const
λ‘ μ μΈλ λ©€λ²λ³μλ μ΄κΈ°νκ° κ°λ₯.
#pragma once
class Date {
public:
Date(int = 1, int = 1, int = 1900);
Date(int, int);
void print()const;
~Date();
private:
int month;
int day;
int year;
int checkDay(int)const;
};
#include <iostream>
using std::cout;
using std::endl;
#include "Date.h"
Date::Date(int mn, int dy, int yr) {
if (mn > 0 && mn <= 12) {
month = mn;
}
else {
month=1;
cout << "Invalid month (" << mn << ") set to 1.\n";
}
year = yr;
day = checkDay(dy);
cout << "Date object constructor fordate ";
print();
cout << endl;
}
void Date::print() const {
cout << month << '/' << day << '/' << year;
}
Date::~Date() {
cout << "Date object destructor for date ";
print();
cout << endl;
}
int Date::checkDay(int testDay) const {
static const int daysPerMonth[13] = {
0,31,28,31,30,31,30,31,31,30,31,30,31 };
if (testDay > 0 && testDay <= daysPerMonth[month])
return testDay;
if (month == 2 && testDay == 29 && (year % 400 == 0 || (year % 4 == 0 &&
year % 100 != 0)))
return testDay;
cout << "Invalid day(" << testDay << ") set to 1.\n";
return 1;
}
#pragma once
#include "Date.h"
class Employee {
public:
Employee(const char* const, const char* const,
const Date&, const Date&);
void print() const;
~Employee();
private:
char firstName[25];
char lastName[25];
const Date birthDate;
const Date hireDate;
};
#include <iostream>
using std::cout;
using std::endl;
#include <cstring>
using std::strlen;
using std::strncpy;
#include "Employee.h"
#include "Date.h"
Employee::Employee(const char *const first, const char *const last,
const Date &dateOfBirth, const Date &dateOfHire) : birthDate(dateOfBirth),
hireDate(dateOfHire)
{
int length = strlen(first);
length = (length < 25 ? length : 24);
strncpy(firstName, first, length);
firstName[length] = '\0';
length = strlen(last);
length = (length < 25 ? length : 24);
strncpy(lastName, last, length);
lastName[length] = '\0';
cout << "Employee object constructor: " << firstName << ' ' << lastName << endl;
}
void Employee::print() const
{
cout << lastName << ", " << firstName << " Hired: ";
hireDate.print();
cout << " Birthday: ";
birthDate.print();
cout << endl;
}
Employee::~Employee()
{
cout << "Employee object destructor: " << lastName << ", " << firstName << endl;
}
#include <iostream>
using std::cout;
using std::endl;
#include "Employee.h"
int main() {
Date birth(7, 24, 1949);
Date hire(3, 12, 1988);
Date birth2();
Employee manager("Bob", "Blue", birth, hire);
cout << endl;
manager.print();
cout << "\n Test Date constructor with invalid values:\n";
Date lastDayoff(14, 35, 1994);
cout << endl;
return 0;
}
λμ λ©λͺ¨λ¦¬ ν λΉ (allocation) λ° ν΄μ (release)
Heap
μ°μ°μ new
Time *ptrTime = new Time;
float *ptrNum = new float;
μ°μ°μ delete
delete ptrTime;
delete ptrNum;
new
λ₯Ό μ΄μ©ν κ°μ²΄ μ΄κΈ°ν
double *ptr = new double(3.14159);
Time *timePtr = new Time(12,45,0);
new
μ°μ°μλ λμ μΌλ‘ λ°°μ΄μ ν λΉν μ μλ€
int *gradesArray = new int[10];
int *gradesArray = new int[count1+count2];
λμ μΌλ‘ ν λΉλ λ°°μ΄μ μ κ±°
delete [] gradesArray;
[]
)κ° μκ³ gradesArrayκ° κ°μ²΄μ λ°°μ΄μ κ°λ¦¬ν€κ³ μλ€.
[]
λ₯Ό λλ½νμ§ μλλ‘ μ‘°μ¬.Static λ°μ΄ν° λ©€λ²
Public static λ°μ΄ν° λ©€λ²
::
)λ₯Ό μ¬μ©Martian::martianCount
.
)λ₯Ό μ¬μ©νλ€.
myMartian.martianCount
Static λ©€λ² ν¨μ