計算機概論實習 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.

Slides:



Advertisements
Similar presentations
C++ Classes & Data Abstraction
Advertisements

 2003 Prentice Hall, Inc. All rights reserved. ECE 2552 Dr. S. Kozaitis Summer Summary of Topics Related to Classes Class definition Defining member.
CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
Class and Objects.
Wednesday, 10/2/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/2/02  QUESTIONS (on HW02 – due at 5 pm)??  Today:  Review of parameters  Introduction.
Road Map Introduction to object oriented programming. Classes
計算機概論實習 Files and Streams C++ views file as sequence of bytes Ends with end-of-file marker n-1 end-of-file marker 67 This is.
計算機概論實習 Review of Practice 8 (P8) PLEASE write a program that when you fail to open a file, you have to throw a exception. Directly using.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Introduction to Classes and Data Abstraction
Classes and Objects  A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
計算機概論實習 Last Practice (P4) members StudentTeacher private: char name[80]; protected: int number; public: void setName(char *); void getName(char*);
計算機概論實習 Class Sample: RECT Class weight length class RECT{ private: int weight, length; public: RECT(); int surface(); int perimeter();
計算機概論實習 A Simple Way to Handle Error if(b != 0) { cout
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
1 C++ Structures Starting to think about objects...
C++ Classes & Object Oriented Programming. Object Oriented Programming  Programmer thinks about and defines the attributes and behavior of objects. 
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
Introduction To Classes Chapter Procedural And Object Oriented Programming Procedural programming focuses on the process/actions that occur in a.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 25P. 1Winter Quarter C++: I/O and Classes Lecture 25.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CPS120: Introduction to Computer Science Functions.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
CS Introduction to Data Structures Spring Term 2004 Franz Hiergeist.
Class 4 (L34) u Constructors u Default Constructor u Example of Default Constructors u Destructors u Constructors Are Called in Global Scope u Constructors.
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Structs and Classes Structs A struct can be used to define a data structure type as follows: struct Complex { double real, imag;} // specifying a Complex.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Class & Objects C++ offers another user-defined data type known class which is the most important feature of the object-oriented programming. A class can.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Seventh step for Learning C++ Programming CLASS Declaration Public & Private Constructor & Destructor This pointer Inheritance.
 2003 Prentice Hall, Inc. All rights reserved. ECE 2552 Dr. Këpuska Summer 2004 from Dr. S. Kozaitis Spring 2003 slides 1 Summary of Chapter 6: Classes.
Procedural and Object-Oriented Programming
Creating Your Own Classes
Chapter 16: Classes and Data Abstraction
Object-Oriented Design (OOD) and C++
Review: Two Programming Paradigms
Object-Oriented Programming
Road Map Introduction to object oriented programming. Classes
CS1201: Programming Language 2
This technique is Called “Divide and Conquer”.
C++ Classes & Object Oriented Programming
Classes: A Deeper Look Outline
Object-Oriented Programming
Starting to think about objects...
Object-Oriented Programming
CLASSES AND OBJECTS.
Object-Oriented Programming
CS1201: Programming Language 2
Object Oriented Programming (OOP) Lecture No. 12
Presentation transcript:

計算機概論實習

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 Class Definition Classes Model objects: Attributes (data members) Behaviors (member functions) Defined using keyword class Member functions Methods Invoked in response to messages

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

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 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 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 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 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 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 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.