Class In C++





Class in c++

Class is a group of similar object. It is a collection of variables, often of different types and its associated functions. Hence, class just binds data and its associated functions under one unit there by enforcing encapsulation. Encapsulation means wrapping up data and its associated functions together into single unit. A key feature of object oriented programming is data hiding. It means that the data is hidden so that it can’t be accessed mistakenly by functions outside the class.

Objects represent instance of a class. Objects are basic run time entities in an object oriented system. In C++, a class variable is known as an object.

Need for class
Classes are needed to represent real word entities that not only have Data_type properties but also associated operations.

Definition of class

Class Class_name
{
Private:
Variable declarations;
Function declarations;
Public:
Variable declarations;
Function declarations;
};

Where, the keyword class specifies that it is a class; Class_name is the name of the class.

The class body contains the declarations of its members (data & functions). Private and public is the access specifier. The body of class is enclosed within brackets and terminated by semicolon.

[Note: the default access specifier is private.]

Example

#include<iostream.h>
#include<conio.h>
Class student
{
Private:
Int x, y, z;
Public:
Void output ()
{
Cout<<” \n enter the value of X & Y:”;
Cin>>X>>Y;
Z=X+Y;
Cout<<”\n Total=”<<Z;
}
};
Void main ()
{
Sum a;
a. Output ();
getch ();
}

 

Creating object / defining the object of a class

The general syntax of defining the object of a class is:-

Class_name object_name;

E.g. Student stu;

 

The above statement will create object “stu” of class “student”.

In C++, a class variable is known as an object. The declaration of an object is similar to that of a variable of any data type. The members of a class are accessed or referenced using object of a class.

Accessing / calling member of a class

All member of a class are private by default. Private member can be accessed only by the function of the class itself.
Public member of a class can be accessed through any object of the class. They are accessed or called using object of that class with the help of dot operator (.).

The general syntax for accessing data member of a class is:-

Object_name.Data_member=value;

The general syntax for accessing member function of a class is:-

Object_name. Function_name (actual arguments);

A program accessing the public member (data and function) using object of a class.

#include<iostream.h>
#include<conio.h>
Class sample
{
Public:
Int a, b, c;
Void sum ()
{
C=a+b;
Cout<<”\n sum :”<< c;
}
};
Void main ()
{
Sample s;
s.a=2;              // accessing public data member of a class
s.b=3;               // accessing public data member of a class
s.sum;
getch ();
}

Messages in c++

The member functions of a class are called using the objects. Some object oriented languages such as small table refer the call to member functions as message. Thus, the call

                s.sum ();

Can be thought of as sending a message to ‘s’ telling it to call sum (). Calling a member function of an object is known as sending message to the object.



Related posts:

  1. Object oriented programming Object oriented programming : The main idea behind object oriented...
  2. Procedural oriented programming Procedural oriented programming (pop):- A program in a procedural language...
  3. Implicit and explicit functions What is Implicit and Explicit function? Continue reading to find...
  4. Derivative or Differential Coefficient of a Function. Differential calculus or the concept of Derivative and Differential Coefficient...
  5. Predefined Data Types Predefined data types Data can be of many types (e.g....