Class is a user defined data type. classes helps to achieve Data abstraction and encapsulation. Instead of holding only data, it can hold both data and functions. An object is an instantiation of a class. In terms of variables, a class would be the type, and an object would be the variable.
Classes are generally declared using the keyword class, with the following format:
class class_Name
{
Access_specifier_1:
Member1;
Access_specifier_2:
Member2;
};
Where class_Name is a valid identifier for the class. The body of the declaration can contain members, that can be either data or function declarations, and optionally access specifiers.
An access specifier is one of the following three keywords: private, public or protected. These specifiers modify the access rights that the members following them.
- private members of a class are accessible only from within other members of the same class or from their friends.
- protected members are accessible from members of their same class and from their friends, but also from members of their derived classes.
- Finally, public members are accessible from anywhere where the object is visible.
By default, all members of a class declared with the class keyword have private access for all its members. Therefore, any member that is declared before one other class specifier automatically has private access.
Example:
#include
using std::cout;
class HelloWorld
{
int x;
public:
HelloWorld():x(10){}
void Print(){
cout<<”Hello World ! Value of x is “<}
};
int main()
{
HelloWorld Obj;
Obj. Print();
}
Here HelloWorld is the name of the class. Since the default access specifier is private , the integer variable x has Here private access. That means it cannot be accessed outside the class.
HelloWorld() is the name of the constructor. Since it is under public access specifier, the constructor has the public access. The constructor must have the same name as the class and it should not return any value. The constructor must have the public access in order to create the object of that class type. We can see about constructors in details in forthcoming sessions.
Also note that the member x is initialized with value 10 in the member initialization list of the constructor. We can see about the initialization list in forthcoming posts.
And we have an public member function called void Print() which takes and returns nothing, Also prints the message with value of x.
In main function we are declaring an object of type HelloWorld. Here obj is the variable name and HelloWorld is the abstract data type name.
Now we are calling the member function Print() on object obj. Since Print() is the public member function, it can be accessed from outside the class.
save the above file as HelloWorldClass.cpp
compile the above program using GNU g++ compiler (Linux).
g++ HelloWorldClass.cpp -o HelloWorldClass
The output file name will be HelloWorldClass . Run the file using ./HelloWorldClass
You can see the output below
Hello World ! Value of x is 10
Things to note when coding the classes
- The class declaration must end with a semi-colon.
- C++ is case sensitive class != Class
- class's constructor function must have the same name as the class, and no return type (not even void).
- Member initialization list can be used only with constructors.
Happy Coding :)
No comments:
Post a Comment