U E D R , A S I H C RSS

AcceleratedC++/Chapter9

1. Chapter 9 Defining new types

κΈ°λ³Έ νƒ€μž… char, int, double λ“± κΈ°λ³Έμ–Έμ–΄μ˜ 일뢀
클래슀 νƒ€μž… string, vector, istream λ“± κΈ°λ³Έμ–Έμ–΄λΌ κ°€μ§€κ³  κ΅¬ν˜„λœ νƒ€μž…
C++은 클래슀 νƒ€μž…κ³Ό 같은 μ‚¬μš©μžκ°€ μ •μ˜ν•œ μ–΄ν”Œλ¦¬μΌ€μ΄μ…˜ 고유의 νƒ€μž…μ„ μ •μ˜ν•  수 μžˆλŠ” λ§Žμ€ κΈ°λŠ₯은 μ§€μ›ν•œλ‹€.

1.1. 9.1 Student_info revisited

4.2.1절 Student_info κ΅¬μ‘°μ²΄λΌ λ‹€λ£¨λŠ” ν•¨μˆ˜λΌ μž‘μ„±ν•˜κ³ , μ΄λΌ ν•œκ°œμ˜ ν—€λ”νŒŒμΌλ‘œ 톡합을 ν•˜λŠ” 것은 μΌκ΄€λœ 방법을 μ œκ³΅ν•˜μ§€ μ•ŠκΈ° λ•Œλ¬Έμ— λ¬Έμ œκ°€ λ°œμƒν•œλ‹€.
λ³Έ μž₯μ—μ„œλŠ” 기쑴의 κ΅¬μ‘°μ²΄λΌ ν™•μž₯ν•˜μ—¬ ν•¨μˆ˜λΌ μž‘μ„±ν•˜κ³  μ΄λŸ°μ‹μ˜ λ¬Έμ œλΌ ν•΄κ²°ν•  수 μžˆλŠ” 방법을 μ•Œλ €μ€λ‹€.

1.2. 9.2 Class types

~cpp 
struct Student_info {
	std::string name;
	double midterm, final;
	std::vector<double> homework;
};
ν”„λ‘œκ·Έλž˜λ¨ΈλŠ” κ΅¬μ‘°μ²΄λΌ λ‹€λ£¨κΈ° μœ„ν•΄μ„œ ꡬ쑰체의 각 λ©€λ²„λΌ λ‹€λ£¨λŠ” ν•¨μˆ˜λΌ μ΄μš©ν•΄μ•Όν•œλ‹€. (Student_info λΌ μΈμžλ‘œ κ°–λŠ” ν•¨μˆ˜λŠ” μ—†κΈ° λ•Œλ¬Έμ—)
κ·ΈλŸ¬λ‚˜ ν”„λ‘œκ·Έλž˜λ¨ΈλŠ” μ§μ ‘μ μœΌλ‘œ 이런 κ΅¬μ‘°λΌ λ‹€λ£¨κΈ° λ³΄λ‹€λŠ” 세뢀적인 κ΅¬ν˜„μ„ 감좔고 싢을 수 μžˆλ‹€.

μ™œ using-선언문을 μ‚¬μš©ν•˜μ§€ μ•ŠλŠ”κ°€?
string, vector 와 같은 것듀은 Student_info의 λ‚΄λΆ€ κ΅¬ν˜„μ‹œμ— ν•„μš”ν•œ 사항이기 λ•Œλ¬Έμ— Student_infoλΌ μ‚¬μš©ν•˜λŠ” ν”„λ‘œκ·Έλž¨μ˜ λ˜λ‹€λ₯Έ ν”„λ‘œκ·Έλž˜λ¨Έμ—κ²ŒκΉŒμ§€ vector, string을 std::에 μ‘΄μž¬ν•˜λŠ” κ²ƒμœΌλ‘œ μ“°κΈ°λΌ κ°•μš”ν•˜λŠ” 것은 μ˜³μ§€μ•Šλ‹€.

1.2.1. 9.2.1 멀버 ν•¨μˆ˜

μƒκΈ°μ˜ κ΅¬μ‘°μ²΄μ•ˆμ— Student_info λΌ λ‹€λ£° 수 μžˆλŠ” λ©€λ²„ν•¨μˆ˜λΌ μΆ”κ°€ν•œ 것
~cpp 
struct Student_info {
	std::string name;
	double midterm, final;
	std::vector<double> homework;
	
	std::istream& read(std::istream&);		//μž…λ ₯ 슀트림으둜 λΆ€ν„° μž…λ ₯을 λ°›μ•„μ„œ 4개의 λ©€λ²„λ³€μˆ˜λΌ μ΄ˆκΈ°ν™”ν•œλ‹€. 
	double grade() const; 					//λ‚΄λΆ€ λ©€λ²„λ³€μˆ˜λΌ ν™œμš©ν•˜μ—¬ μ μˆ˜λΌ κ³„μ‚°ν•˜κ³  double ν˜•μœΌλ‘œ λ¦¬ν„΄ν•œλ‹€.
											//ν•¨μˆ˜ λͺ…μ˜ 뒀에 constλΌ λΆ™μ΄λ©΄ 멀버 λ³€μˆ˜μ˜ λ³€ν˜•μ„ ν•  수 μ—†λŠ” ν•¨μˆ˜κ°€ λœλ‹€. (ꡬ쑰적으둜 μ’‹λ‹€) 
};
 
  • s:Student_info 라면 λ©€λ²„ν•¨μˆ˜λΌ ν˜ΈμΆœν•˜κΈ° μœ„ν•΄μ„œλŠ” s.read(cin), s.grade() 와 같이 ν•¨μˆ˜λΌ μ‚¬μš©ν•˜λ©΄μ„œ κ·Έ ν•¨μˆ˜κ°€ μ†ν•΄μžˆλŠ” κ°μ²΄λΌ μ§€μ •ν•΄μ•Όν•¨. μ•”λ¬΅μ μœΌλ‘œ νŠΉμ •κ°μ²΄κ°€ κ·Έ ν•¨μˆ˜μ˜ 인자둜 μ „λ‹¬λ˜μ–΄ κ·Έ 객체의 λ°μ΄ν„°λ‘œ 접근이 κ°€λŠ₯ν•˜κ²Œ λœλ‹€.
    ~cpp 
    istream & Student_info::read(istream& in)
    {
    	in>>name>>midterm>>final;
    	read_hw(in, homework);
    	return in;
    }
     
    이 ν•¨μˆ˜μ—μ„œ μ•Œμ•„μ•Όν•  3가지
    • ν•¨μˆ˜μ˜ 이름이 Student_info::read이닀
    • Student_info의 λ©€λ²„ν•¨μˆ˜μ΄λ€λ‘œ Student_info κ°μ²΄λΌ μ •μ˜ν•  ν•„μš”λ„ 인자둜 λ„˜κΈΈ ν•„μš”λ„ μ—†λ‹€.
    • 객체 λ‚΄λΆ€μ˜ λ°μ΄ν„°λΌ μ§μ ‘μ μœΌλ‘œ μ ‘κ·Ό κ°€λŠ₯ν•˜λ‹€. μ˜ˆλΌ λ“€μ–΄μ„œ s.name으둜 μ ‘κ·Όν•  ν•„μš”μ—†λ‹€.

  • ~cpp 
    double Student_info::grade() const {
    	return ::grade(midterm, final, homework);
    }
     
    ::λΌ μ‚¬μš©ν•¨μœΌλ‘œμ¨ ν˜ΈμΆœλ˜λŠ” gradeλΌ κ°μ²΄μ˜ λ©€λ²„ν•¨μˆ˜κ°€ μ•„λ‹ˆλΌ μ „μ—­ grade의 ν˜•μœΌλ‘œ μ‚¬μš©ν•˜λŠ” 것이 κ°€λŠ₯ν•˜λ‹€.
    κ°μ²΄μ—μ„œλŠ” const 객체 λ‚΄λΆ€μ˜ 데이터듀을 constν˜•μœΌλ‘œ λ‹€λ£° μˆ˜λŠ” μ—†μ§€λ§Œ, λ©€λ²„ν•¨μˆ˜λΌ const λ©€λ²„ν•¨μˆ˜λ‘œ λ§Œλ“¦μœΌλ‘œμ¨ λ³€ν˜•μ— μ œν•œμ„ μ„ μˆ˜ μžˆλ‹€.
    • const 객체에 λŒ€ν•΄μ„œλŠ” const λ©€λ²„ν•¨μˆ˜κ°€ μ•„λ‹Œ ν•¨μˆ˜λŠ” 호좜 ν•  수 μ—†λ‹€.

1.2.2. 9.2.2 비멀버 ν•¨μˆ˜λ“€

이전에 μ‚¬μš©λ˜μ—ˆλ˜ compare와 같은 ν•¨μˆ˜λΌ μ–΄λ–€μ‹μœΌλ‘œ μ •μ˜ν•΄μ•Ό ν•  것인가?
compareν•¨μˆ˜λŠ” λ™μΌν•œ ν˜•μ˜ 2개의 Student_infoλΌ λ°›μ•„μ„œ μ„œλ‘œλΌ λΉ„κ΅ν•˜λŠ” 역할을 ν•œλ‹€. μ΄λŸ°ν•¨μˆ˜λΌ μ²˜λ¦¬ν•˜λŠ” 일반적인 방법이 μžˆλŠ”λ°, 9.5, 11.2.4, 11.3.2, 12.5, 13.2.1 μ—μ„œ 배우게됨.
friend ν•¨μˆ˜λΌ μ˜λΈν•˜λŠ” 것

1.3. 9.3 Protection

read, gradeλΌ μ •μ˜ν•¨μœΌλ‘œμ¨ Student_info에 직접적인 접근을 ν•˜μ§€ μ•Šκ³ λ„ λ°μ΄ν„°λΌ λ‹€λ£° 수 μžˆμ—ˆλ‹€.
그런데 더 λ‚˜κ°€μ„œ μ΄μ œλŠ” μ•„μ˜ˆ 이 객체의 λ‚΄λΆ€λ©€λ²„λΌ ν”„λ‘œκ·Έλž¨μ˜ λ‹€λ₯Έ λΆ€λΆ„μ—μ„œ λ‹€λ£¨λŠ” 것을 κΈˆμ§€μ‹œν‚€κ³  μ‹Άλ‹€λ©΄?


~cpp 
class Student_info {
public:
	//interface is here
	double grade() const;
	std::istream& read(std::istream&);
private:
	//implementation is here
	std::string name;
	double midterm, final;
	std::vector<double> homework;
};
 
struct ν‚€μ›Œλ“œ λŒ€μ‹  class ν‚€μ›Œλ“œ μ‚¬μš©. λ³΄ν˜Έλ ˆμ΄λΈ”(protection label) μ‚¬μš©. λ ˆμ΄λΈ”μ€ μˆœμ„œμ—†μ΄ μ—¬λŸ¬λΆ„ μ€‘λ³΅μœΌλ‘œ λ‚˜μ™€λ„ 무관함.
private λ ˆμ΄λΈ” 뒀에 μ‘΄μž¬ν•˜λŠ” λ³€μˆ˜, ν•¨μˆ˜λ“€μ€ λΉ„ λ©€λ²„ν•¨μˆ˜μ—μ„œ μ ‘κ·Όν•  수 μ—†λ‹€. 반면 public은 접근이 κ°€λŠ₯ν•˜λ‹€.
class ν‚€μ›Œλ“œλΌ μ‚¬μš©ν•œ 클래슀 κΈ°λ³Έ 보호λͺ¨λ“œκ°€ private 으둜 λ™μž‘ν•œλ‹€.
struct ν‚€μ›Œλ“œλΌ μ‚¬μš©ν•œ 클래슀 κΈ°λ³Έ 보호λͺ¨λ“œκ°€ public 으둜 λ™μž‘ν•œλ‹€.
β€» 이것을 보면 C++μ—μ„œ 클래슀의 κ΅¬ν˜„μ€ Cμ—μ„œ ꡬ쑰체의 κ΅¬ν˜„μ„ ν™•μž₯μ‹œν‚¨ κ²ƒμœΌλ‘œ λ³Ό 수 μžˆλŠ” 증거가 λœλ‹€.

λ‹€μŒμ€ 각기 λ™μΌν•œ ν‘œν˜„μ‹μ„ λ‚˜νƒ€λ‚Έ 2κ°€μ§€μ˜ κ²½μš°μ΄λ‹€.
~cpp 
class Student_info {
public:
	double grade() const;
};

struct Student_info {
	dobule grade() const;
};
 
~cpp 
class Student_info {
	std::string name;
public:
	double grade() const;
};

struct Student_info {
private:
	std::string name;
public:
	dobule grade() const;
};
 
일반적으둜 μžλ£Œκ΅¬μ‘°κ°€ 간단할 λ•Œμ—λŠ” structλΌ μ΄μš©ν•œλ‹€. κ·ΈλŸ¬λ‚˜ 2가지 ν‚€μ›Œλ“œμ˜ μ‚¬μš©μ˜ μ°¨μ΄λŠ” μ‘΄μž¬ν•˜μ§€ μ•ŠλŠ”λ‹€. 단지 λ¬Έμ„œν™”λΌ μ–΄λ–»κ²Œ ν•˜λŠλƒμ— μ˜ν•΄ 차이가 생길 뿐이닀.

1.3.1. 9.3.1 μ ‘κ·Ό ν•¨μˆ˜


~cpp 
class Student_info {
public:
	double grade() const;
	std::istream& read(std::istream&);
	std::string name() const {return n;}
private:
	std::string n;
	double midterm, final;
	std::vector<double> homework;
};
 
name 멀버 λ³€μˆ˜μ— μ§μ ‘μ μœΌλ‘œ μ ‘κ·Όν•˜λŠ” λŒ€μ‹ μ— name()ν•¨μˆ˜λΌ λ§Œλ“€μ–΄μ„œ μ ‘κ·Όν•˜λ„λ‘ λ§Œλ“€μ—ˆλ‹€. const ν•¨μˆ˜μ΄λ€λ‘œ λ©€λ²„λ³€μˆ˜μ˜ 변경을 λΆˆν—ˆν•œλ‹€. 리턴값이 λ³΅μ‚¬λœ 것이기 λ•Œλ¬Έμ— 변경을 ν•˜λ”λΌλ„ λ©€λ²„λ³€μˆ˜μ— 영ν–₯을 μ£Όμ§€λŠ” μ•ŠλŠ”λ‹€.
name 멀버 ν•¨μˆ˜μ™€ 같이 클래슀 내뢀에 μ •μ˜λΌ ν•˜λŠ” 것은 inline으둜 ν•¨μˆ˜λΌ μ •μ˜ν•˜λŠ” 것도 λ™μΌν•œ νš¨κ³ΌλΌ κ°€μ§„λ‹€. μ΄λ ‡κ²Œ λ©€λ²„λ³€μˆ˜μ— 접근을 μœ„ν•΄μ„œ λ§Œλ“€μ–΄μ§€λŠ” ν•¨μˆ˜λΌ ν†΅μΉ­ μ ‘κ·Όν•¨μˆ˜(accessor function)이라고 λΆ€λ₯Έλ‹€.
μ ‘κ·Όν•¨μˆ˜λŠ” μΊ‘μŠν™”μ˜ κΈ°λ³Έκ°œλ…μ΄ λ°˜ν•˜λŠ” κ²ƒμœΌλ‘œ λ‹€λ₯Έ μΈν„°νŽ˜μ΄μŠ€ μΌλΆ€λ‘œμ„œλ§Œ μ‚¬μš©ν•΄μ•Όν•œλ‹€.


~cpp 
bool compare(const Student_info& x, const Student_info& y) {
	return x.name() < y.name();
}
 
인자둜 받은 λ³€μˆ˜λ“€μ„ 직접 μ ‘κ·Όν•˜μ§€ μ•Šκ³  κ·Έ λ©€λ²„ν•¨μˆ˜λ‘œ μ ‘κ·Όν•œλ‹€.

1.3.2. 9.3.2 λΉ„μ–΄μžˆλŠ”μ§€ ν…ŒμŠ€νŠΈ

λ§Œμ•½ s:Student_info 에 read(istream&)을 ν†΅ν•΄μ„œ λ°μ΄ν„°λΌ μž…λ ₯ν•˜μ§€ μ•Šκ³ μ„œ s.grade()λΌ μ‚¬μš©ν•œλ‹€λ©΄ ν”„λ‘œκ·Έλž¨μ„ μ—λŸ¬λΌ λ‚Ό 것이닀.


~cpp 
class Student_info {
public:
	double grade() const;
	std::istream& read(std::istream&);
	std::string name() const {return n;}
	bool valid() const { return !homework.empty(); }	// μ‚¬μš©μžμ—κ²Œ κ·Έ 객체가 μœ νš¨ν•œ λ°μ΄ν„°λΌ κ°€μ‘ŒλŠ”μ§€λΌ μ•Œλ €μ€λ‹€. 
private:
	std::string n;
	double midterm, final;
	std::vector<double> homework;
};
 
상기와 같은 λ°©μ‹μœΌλ‘œ ν†΅ν•΄μ„œ gradeλΌ ν˜ΈμΆœν•˜κΈ° 전에 객체의 μœ νš¨μ„±μ„ κ²€μ‚¬ν•œλ‹€λ©΄ μ—λŸ¬λΌ μ—†μ• λŠ” 것이 κ°€λŠ₯ν•˜λ‹€.

1.4. 9.4 The Student_info class


~cpp 
class Student_info {
public:
	double grade() const;
	std::istream& read(std::istream&);
	std::string name() const {return n;}
	bool valid() const { return !homework.empty(); }	// μ‚¬μš©μžμ—κ²Œ κ·Έ 객체가 μœ νš¨ν•œ λ°μ΄ν„°λΌ κ°€μ‘ŒλŠ”μ§€λΌ μ•Œλ €μ€λ‹€. 
private:
	std::string n;
	double midterm, final;
	std::vector<double> homework;
};
bool compare(const Student_info& x, const Student& y);
 
λ™μΌν•œ 일을 ν•˜μ§€λ§Œ ν”„λ‘œκ·Έλž˜λ¨ΈλŠ” μ‹€μ œλ‘œ 객체의 λ‚΄λΆ€μ˜ κ΅¬ν˜„μ„ κ°μΆ”λŠ” 것이 κ°€λŠ₯ν•˜λ‹€.
'''β€» 이런 μ‹μœΌλ‘œ ν—€λ”νŒŒμΌμ„ μ œκ³΅ν•˜κ³  μ‹€μ œμ˜ κ΅¬ν˜„λœ μ†ŒμŠ€νŒŒμΌμ€ μ΄λΈ μ»΄νŒŒμΌλœ obj파일둜 μ œκ³΅ν•œλ‹€λ©΄ μ‹€μ œλ‘œ νŒλ§€λ˜λŠ” μ»΄νŒŒμΌλŸ¬λΌλ„ κ·Έ μ»΄νŒŒμΌλŸ¬κ°€ μ œκ³΅ν•˜λŠ” ν•¨μˆ˜μ˜ μ‹€μ œκ΅¬ν˜„μ€ λ³Ό μˆ˜κ°€ μ—†λ‹€.
ν—€λ”νŒŒμΌμ€ 단지 μ»΄νŒŒμΌλŸ¬κ°€ μ»΄νŒŒμΌμ„ ν• λ•Œ 이런 ν•¨μˆ˜κ°€ μ‘΄μž¬ν•œλ‹€κ³  κ°€μ •λ§Œ ν•˜κ³ , μ‹€μ œλ‘œ κ·Έ ν•¨μˆ˜μ˜ μ½”λ“œκ°€ μ‘΄μž¬ν•˜λŠ” 것을 확인 ν•  수 μž‡λŠ” 것은 λͺ¨λ“  κ΄€λ ¨ 였브젝트 νŒŒμΌμ„ λ§ν¬ν•œλ’€μ— ν˜ΈμΆœν• λ•Œ μ—†λ‹€λŠ” 것을 확인할 수 λ°–μ—” μ—†λ‹€.'''

1.5. 9.5 Constructors

μƒμ„±μž(Constructor)
객체가 μ–΄λ–€κ²Œ μ΄ˆκΈ°ν™”λ˜λŠ”μ§€λΌ μ •μ˜ν•˜λŠ” νŠΉλ³„ν•œ 멀버 ν•¨μˆ˜μ΄λ‹€. λͺ…μ‹œμ μΈ ν˜ΈμΆœμ„ μ§€μ›ν•˜μ§€ μ•ŠμœΌλ©°, μ˜€λ‘œμ§€ 객체가 생성될 λ•Œμ—λ§Œ μ‹€ν–‰λœλ‹€.
λ§Œμ•½ ν”„λ‘œκ·Έλž˜λ¨Έκ°€ μƒμ„±μžλΌ λͺ…μ‹œμ μœΌλ‘œ μ§€μ •ν•˜μ§€ μ•ŠμœΌλ©΄ μ»΄νŒŒμΌλŸ¬λŠ” Default ConstructorλΌ λ§Œλ“€μ–΄μ£ΌλŠ”λ°, 이 κΈ°λ³Έ μƒμ„±μžλŠ” λͺ…μ‹œμ μΈ μ΄ˆκΈ°ν™”λΌ μ§€μ›ν•˜μ§€ μ•ŠμœΌλ©°, μ˜€λ‘œμ§€ λ™μΌν•œ ν˜•μ˜ λ‹€λ₯Έ 객체둜의 μ΄ˆκΈ°ν™”λ§Œμ„ μ œκ³΅ν•œλ‹€.
μƒμ„±μžλŠ” 클래슀의 이름과 λ™μΌν•œ μ΄λ¦„μœΌλ‘œ ν•¨μˆ˜μ˜ 이름을 κ°–λŠ”λ‹€. λ˜ν•œ λ¦¬ν„΄νƒ€μž…μ΄ μ—†λ‹€.
μƒμ„±μžμ˜ κ·œμΉ™
  • ν•˜λ‚˜μ΄μƒμ˜ μƒμ„±μžκ°€ μ‘΄μž¬ν•œλ‹€λ©΄ ν˜„μž¬ 상황에 λ§žλŠ” μƒμ„±μžλΌ μ„ νƒν•˜μ—¬ μ‹€ν–‰λœλ‹€.
  • λ‚΄μž₯ νƒ€μž… 객체인 경우 값지정 μ΄ˆκΈ°ν™”μ‹œμ—λŠ” 0으둜 λ””ν΄νŠΈ μ΄ˆκΈ°ν™” μ‹œμ—λŠ” μ •μ˜λ˜μ§€ μ•Šμ€ κ°’μœΌλ‘œ μ„ΈνŒ…λœλ‹€.
  • μƒμ„±μžλΌ κ°–μ§€ μ•ŠλŠ” νƒ€μž…μΈ 경우. κ°μ²΄λΌ μ–΄λ–€ μ‹μœΌλ‘œ μ΄ˆκΈ°ν™”ν–ˆλŠ” 가에 λ”°λΌμ„œ 값지정, λ””ν΄νŠΈ μ΄ˆκΈ°ν™”λΌ μž¬κ·€μ μœΌλ‘œ 행함.

ν˜„μž¬ Student_info ν˜•μ€ 3번째의 κ²½μš°μ— ν•΄λ‹Ήν•˜λ©°, ν˜„μž¬ μ œκ³΅λ˜λŠ” λ©€λ²„ν•¨μˆ˜λ‘œλŠ” λ¬Έμ œκ°€ 생길 일이 μ—†μ§€λ§Œ, 후에 이 ν΄λž˜μŠ€μ— κΈ°λŠ₯을 μΆ”κ°€ν•  경우 λ¬Έμ œκ°€ λ°œμƒν•  κ°€λŠ₯성이 μ‘΄μž¬ν•˜λ€λ‘œ μƒμ„±μžλΌ λ§Œλ“€μ–΄ μ£ΌλŠ” 것이 μ’‹λ‹€.


~cpp 
class Student_info {
public:
	Student_info();
	Student_info(std::istream&);
};
 

1.5.1. 9.5.1 κΈ°λ³Έ μƒμ„±μž

Default Constructor?
μ•„λ¬΄λŸ° μΈμžλΌ κ°–μ§€ μ•ŠλŠ” μƒμ„±μž. κΈ°λ³Έ μƒμ„±μžλŠ” 보톡 κ·Έ 객체의 데이터 멀버듀을 μ λ‹Ήνžˆ μ΄ˆκΈ°ν™”μ‹œν‚€κΈ°λ§Œ 함.
~cpp 
Student_info::Student_info():midterm(0). final(0) {}	// n, homework 의 경우 STLμ—μ„œ μ œκ³΅λ˜λŠ” λ””ν΄νŠΈ μƒμ„±μžκ°€ μž¬κ·€μ μœΌλ‘œ 호좜 
 

μƒμ„±μž 초기 μ„μ •μž(constructor initializer)
객체의 생성 μˆœμ„œ
* κ΅¬ν˜„ μ‹œμŠ€ν…œμ΄ κ°μ²΄λΌ λ‹΄μ„λ§Œν•œ λ©”λͺ¨λ¦¬λΌ 할당함.
* 초기 μ„μ •μž λͺ©λ‘μ— μ˜ν•΄ 객체듀을 μ΄ˆκΈ°ν™”
* μƒμ„±μž λ³Έμ²΄λΌ μ‹€ν–‰
μƒκΈ°μ—μ„œ 보λ“이 : { 사이에 μƒμ„±μžλΌ λͺ…μ‹œμ μœΌλ‘œ μ‚¬μš©ν•¨μœΌλ‘œμ¨ κ°μ²΄λΌ μ΄ˆκΈ°ν™”ν•˜λŠ” μž‘μ—…μ„ ν•˜λŠ” 쀑에 0으둜 μ΄ˆκΈ°ν™” ν•¨μœΌλ‘œμ¨ μ˜€λ²„ν—€λ“œλΌ μ„μ΄λŠ” 것이 κ°€λŠ₯ν•˜λ‹€.
β€»λ§Œμ•½ 객체의 λ©€λ²„λ‘œ const 멀버가 μ‘΄μž¬ν•œλ‹€λ©΄ κ·Έ λ©€λ²„λŠ” λ°˜λ“œμ‹œ 이 ν‘œν˜„μœΌλ‘œλ§Œ μ΄ˆκΈ°ν™”κ°€ κ°€λŠ₯ν•˜λ‹€. 객체가 μ΄ˆκΈ°ν™” λ˜λŠ” μ‹œκΈ°λŠ” μƒμ„±μž μ½”λ“œλ‘œ μ§„μž…λ˜κΈ° 이전인데 일단 μ΄ˆκΈ°ν™”λœ const λ©€λ²„λŠ” μ»΄νŒŒμΌλŸ¬κ°€ κ·Έ κ°’μ˜ μˆ˜μ •μ„ ν—ˆμš©ν•˜μ§€ μ•ŠκΈ° λ•Œλ¬Έμ΄λ‹€.

1.5.2. 9.5.2 μΈμžλΌ μ‚¬μš©ν•˜λŠ” μƒμ„±μž


~cpp 
Student_info::Student_info(istream& is) {read(is);}
 
이 μƒμ„±μžλŠ” μ‹€μ§ˆμ μΈ μž‘μ—…μ„ read ν•¨μˆ˜μ— μœ„μž„ν•©λ‹ˆλ‹€.

1.5.3. 9.5.3 Entire Source File


~cpp 
//Student_info.h
#ifndef GUARD_Student_info
#define GUARD_Student_info

#include <string>
#include <vector>

class Student_info {
public:
	Student_info();              // construct an empty `Student_info' object
	Student_info(std::istream&); // construct one by reading a stream
	std::string name() const { return n; }
	bool valid() const { return !homework.empty(); }

	// as defined in 9.2.1/157, and changed to read into `n' instead of `name'
	std::istream& read(std::istream&);

	double grade() const;    // as defined in 9.2.1/158
private:
	std::string n;
	double midterm, final;
	std::vector<double> homework;
};

bool compare(const Student_info&, const Student_info&);

#endif
 
~cpp 
//Student_info.cpp
#include <iostream>
#include <vector>

#include "grade.h"
#include "Student_info.h"

using std::istream;
using std::vector;

double Student_info::grade() const
{
	return ::grade(midterm, final, homework);
}

bool compare(const Student_info& x, const Student_info& y)
{
	return x.name() < y.name();
}

Student_info::Student_info(): midterm(0), final(0) { }

Student_info::Student_info(istream& is) { read(is); }	

// read homework grades from an input stream into a `vector<double>'
istream& read_hw(istream& in, vector<double>& hw)
{
	if (in) {
		// get rid of previous contents
		hw.clear();

		// read homework grades
		double x;
		while (in >> x)
			hw.push_back(x);

		// clear the stream so that input will work for the next student
		in.clear();
	}
	return in;
}

istream& Student_info::read(istream& in)
{
	in >> n >> midterm >> final;
	read_hw(in, homework);
	return in;
}
 

1.6. 9.6 Using the Student_info class


~cpp 
//grading_prog.cpp
#include <algorithm>
#include <iomanip>

#include <ios>

#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>

using std::max;

#include "Student_info.h"
#include "median.h"

using std::cin;                    using std::cout;
using std::domain_error;           using std::endl;
using std::setprecision;           using std::setw;
using std::sort;                   using std::streamsize;
using std::string;                 using std::vector;

int main()
{
	vector<Student_info> students;
	Student_info record;
	string::size_type maxlen = 0;

	// read and store the data
	while (record.read(cin)) {                           // changed
		maxlen = max(maxlen, record.name().size());      // changed
		students.push_back(record);
	}

	// alphabetize the student records
	sort(students.begin(), students.end(), compare);

	// write the names and grades
	for (vector<Student_info>::size_type i = 0;
	     i != students.size(); ++i) {
		cout << students[i].name()   // this and the next line changed
		     << string(maxlen + 1 - students[i].name().size(), ' ');
		try {
			double final_grade = students[i].grade();   // changed
			streamsize prec = cout.precision();
			cout << setprecision(3) << final_grade
			     << setprecision(prec) << endl;
		} catch (domain_error e) {
			cout << e.what() << endl;
		}
	}
	return 0;
}
 
----
AcceleratedC++
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:22:26
Processing time 0.0573 sec