C++ Programming:
From Problem Analysis
to Program Design
Classes and Data Abstraction
Objectives
In this ppt, you will:
• Learn about classes
• Learn about private, protected, and
public members of a class
• Explore how classes are implemented
• Examine constructors and destructors
• Learn about the abstract data type (ADT)
2
Objectives (continued)
• Explore how classes are used to implement
ADTs
• Learn about information hiding
• Explore how information hiding is implemented
in C++
• Learn about the static members of a class
3
Classes
• Class: collection of a fixed number of
components (members)
• Definition syntax:
− Defines a data type; no memory is allocated
− Don’t forget the semicolon after closing brace
4
Classes (continued)
• Class member can be a variable or a function
• If a member of a class is a variable
− It is declared like any other variable
• In the definition of the class
− You cannot initialize a variable when you
declare it
• If a member of a class is a function
− Function prototype is listed
− Function members can (directly) access any
member of the class
5
Classes (continued)
• Three categories of class members
− private (default)
• Member cannot be accessed outside the class
− Public
• Member is accessible outside the class
− Protected //will be discussed later
class A {
Any member (variable or
int x; function) can be public or
private or protected
void fun();
};
6
Classes (continued)
These functions cannot
modify the member variables
of a variable of type
clockType
const: formal parameter can’t modify
the value of the actual parameter
private members,
can’t be accessed from outside the class
7
Unified Modeling Language Class
Diagrams
To describe the class graphically
+: member is public
-: member is private
#: member is protected
8
Variable (Object) Declaration
• Once a class is defined, you can declare
variables of that type
clockType myClock;
clockType yourClock;
• A class variable is called a class object or
class instance
OOP stands for Object Oriented
Programming: encapsulates data
and functions into packages called
classes.
9
Accessing Class Members
• Once an object is declared, it can access the
public members of the class
• Syntax:
− The dot (.) is the member access operator
• If object is declared in the definition of a
member function of the class, it can access
the public and private members
10
Member functions
Member functions can be defined inside or outside the class.
But they must be declared inside the class body.
Syntax of writing definition outside the class body:
Return_type Class_name :: name_of_ function()
{
…….
}
Accessing Member Functions
Member functions can be accessed only through the object of the same class with
the help of “.” (dot) operator.
Calling of the function will be from Main().
Ex: object.func();
5 10
Built-in Operations on Classes
• Most of C++’s built-in operations do not apply
to classes
− Arithmetic operators cannot be used on class
objects unless the operators are overloaded
(like +, * etc)
− You cannot use relational operators to
compare two class objects for equality (like
< , > etc)
• Built-in operations valid for class objects:
− Member access (.)
− Assignment (=) 15
Assignment Operator and Classes
16
Class Scope
• An object can be automatic or static
• A member of the class is local to the class
• You access a class member outside the
class by using the class object name and
the member access operator (.)
17
Functions and Classes
• Objects can be passed as parameters to
functions and returned as function values
• As parameters to functions
− Objects can be passed by value or by
reference
• If an object is passed by value
− Contents of data members of the actual
parameter are copied into the corresponding
data members of the formal parameter
18
Reference Parameters and Class
Objects (Variables)
• Passing by value might require a large
amount of storage space and a considerable
amount of computer time to copy the value of
the actual parameter into the formal
parameter
• If a variable is passed by reference
− The formal parameter receives only the
address of the actual parameter
19
Reference Parameters and Class
Objects (Variables) (continued)
• Pass by reference is an efficient way to pass a
variable as a parameter
− Problem: when passing by reference, the actual
parameter changes when formal parameter
changes
− Solution: use const in the formal parameter
declaration
20
Example:
Implementation of Member
Functions
Scope resolution operator
22
Implementation of Member
Functions (continued)
23
if hr= 23 then hr= 0
if hr= 23 min=59 then
min =0 hr++
hr=0 min=0
if hr= 23 min= 59 sec = 59 then by sec++
sec=0 min++
min=0 hr++
hr= 0 min=0 sec=0
Implementation of Member
Functions (continued)
• Once a class is properly defined and
implemented, it can be used in a program
− A program that uses/manipulates the objects
of a class is called a client of that class
• When you declare objects of the class
clockType, every object has its own copy of
the member variables (hr, min, and sec)
• Variables such as hr, min, and sec are
called instance variables of the class
− Every object has its own instance of the data
27
Accessor and Mutator Functions
• Accessor function:
member function that only accesses the value(s)
of member variable(s)
• Mutator function:
− member function that modifies the value(s) of
member variable(s)
• Constant function:
Member function that cannot modify
member variables
Use const in function heading
28
Order of public and private
Members of a Class
• C++ has no fixed order in which you declare
public and private members
• By default, all members of a class are
private
• Use the member access specifier public to
make a member available for public access
29
Order of public and private
Members of a Class (continued)
30
Order of public and private
Members of a Class (continued)
31
Order of public and private
Members of a Class (continued)
32
Constructors
• Use constructors to guarantee that data
members of a class are initialized
• Two types of constructors:
− With parameters
− Without parameters (default constructor)
• The name of a constructor is the same as the
name of the class
• A constructor has no type
33
Constructors
-Constructor is the member function of the class.
-It has same name as the class name.
-It must be define in the public section of the
class.
-It is implicitly called by the compiler at the time
of object creation (from main()), if not
declare/defined in code.
- It does not have return type.
- It can be defined outside the class.
Types of Constructors
Default constructor:
classname();
Parameterized constructor:
classname(arguments);
Copy constructor:
classname(arg. Of type class);
Syntax for Constructors
Declaration Syntax :
classname();
Invoking a Constructor
• A constructor is automatically executed when
a class variable is declared
37
Invoking the Default Constructor
• To invoke the default constructor:
• Example:
clockType yourClock;
38
Invoking a Constructor with
Parameters
• Syntax:
• The number of arguments and their type
should match the formal parameters (in the
order given) of one of the constructors
− Otherwise, C++ uses type conversion and
looks for the best match
− Any ambiguity leads to a compile-time error
39
Constructors (continued)
• A class can have more than one constructor
− Each must have a different formal parameter
list
• Constructors execute automatically when a
class object enters its scope
− They cannot be called like other functions
− Which constructor executes depends on the
types of values passed to the class object
when the class object is declared
41
Constructors (continued)
43
Can be replaced with:
setTime(hours, minutes, seconds);
Constructors and Default
Parameters
• If you replace the constructors of clockType
with the constructor in Line 1, you can declare
clockType objects with zero, one, two, or
three arguments as follows:
clockType clock1; //Line 2
clockType clock2(5); //Line 3
clockType clock3(12, 30); //Line 4
clockType clock4(7, 34, 18); //Line 5
45
Classes and Constructors:
A Precaution
• If a class has no constructor(s), C++ provides
the default constructor
− However, object declared is still uninitialized
• If a class includes constructor(s) with
parameter(s), but not the default constructor
− C++ does not provide the default constructor
46
Note: if you have a constructor with all default parameters you cannot declare a
constructor without parameters, they both called default constructor.
clockType clockType(int = 0, int = 0, int = 0);
clockType clockType (); //illegal in the same class
Examples
class A { class A { class A {
private: private: private:
int x; int x; int x;
public: public: public:
A (int =0); A (int =0); A (int);
}; A(); };
};
void main()
void main() void main() {
{ {
A obj1; //illegal
A obj1; //illegal
A obj1();//Not Correct?!
} }
A obj1; //Correct
}
Thank you