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
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.]
Int x, y, z;
Void output ()
Cout<<” \n enter the value of X & Y:”;
Void main ()
a. Output ();
Creating object / defining the object of a class
The general syntax of defining the object of a class is:-
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:-
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.
Int a, b, c;
Void sum ()
Cout<<”\n sum :”<< c;
Void main ()
s.a=2; // accessing public data member of a class
s.b=3; // accessing public data member of a class
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
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.
- Object oriented programming Object oriented programming : The main idea behind object oriented...
- Procedural oriented programming Procedural oriented programming (pop):- A program in a procedural language...
- Implicit and explicit functions What is Implicit and Explicit function? Continue reading to find...
- Derivative or Differential Coefficient of a Function. Differential calculus or the concept of Derivative and Differential Coefficient...
- Predefined Data Types Predefined data types Data can be of many types (e.g....