2.classes and Objects
2.classes and Objects
A class is defined in C++ using keyword class followed by the name of class. The
body of class is defined inside the curly brackets and terminated by a semicolon at
the end.
Defining Class and Declaring Objects
Declaring Objects: When a class is defined, only the specification for the object
is defined; no memory or storage is allocated.
To use the data and access functions defined in the class, you need to create
objects.
Syntax: ClassName ObjectName
Accessing Data Members and Member Functions
The data members and member functions of class can be accessed using the
dot(‘.’) operator with the object.
For example if the name of object is obj and you want to access the member
function with the name printName() then you will have to write obj.printName() .
Accessing a data member depends solely on the access control of that data
member.
There are 3 types of access modifiers available in C++: Public, Private,
Protected
Defining Class and Declaring Objects
Defining Class and Declaring Objects
Member Functions in Classes