Sie sind auf Seite 1von 16

User-Defined

Type_Classes

C++ Programming: An Object-Oriented Approach

Behrouz A. Forouzan
Richard F. Gilberg
Types and Instances in Real Life

• A type is an abstraction, an instance of that type is a concrete entity.


• Example, the word person is a type, and that John, Sue, and Michelle
are instances of the type person.
• The relationship between a type and its instances is a one-to-many
relation.
Type and Instances
Attributes and Behaviors
• Attribute is any characteristic that interests us in an instance.
• For example, if the instance is an employee, we may be interested
only in the employee’s name, address, position, and salary.
• Behavior is is an operation that an instance can perform on itself.
• For example, if an instance is an employee, we assume that she can
give her name, her address, and her salary.
Classes and Objects in Programs
• A user-defined type can be created using a construct named class.
• An instance of a class is referred to as an object.
• Attributes and behaviors of an object are represented as data
members and member functions.
Data Members and Member Functions
• A data member of an object is a variable whose value represents an
attribute.
• For example, the radius of a circle object can be represented by the
value of a variable of type double.
• A member function is a function that simulates one of the behaviors
of an object.
• For example, we can write a function that allows a circle to give its
radius, its area, and its perimeter.
Prosedural versus Object Oriented
• In procedural programming, we need only write an application
program (the main function and some other functions) to use objects
of built-in types.
• In object-oriented programming we need three sections: the class
definition, the member function definition, and the application.
Three sections of object-oriented paradigm
Class Definition
• A class definition is made of three parts: a header, a body, and a
semicolon.
• A class header is made of the reserved word class followed by the
name given by the designer.
• The class body is a block that holds the declaration of data members
and member functions in the block.
• A semicolon terminates the definition.
Example
Declaring Data Members
• The data members of a class simulate the attributes of the objects
that are instantiated from the class.
• We may have several attributes, some of which are dependent on the
others and can be calculated given the other attributes.
• Among the dependent attributes, we need to select the simplest and
the most basic ones.
Declaring Member Functions
• Class definition also declares all functions that are used to simulate
the behavior of the class.
• Some functions have the const qualifier at the end and some do not.
• Those that change something in the object cannot use this qualifier;
those that are not allowed to change anything need this qualifier.
Access Modifiers
• Determines how a class can be accessed.
• When a member is private, it can only be accessed inside the class
(through member functions).
• When a member is public, it can be accessed from anywhere (inside
the same class, inside the subclasses, and in the application).
Access Modifiers for Data Members
• The modifiers for data members are normally set to private for
emphasis.
• This means that the data members are not accessible directly.
• They must be accessed through the member functions.
Access Modifiers for Member Functions
• To operate on the data members, the application must use member
functions, which means that the declaration of member functions
usually must be set to public.
• Sometimes the modifier of a member function must be set to private,
such as when a member function must help other member functions.
Group Modifier Access
• We have collected all data members under one group and have used
the private keyword followed by a colon to say that all of the
keywords are private.
• We have also grouped all member functions and used one public
keyword followed by a colon to say that all of them are public.

Das könnte Ihnen auch gefallen