Chapter 6 classes and data abstraction
This presentation is the property of its rightful owner.
Sponsored Links
1 / 14

Chapter 6: Classes and Data Abstraction PowerPoint PPT Presentation


  • 65 Views
  • Uploaded on
  • Presentation posted in: General

Chapter 6: Classes and Data Abstraction. Outline 6.1Introduction 6.2Structure Definitions 6.3Accessing Members of Structures 6.4Implementing a User-Defined Type Time with a Struct 6.5Implementing a Time Abstract Data Type with a Class 6.6 Class Scope and Accessing Class Members

Download Presentation

Chapter 6: Classes and Data Abstraction

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Chapter 6 classes and data abstraction

Chapter 6: Classes andData Abstraction

Outline

6.1Introduction

6.2Structure Definitions

6.3Accessing Members of Structures

6.4Implementing a User-Defined Type Time with a Struct

6.5Implementing a Time Abstract Data Type with a Class

6.6Class Scope and Accessing Class Members

6.7Separating Interface from Implementation

6.8Controlling Access to Members

6.9Access Functions and Utility Functions

6.10Initializing Class Objects: Constructors

6.11Using Default Arguments with Constructors

6.12Using Destructors

6.13When Constructors and Destructors Are Called

6.14Using Data Members and Member Functions

6.15A Subtle Trap: Returning a Reference to aPrivate Data Member

6.16 Assignment by Default Memberwise Copy

6.17 Software Reusability


6 6 class scope and accessing class members

6.6 Class Scope and Accessing Class Members

  • 클래스 범위(Class scope)

    • 데이터멤버, 멤버함수를 직접 이름으로 접근

  • 클래스 외부 범위

    • 데이터와 멤버함수는 클래스 핸들을 통해 접근

    • 예: t.printStadard(), t->printStandard

  • 함수범위(Function scope)

    • 변수는 정의된 함수 내에서만 알려짐

    • 함수 수행이 종료되면 변수도 소멸됨


6 6 class scope and accessing class members1

6.6 Class Scope and Accessing Class Members

  • 클래스 멤버의 접근

    • Structs와 동일함

    • 객체의 경우 도트(.), 포인터의 경우 화살표(->)를 사용함

    • 예:

      • t.hour는 t의 hour요소임

      • TimePtr->hour역시 hour요소임


Chapter 6 classes and data abstraction

1. Class definition2. Create an object of the class2.1 Assign a value to the object. Print the value using the dot operator2.2 Set a new value and print it using a reference

1// Fig. 6.4: fig06_04.cpp

2// Demonstrating the class member access operators . and ->

It is rare to have public member variables. Usually only member functions are public; this keeps as much information hidden as possible.

3//

4// CAUTION: IN FUTURE EXAMPLES WE AVOID PUBLIC DATA!

5#include <iostream>

6

7using std::cout;

8using std::endl;

9

10// Simple class Count

11class Count {

12public:

13int x;

14void print() { cout << x << endl; }

15};

16

17int main()

18{

19Count counter, // create counter object

20 *counterPtr = &counter, // pointer to counter

21 &counterRef = counter; // reference to counter

22

23cout << "Assign 7 to x and print using the object's name: ";

24counter.x = 7; // assign 7 to data member x

25counter.print(); // call member function print

26

27cout << "Assign 8 to x and print using a reference: ";

28counterRef.x = 8; // assign 8 to data member x

29counterRef.print(); // call member function print

30

1. Class definition2. Create an object of the class2.1 Assign a value to the object. Print the value using the dot operator2.2 Set a new value and print it using a reference


2 3 set a new value and print it using a pointer program output

31cout << "Assign 10 to x and print using a pointer: ";

32counterPtr->x = 10; // assign 10 to data member x

33counterPtr->print(); // call member function print

34return 0;

35}

2.3 Set a new value and print it using a pointerProgram Output

Assign 7 to x and print using the object's name: 7

Assign 8 to x and print using a reference: 8

Assign 10 to x and print using a pointer: 10


6 7 separating interface from implementation

6.7 Separating Interface from Implementation

  • 인터페이스를 구현으로부터 분리

    • 프로그램 수정을 용이하도록 함

    • 해더파일(Header files)

      • 클래스 정의와 함수 원형을 저장

    • 소스코드 파일 (Source-code files)

      • 멤버 함수의 정의를 포함


Chapter 6 classes and data abstraction

File  New Project


Chapter 6 classes and data abstraction

Project  Add to Project


Chapter 6 classes and data abstraction

Execute  Rebuild All

Execute  Compile and Run


Chapter 6 classes and data abstraction

# Compiler

CC=g++

# Parameters given to the compiler

CFLAGS=-s -IC:\DEV-C_~1\Include\ -IC:\DEV-C_~1\Include\G__~1 -IC:\DEV-C_~1\Include\ -LC:\DEV-C_~1\Lib\ -BC:\DEV-C_~1\Bin\

# Output filename (*.exe)

OUTPUT="week2-HGD.exe"

# Source files

SRCS="C:\Fig6_05\WorkTime.cpp" "C:\\Fig6_05\test.cpp"

# Output object files (*.o)

OBJS="C:\Fig6_05\WorkTime.o" "C:\Fig6_05\test.o" "C:\Fig6_05\rsrc.o"

# Standard targets

all: compile_res

$(CC) -c $(SRCS) $(CFLAGS)

$(CC) -o $(OUTPUT) $(OBJS) $(CFLAGS)

compile_res:

windres --include-dir C:\DEV-C_~1\Include\ --include-dir C:\DEV-C_~1\Include\G__~1 --include-dir C:\DEV-C_~1\Include\ --use-temp-file -I rc -O coff -i "C:\강의\2003-2\프로그~1\ch06\Fig6_05\rsrc.rc" -o "C:\강의\2003-2\프로그~1\ch06\Fig6_05\rsrc.o"

Makefile의 내용


1 using the same time class as before create a header file

1//Fig. 6.5: time1.h

2// Declaration of the Time class.

3// Member functions are defined in time1.cpp

Dot ( . ) replaced with underscore ( _ ) in file name.

4

If time1.h (TIME1_H) is not defined (#ifndef) then it is loaded (#defineTIME1_H). If TIME1_His already defined, then everything up to #endifis ignored.

This prevents loading a header file multiple times.

5// prevent multiple inclusions of header file

6#ifndef TIME1_H

7#define TIME1_H

8

9// Time abstract data type definition

10class Time {

11public:

12Time(); // constructor

13void setTime( int, int, int ); // set hour, minute, second

14void printMilitary(); // print military time format

15void printStandard(); // print standard time format

16private:

17int hour; // 0 - 23

18int minute; // 0 - 59

19int second; // 0 - 59

20};

21

22#endif

1. Using the same Time class as before, create a header file


Chapter 6 classes and data abstraction

23// Fig. 6.5: time1.cpp

24// Member function definitions for Time class.

Source file uses #include to load the header file

25#include <iostream>

26

27using std::cout;

28

Source file contains function definitions

29#include "time1.h"

30

31// Time constructor initializes each data member to zero.

32// Ensures all Time objects start in a consistent state.

33Time::Time() { hour = minute = second = 0; }

34

35// Set a new Time value using military time. Perform validity

36// checks on the data values. Set invalid values to zero.

37void Time::setTime( int h, int m, int s )

38{

39hour = ( h >= 0 && h < 24 ) ? h : 0;

40minute = ( m >= 0 && m < 60 ) ? m : 0;

41second = ( s >= 0 && s < 60 ) ? s : 0;

42}

43

44// Print Time in military format

45void Time::printMilitary()

46{

47cout << ( hour < 10 ? "0" : "" ) << hour << ":"

48 << ( minute < 10 ? "0" : "" ) << minute;

49}

50

51// Print time in standard format

52void Time::printStandard()

53{

54cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 )

55 << ":" << ( minute < 10 ? "0" : "" ) << minute

56 << ":" << ( second < 10 ? "0" : "" ) << second

57 << ( hour < 12 ? " AM" : " PM" );

58}

2. Create a source code file2.1 Load the header file to get the class definitions2.2. Define the member functions of the class


  • Login