Class and Objects
Class and Object, Access Modifiers, Constructors
Procedural Programming
Procedural programming focuses on the process/actions that occur in
a program. The program starts at the beginning, does something, and
ends.
Limitations of Procedural Programming
• If the data structures change, many functions must also be changed
• Programs that are based on complex function hierarchies are:
• difficult to understand and maintain
• difficult to modify and extend
• easy to break
Object-Oriented Programming
• Objected-Oriented Programming is computer programming model
that organizes software design around data or Objects(Data fields
that have attributes and behaviors)
• Object-oriented programming (OOPs) makes development and
maintenance easier
• OOPs provides data hiding (Abstraction)
• OOPs provides the ability to simulate real-world event much more
effectively
Classes and Objects
• Classes
• Classes are the standard unit of programming
• Model objects that have attributes (data members) and behaviors (member
functions)
• Defined using keyword class
• Have a body delineated with braces ({ and })
• Class definitions terminate with a semicolon
• A class is like a blueprint – reusable
• For example, a house is an instance of a “blueprint class”
Classes and Objects
• Objects
• Object is a run time entity.
• Objects are instantiated (created)
from the class
• Memory is allocated when object is
created
• attributes: members of a class
• methods or behaviors: member
functions of a class
Class definition
class Class_name {
public:
constructor and destructor
member functions
private:
data members
};
Member access specifiers
• Classes can limit the access to their member functions and
data
• The three types of access a class can grant are:
• Public — Accessible wherever the program has access to an object
of the class
• private — Accessible only to member functions of the class
• Protected — Accessible form Child class members only
Objects
• Class definition and declaration
• Once a class has been defined, it can be used as a type in object, array
and pointer declarations
• Example:
Classname objname, //Object of Type Classs
ArrayofClass[8], // Array of Type Class
*pointofClass, //Pointer to Class Object
&referenceofClass; //Reference to Class Object
• Accessing class members
• Dot (.) for objects and arrow (->) for pointers
• Example:
• t.hour is the hour element of t
• TimePtr->hour is the hour element
Initializing Class Objects: Constructors
• Constructor : Special Function that
• Initialize class members
• Same name as the class
• No return type
• Is executed / called at the time of object creation