0% found this document useful (0 votes)
12 views32 pages

Class1 Session 12

Uploaded by

ermiasnigussie54
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views32 pages

Class1 Session 12

Uploaded by

ermiasnigussie54
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Encapsulation, Data Hiding and

Static Data Members


Encapsulation in C++
• Bind the Data and Code together

• Classes are provided in C++ to bind data and code


together
• View everything in world as objects

• Generalization of objects is classes

• Characteristics / Properties are data members and


operations that can be performed by the objects /
on the objects are member functions
What are Classes?
• Stroustrup initially named classes as structures
with functions
• In C++, structures can have functions
Time Problem
In a online examination system, each test will be
scheduled for ‘x’ minutes. The student is free to
take up the test on his convenience but once he
starts the test, he must complete. Given the start
time and the value of ‘x’ for an examination,
develop an algorithm and write a ‘C++’ code for
the examination system to calculate the finish
time of the test.
Structural Programming for Time Problem

Its also possible to write a purely procedural


program in C++

Time problem shall be solved using purely


structural programming in C++
Structures in C++
• Can have functions in it
• Solution for time problem with functions in
structure
Classes in C++
• Similar to structures, only access specifier
changes
• Data hiding is achieved by using access
specifiers
• By default all members of structures in C++
has public access whereas members of classes
have private access by default
Access Specifiers in C++
• Three
– Public
– Private
– Protected
Public Access Specifier is used in the program
Definition of member functions are similar to
structures
Constant member functions
• A function becomes const when const keyword
is used at the end of function’s declaration
• The idea of const functions is not allow them
to modify the object on which they are called.
• It is recommended practice to make as many
functions const as possible so that accidental
changes to objects are avoided.
Constant member functions
• Objects can also be constants

• Constant functions can be accessed by both


constant and non-const member functions
Static Data Members
• Class members static using static keyword

• Shared by all objects of the class

• Shall be initialized outside the class using scope


resolution operator
• Static data members can be referred to without
referring to an object of class type but by using class
name and scope resolution operator
Each object has a
copy of normal
variables whereas
static variables are
shared among the
objects

In figure,
Variable1,
Variable2 etc are
normal variables
Static Member Functions
• can be called even if no objects of the class
exist and thestatic functions are accessed
using only the class name and the scope
resolution operator ::.
• can only access static data member, other
static member functions and any other
functions from outside the class.

You might also like