Class and Object in C++:
Class:
- Class is used to hold data members and member function i.e. variables and functions.
- It can be easily accessed anywhere and used to reduce the reusability of the code.
For example,
- Let’s take student,
- Student properties are all called as variables
and student performance or student-related calculations(i.e. student result
based on total and it may be pass or fail) are called as a function.
Where,
The student is a class.
Student properties (variables)
: Roll_num,name,age,ect.
Student behaviours(function) :
total,average,result,fees details ect.
Note:
- If one or more variables declared within the class, called data members.
- If one or more functions declared in within the class, called member function.
Object:
- An object is used to access the members in the class.
- It simply said as the short name of the class.
Syntax:
Note:
The class name and object name is a user-defined word.
Access specifiers:
- C++ access specifiers are used for determining or setting the boundary for the availability of class members (data members and member functions) beyond that class.
Types of access specifiers:
- Private
– It is used to access the data member and member function within the classes.
- Public – It is used to
access the data member and member function out of the class. That means any
class can be accessible.
- Protected – It members cannot be accessed from outside the class, however, they can be accessed in inherited classes.
Note:
- More details are given in the inheritance concept. Please click the below link to aid for more learning.
Example:
If you need details about iomanip.h header file and
setw() function, click the below link to learn.
No comments:
Post a Comment