C++ language

Classe and Object In C++
Previous Home Next

Classes In C++

A class is like structure in C. A class is a way to bind the data and its associated functions in a unit. It allows the data to be hidden. The keyword 'class' is used to define a class. The body of class is enclosed within braces and terminated by a semicolon. These functions and variables collectively called 'class members'. The class body contains the variables and functions All classes members are private by default. A class is inherited privately by default. The arrays can be used as member variables in class.

The class members that have been declared private can be accessed only within the class. On the other hand public class members can be accessed outside the class.

The variables defined in the class is called data members and the functions that are used in the class is celled member function. We can say that class is a collection of data member and member function. Only the member function can have access to the private data members and private function, while the public member can be accessed from outside the class.

A class must have end with a semicolon.

Class Declaration

class class_name
private :
   variable declaration;
    function declaration;
public :
   variable declaration;
    function declaration;

Object in C++

A class provides the blueprints for objects, so we can say an object is created from a class. In other word Class is a collection of object .Hance we can say an object is the smallest entity of class which connect with the real word entity.

class_name variable name;

// Simple example in c++ using Class and Objects

#include <iostream>
using namespace std;
class temp
        int val1;
        float val2;
       void int_val(int d){
          cout<<"Number: "<<val1;
       float float_val(){
           cout<<"\nEnter data: ";
           return val2;
 int main(){
      temp obj1, obj2;
      cout<<"You entered "<<obj2.float_val();
      return 0;

Previous Home Next