Class in C++ Programming


A class is often defined as the blueprint or template for an object. We can create multiple objects from a class.

An object is an identifiable entity with some characteristics, state and behaviour.

Memory is allocated when we create the objects of a class type. A class contains properties and function to define the state and behaviour of its object.

Example :
   A dog has states - color, name, breed as well as behaviors – wagging the tail, barking, eating. An object is an instance of a class.

Syntax Of Class :
   class Class_Name
   {
        Access specifier ;
        Data Members ;
        Members Functions ( ) ;
        {
        }
   };

 Access specifier :

  • Public : You can access it from any class
  • Private : You can access it within the class where it is defined
  • Protected : Accessible only in the same package or other subclasses in another package

 Data Members :

  • Data members are the data variables to be used

 Members Functions :

  • Member functions are the functions used to manipulate these variables

Syntax Of Object :
   Class_Name object_name ;

The program demonstrates the use of classes and member functions to calculate the area and circumference of a circle.

  • The program defines a class circle that has a private data member radius and two public member functions: area() and circumference().
  • The area() function prompts the user to enter the radius of the circle, reads the input from the user, and calculates the area of the circle using the formula A=πr^2. The calculated area is then returned to the main() function.
  • The circumference() function calculates the circumference of the circle using the formula C=2πr and returns the calculated value.
  • In the main() function, an object of class circle is created and its area() and circumference() member functions are called to calculate and display the area and circumference of the circle respectively.
  • Finally, the main() function returns 0 to indicate successful program execution.

Source Code

/*
Basic Class Example
1.Area of a circle
2.Circumference of a circle
*/
#include<iostream>
using namespace std;
class circle
{
   private:
       float radius;
   public:
     float area()
     {
         //A=πr2
         cout<<"\nEnter The Radius:";
         cin>>radius;
         return (3.14*(radius*radius));
     }
     float circumference()
     {
        //C=2πr
          return (2*3.14*radius);
     }
};
 
int main()
{
    circle o;
    cout<<"Area of Circle : "<<o.area()<<endl;
    cout<<"Circumference of Circle : "<<o.circumference();
     return 0;
}
 

Output

Area of Circle :
Enter The Radius:12
452.16
Circumference of Circle : 75.36
To download raw file Click Here

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples