Sie sind auf Seite 1von 5

4/3/2013

16

17

Object-Oriented Programming Paradigm


Use object as the key abstraction for designing

Data Abstraction
The separation of the logical properties of a data

computer programs Features


Data abstraction Encapsulation Messaging (interactions between objects) Modularity Polymorphism Inheritance

type from its implementation details


Higher level view of data

18

19

Abstract Data Types (ADTs)


Why ADTs? Enable analysis before coding Focus on logical view A high-level specification Implementation independent A set of organized data objects A set of operations Reason about feasibility and efficiency

ADT Example
TYPE Time DOMAIN Each Time value is a time in hours, minutes, and seconds. OPERATIONS Set the time Print the time Increment by one second Compare 2 times for equality Determine if one time is less than another

20

21

Another ExampleThe Stack ADT


Data elements are organized as a stack Insertions and deletions can only be performed at

Primary Operations on a Stack


Insert an element Check if the stack is full Remove an element
Check if the stack is empty

one end (top)


Cannot insert or remove at an index x

Last in first out retrieval


top x

The top position The size

4/3/2013

22

23

The Stack ADT


Convey the picture of a stack data structure How data are organized What operations are possible Visualize the computations

C++ Classes
A class is a structured type in C++ to implement

an abstract data type (ADT)

24

25

C++ Classes
Syntax:
class ClassName { private: DataMemberList; MethodMemberList1; public: MethodMemberList2; };

Private or Public Members


Private members of a class are only accessible

within the class


Public members of a class are accessible by the

clients

class, private, and public are key words

26

27

Implement the Time class in C++


class { Time
// Declares a class data type // does not allocate memory // 5 public member functions

Use of const with Member Functions


When a member function does not modify the private

public : void void void bool bool private : int int int }; hrs; mins; secs;

data members, use const in both the function prototype and the heading of the function definition
secs);

Set (int hours , int Increment (); Write () const;

mins , int

//const: does not modify the value of data members

Equal (Time otherTime) const; LessThan (Time otherTime) const;


// 3 private data members

26

4/3/2013

28

29

Store Student Records in Class


class Student

C++ Classes
A class usually has two types of members Data fields (member variables) Methods (member functions) Data fields should always be private Methods can be private or public

// student is a class name (an ADT)

{ //begin of class private: //only accessible within this class int id; string firstName; string lastName; Member variables char gender; string major; float gpa; public: //accessible outside of this class int GetID ( ) { ... } void SetID ( int id ) Member { ... } ... }; //end of class

(similar to a struct)

functions

30

31

Class Objects
Facilitates re-use of C++ code for an ADT Software that uses the class is called a client Variables of the class type are called objects or

class type Declaration


The class declaration creates a data type and

names the members of the class


It does not allocate memory for any variables

instances Client code uses classs public member functions to manipulate class objects

of that type Client code still needs to declare class variables

32

33

For Example
class Student // Student is a new data type! { private: int id; string firstName; ... }; Student stu1;
// instantiate an object stu1 in Student class // stu1 is an object (instance) of the Student class.

Exercise 1
Construct a class named Rectangle that has data

members named length and width in float type


The class should have member methods named

Perimeter() and Area() to calculate a rectangles perimeter and area, and a member method named SetSize() to set a rectangles length and width, and a member method named ShowData() that displays a rectangles length, width, perimeter, and area

4/3/2013

34

35

Exercise 2
Define a class called Employee Attributes? Methods?

class Employee { private: int id; string name; float payRate; float hours; float wages; public: void SetEmp(int idnum, string fullname, float rate, float hr) { id = idnum; .. } void CalcSalary() { wages = payRate * hours; } void PrintPayCheck () { cout << Employee ID: << id << endl << Name: << name << .; } };

Employee Class

36

37

Aggregate class Operations


Assignment to another class variable using (=) Student stu1, stu2; //instantiate 2 objects of Student class stu1.set(1, john, 78); //set the initial values of object S stu2 = stu1; //member-by-member assignment of stu1 to stu2 Can be passed to a function as an argument by value or by reference

Some Objects We Used


The member selection operator (.) selects either data members or

function members
Header files iostream and fstream declare the istream, ostream,and

ifstream, ofstream I/O classes


Both cin and cout are class objects, for example

cin.get (someChar);
The following statements declare myInfile as an instance of class

ifstream and invoke function member called open ifstream myInfile; myInfile.open (mydata.dat);

38

41

Information Hiding
Information hiding - Class implementation details are

Access Data Fields within a Class


class Student { private: long id; string firstName; string lastName; char gender; string major; float gpa; public: void PrintStudentRecord () { cout << "Name : " << firstName << "Gender: " << gender << "Major : " << major << "GPA : " << gpa } ... };

hidden from the clients view


Public functions of a class provide the interface between

the client code and the class objects

client code

specification

implementation

<< << << <<

" " << lastName endl endl endl;

<< endl

4/3/2013

42

43

Method Calls
void main() { Student stu1; ... stu1.PrintStudentRecord(); ... return 0; }

Access private data in a class


class Student { private: long id; string firstName; string lastName; char gender; string major; float gpa; ... };
int main() { Student stu1; ... cout << stu1.id; ... }

Das könnte Ihnen auch gefallen