003 CreateYourFirstClass
003 CreateYourFirstClass
Programming with
C++
Create Your First Class
Objects and Classes
• Classes: Where Objects Come from
• A class is code that describes a particular type of object. It specifics
the data that an object can hold (the object’s fields), and the actions
that an object can perform (the object’s methods)
• You can think of a class as a code “blueprint” that can be used to
create a particular type of object.
• When a program is running, it can use the class to create, in
memory, as many objects of a specific type as needed.
• Each object that is created from a class is called an instance of
the class.
2
Objects and Classes
Classes in College Management Program
3
Objects and Classes
= Ayham
= 1005
= 1/3/2003
= Amman
= 3.5
=2 Student 1 : Ayham
= Tala
Three Objects,
= 1032
= 5/9/2002
each object is
an instance
= Amman
=4
=3 Student 2 : Tala
from “Student”
class
= Rayan
= 1632
= 5/8/2004
= Amman
Class
= 3.7
=1 Student 3 : Rayan
4
Writing a Class, Step by Step
• A Rectangle object will have the following fields and methods
fields
methods
5
My First C++ Class
class Rectangle
{
};
6
My First C++ Class
class Rectangle
{
private:
float length;
float width;
};
7
My First C++ Class
class Rectangle
{
private: Access Modifier
float length;
float width;
};
8
My First C++ Class
Access Method
specifier name
Return
type
public:
void setLength(float len)
11
My First C++ Class
class Rectangle
{
private:
float length;
float width;
public:
void setLength(float len)
{
if (len >= 0)
length = len;
else
cout << “Error, please enter positive value”;
}
};
12
Creating Rectangle Object
13
Creating Rectangle Object
When create the Rectangle class, the Rectangle become a new
type in C++. In C++, classes are considered as new (custom) types
that can be added to existing types in C++ language.
Variable name
14
Creating Rectangle Object
A Rectangle object
box
The box variable length: 0.0
holds the address address
of the Rectangle width: 0.0
object.
memory space
15
Creating Rectangle Object
The methods
that appear in
the popup
menu are the
public methods
that belong to
Rectangle class.
16
Creating Rectangle Object
17
More Examples
18
More Examples
(-) indicates “private”: in general all attributes should be “private”
(+) indicates “public”: in general, all methods should be “public”
“private” attributes
“public” methods
19
More Examples
Setter, Mutator
Getter, Accessor
20
Separating class code into two
files
• The class code can be separated into two files:
1. Header file (.h)
2. Implementation file (.cpp)
21
Separating class code into two
files
• The class code can be separated into two files:
1. Header file (.h)
1. Contains the declaration of all the class members.
2. Only attributes declaration and methods prototypes.
2. Implementation file (.cpp)
22
Separating class code into two
files
Rectangle.h
• The class code can be separated
into two files:
1. Header file (.h)
1. Contains the declaration of
all the class members.
2. Only attributes declaration
and methods prototypes.
2. Implementation file (.cpp)
23
Separating class code into two
files Rectangle.cpp
24
Separating class code into two
files
• The class code can be separated into two files:
1. Header file (.h)
1. Contains the declaration of all the class members.
2. Only attributes declaration and methods prototypes.
2. Implementation file (.cpp)
• Contains the implementation of the class methods.
25
Separating class code into two
files
main.cpp
• The class code can be
separated into two files:
1. Header file (.h)
2. Implementation file (.cpp)
• Client code
• Client code, is the one
that includes the main
function. This file should
be stored by the name
“main.cpp”.
26
Separating class code into two
files main.cpp
Rectangle.cpp
Rectangle.h
output
27
Why Separating class code into
two files
• The class code can be separated into two files: Header file
(.h) and Implementation file (.cpp)
• Advantages:
1. Hide your implementation from other developers.
2. More organized
28
Review (main points)
• Classes and objects are the two main aspects of object-
oriented programming.
• A class is a template for objects, and an object is an
instance of a class.
Banana
Fruit
Mango
Car
objects
objects 29
Review (main points)
• Classes and objects are the two main aspects of object-
oriented programming.
• A class is a template for objects, and an object is an
instance of a class.
• When the individual objects are created, they take all the
variables and functions from the class.
30
Review (main points)
• Protection Levels:
• When we design a class we can control how member functions and data of that class can be
accessed.
• Each member of the class is assigned a protection level:
• Private Members : Object data and functions that can be accessed only from within
member functions of the object.
• Public Members : Object data and functions that can be accessed from both outside and
inside the member functions of the object.
• The primary purpose of protection levels is to allow programmers to provide an unchanging
public interface for a class while being able to modify the way the class is implemented without
breaking code that uses the class.
• This is a mechanism for information hiding.
31
Review (main points)
• Protection Levels (cont.):
• Reasons for data hiding (private members):
• Makes interface simpler for user.
• Principle of least privileged (need-to-know)
• More secure. Less chance for misuse (accidental or malicious).
• Class implementation easy to change without affecting other modules that use it.
• If protection levels not given, members default to private.
32