Lec07 (Topic 4 Define Classes)
Lec07 (Topic 4 Define Classes)
LECTURE 7
Defining Classes (Part 1)
2
Procedural Programming
• Procedural Programming is associated with top-down design.
Main Program
Data
Main program coordinates calls
to procedures and hands over
appropriate data as parameters.
Procedure 1 Procedure 2 Procedure 3
3
• The idea is to design a program to solve a problem by combining both the data
and procedures (member functions) that operate on that data into a singe unit
called object.
• C++ and Java are examples of OOP languages.
5
Object-Oriented Programming
• In OOP, object's methods (member functions) provide the only way to access its
data.
• The data can be set hidden inside an object, and therefore safe from direct
alteration from outside the class.
What is an “Object”?
• An object is a computer representation of real-world person, place,
event or thing.
7
What is an “Object”?
• An object can be characterized by a set of attributes, and a set of behaviors that
act upon those attributes.
• We refer to a group of similar objects (with the same attributes and behaviors) as a
class.
8
What is an “Object”?
Example: Class
Object/Class: Student
Attributes: Student
1. Name Attributes
2. Student ID 1. name
3. Courses
2. student_ID
Behaviors/Methods:
…
4. Do homework Methods
5. Register course 1. do_homework()
6. Withdraw …
9
Categorizing Objects
• The two most common ways for us to categorize objects are:
• For example, we might group together in the category “car” those objects that
have same:
• Attributes (color, speed, model, etc), and
• Behaviors (drive, stop, and turn)
• We refer to a group of similar objects (with the same attributes and behaviors)
as a class.
Class: human
Categorizing by Composition
• When categorizing objects by composition, an attribute of a particular class is a
class by itself.
Personnel Dept.
Personnel Data
Object Behaviors
• Categories of behaviors that an object can have:
Object Behaviors
• For the Car class, we have identified 3 behaviors: drive, stop and turn.
• Class diagrams are widely used to describe the types of objects in a system and
their relationships.
17
Class Diagram
Class diagram contains the following elements:
Class Diagram
• The name of the class appears in the
top section.
Car
• The attributes in the middle section. Class
model
• The behaviors in the bottom section.
Attributes color
speed
• Note that the class diagram here is
incomplete as it does not state access
privilege of the members (discuss Behaviors drive()
later) stop()
turn()
19
Class Diagram
Car Wheel
Association model
model
color width
speed
1 n
wheel
change ()
drive ()
stop () More than 1
turn ()
4 Principles of OOP
Abstraction VS Encapsulation
22
Abstraction in C++
• Abstraction is the process of separating the logical properties from the
implementation details.
• We have an abstract view of what the engine does, but are not interested in
the engine’s actual details implementation.
• In C++, object classes are created using the class data type.
}; methods
24
Car
- model:string
class Car { - color:string
string model; - speed:int
string color;
int speed;
};
25
Encapsulation
• To reduce the need for outside users to worry about how something is done.
Encapsulation
Encapsulation in C++
private :
• If a class’s members are declared as private, then they are not
accessible to anything except for the object itself.
• Thus, the only place where the scope of a private member would be
valid is within the object’s behaviors.
Encapsulation in C++
public:
• Members that are declared as public are the exact opposite –they are
fully accessible to the outside world.
• Any public member can be accessed by any other C++ object (assuming
that the scope is valid).
Encapsulation in C++
• If we leave the class declaration as it is, all of the members will be private
(default).
class Car {
class Car { private:
string model; string model;
Same
string color; string color;
int speed; int speed;
}; };
• If we want all of the members to be public, then we must use the public
keyword.
class Car {
public:
string model; All public
string color;
int speed;
};
30
Encapsulation in C++
• We can “mix-and-match” keywords in order to obtain different combinations.
class Car {
class Car { public:
string model; public string model;
public: string color;
string color; private:
int speed; int speed;
}; };
31
• Instances of an object class can execute all of the methods that their class
defines.
• Just as with any function, the first step in writing a method is to determine
it’s interface/header.
• The interface is simply the name of the method, the parameters it requires,
and its return value.
C++ Method Declaration
• Consider the car class’s first behavior: drive.
• For argument’s sake, let’s assume that it takes as parameters two integers,
speed and distance, and returns nothing.
Car
- Model:string
- Color:string
- Speed:int
32
33
• Users do not have to worry about the details (consider how long and
difficult-to-read if a class declaration includes 20 methods, each with
definitions hundreds of lines long).
• A class’s methods have access privileges in the same way that attributes do.
class Car
{ Car
public:
void drive (int speed,
- Model:string
int distance);
- Color:string
void stop( );
void turn( );
- Speed:int
private:
string model; + drive (speed:int, distance:int): void
string color; + stop (): void
int speed; + turn (): void
};
36
Commenting
• It is becoming a convention to place a comment at each declaration
(member or method) to describe its purpose.
• For data members, describe what the member represents and any
restrictions that exist on its value(s)
Commenting
• For data methods, describe what the method does, what parameters (if any)
it takes, and what its possible return values (if any) are.
• Parameters should be described in the same way that data members are.
// Method: drive
// Causes the car to drive forward at the specified speed
// for the specified distance.
//
// Parameters:
// speed – the speed of the car (in miles per hour)
// distance – the distance that the car should go (in miles)
//
// Return value:
// Returns 0 if successful, -1 otherwise.
• By convention, we use the name of the header file with an underscore (‘_’) in place
of the period.
Car class => CAR_H
• This gives us pretty good odds that we won’t accidentally use a name that already
exists.
• The first time the header file is included, the name is not defined and the compiler
enters the block, defining the name and parsing and compiling the header code.
40
• In the source file, you must include the header file containing the class
declaration.
• Thus, our initial “car.cpp” file might look something like this:
• Thus, to define our method drive( ) for the car class, we would use the
following structure:
• Thus, any data member of a particular class can be accessed from within
that class’s methods.
class Foo {
public: void Foo::bar() {
void bar(); x = 5;
private: };
int x; // data member
};
x here is data member
44
void Foo::bar( )
{
int x;
x = 5; // Sets local variable x to 5
this -> x = 5; // Sets the member Foo::x to 5
Foo::x = 5; // Same result as previous line
};