Presentation is loading. Please wait.

Presentation is loading. Please wait.

計算機概論實習 2007-03-30. 2 What is Class In C++, class is the keyword which means encapsulation. Property and method are used to define what is class. Materialize.

Similar presentations


Presentation on theme: "計算機概論實習 2007-03-30. 2 What is Class In C++, class is the keyword which means encapsulation. Property and method are used to define what is class. Materialize."— Presentation transcript:

1 計算機概論實習 2007-03-30

2 2 What is Class In C++, class is the keyword which means encapsulation. Property and method are used to define what is class. Materialize your class in memory  Object

3 3 Class Definition Classes Model objects: Attributes (data members) Behaviors (member functions) Defined using keyword class Member functions Methods Invoked in response to messages

4 4 The General Form class ClassName{ private: data members; public: constructor(); member_functions(); }; Class Body Constructor Member access specifiers Member functions Data members

5 5 Member Access Member access specifiers public: Accessible wherever object of class in scope private: Accessible only to member functions of class protected: Accessible to class members in the member-list up to the next access specifier (public or private) or the end of the class definition.

6 6 Example class Test_Time{ int hour; Test_Time(){} void setHour(int); void print(); }; public: private: void main(){ Test_Time TT; cout << "input hour:"; TT.print(); }; Error: 無法存取 private 成員 cin >> TT.hour; TT.setHour(24);

7 7 Member Functions Defined Outside Class ReturnType ClassName::MemberFunctionName( InputType ) { statement…; } void Test_Time::print(){ cout << "The time is:" << hour << ":" << min << ":" << sec << endl; } void Test_Time::setHour(int h){ if (h = 0) hour = h; else hour = 0; }

8 8 Constructor Constructor function Special member function Initializes data members Same name as class Called when object instantiated Several constructors Function overloading No return type

9 9 Example class Test_Time{ public: int hour, min, sec; Test_Time(); Test_Time(int, int, int); void print(); }; Test_Time::Test_Time(int h, int m, int s){ hour = h; min = m; sec = s; } void main() { Test_Time TT(24,24,24); TT.print(); }

10 10 Use Header File to Maintain Easily Header files contain Function prototypes Definitions of data types and constants Header files ending with.h Programmer-defined header files #include “myheader.h” Library header files #include

11 11 Practice 3 (P3) Input: Please write a program that a teacher can input three students’ name and their grades including, Chinese, English, and Mathematics Output: Please output these students’ name and their average grades. Advanced: Output above information in average grades order.


Download ppt "計算機概論實習 2007-03-30. 2 What is Class In C++, class is the keyword which means encapsulation. Property and method are used to define what is class. Materialize."

Similar presentations


Ads by Google