Class and Object
Class and Object
Class in C++
Classes are fundamental to object-oriented programming (OOP).
They serve as blueprints for creating objects. Classes define data
members (attributes) and member functions (methods) that
objects will inherit.
What is Class and its
Types
A class is a user-defined data type that acts as a template for creating
objects. Common class types include abstract classes, concrete classes,
and template classes, each serving a specific purpose in program design.
3
A template class allows you to define a generic class that can work
with different data types, promoting code reusability and flexibility.
Examples of
Class
Let's look at an example of a class representing a 'Person'. It defines attributes like name, age, and methods like
introducing oneself.
Blueprint (Class)
The class 'Person' acts as the blueprint for creating objects.
Instance (Object)
Each person object (like 'John', 'Jane', etc.) is a specific instance
of the 'Person' class.
Functionality
Each object has access to the methods defined in the class,
allowing it to perform actions specific to its type.
What is Object
An object is a real-world entity that has a state and behavior. In C+
+, objects are created from classes, inheriting the data members
and member functions defined in the class blueprint. They are like
unique instances of the class, possessing specific values for the
data members.
State Represents the data
members (attributes) of the
object, holding information
about its current condition.
John
The object 'John' has the name "John" and age 25.
Jane
The object 'Jane' has the name "Jane" and age 30.
Emily
The object 'Emily' has the name "Emily" and age 22.
Functions of
Class
Functions associated with a class are known as member functions. They
define the behavior and functionality of objects created from that class.
Member functions operate on the data members of the class and can be
accessed using the object's name.
Other Member
Functions
Functions that perform operations or tasks specific to the class, not
just accessing or modifying data members.
Functions of
Object
Objects interact with their member functions. When a member function is
called on an object, it operates on that specific object's data members. The
object's state is affected by the function's actions.
Function Call
1
A member function is called using the object name followed by
the function name, e.g. 'personObject.setName("Alice");'
Data Access
2
The function accesses and potentially modifies the data
members associated with the specific object it was called on.
State Change
3
The function's actions may change the object's internal data,
updating its state.