Class and object program in c++ is simple and basic program in object oriented programming concept.
In this tutorial we will see C++ classes and objects, simple class program in c++.
First we will see what is class, how to define class and how to create object in c++.
What is class ?
Class definition : Class is a user defined data type. It encloses data members and member functions.
By default all the members of the class are private.
Syntax for class
How to define class
class class_name {
<visibility mode>:
Datatype mem1;
……
Datatype memN;
<visibility mode>:
return_type function_name(arguments);
….
};
return_type class_name :: function_name(arguments)
{
//body of function
}
How to create object
Class_name object_name1,…..object_nameN ;
Algorithm for simple class and object in c++
- Start
- Write class with member function and declare functions. class circle.
- Define function outside of class using scoe resolution operator
- Write a main function
- Create object
- Call function using objects.
- End
Area of circle program in c++ using class
12345678910111213141516171819202122232425262728293031 | #include <iostream>using namespace std;class circle{ float radius, area; //data members public: circle() { cout<<“\n Enter the value of Radius : “; cin>>radius; } void calculate(); //member function void display(); //member function};void circle :: calculate() //accessing data members of a class circle{ area = 3.14 * radius * radius;}void circle :: display(){ cout<<“\n Area of Circle : “<<area;}int main(){ circle cr; //object created cr.calculate(); //calling function cr.display(); //calling function return 0;} |
Output :

Explanation :
1. Execution starts with main method.
2. First we create object of class
circle cr; => object created.
and default constructor get executed.
circle() {
cout<<“\n Enter the value of Radius : “;
cin>>radius; => radios = 5
}
3. Next we call function in class circle using object
cr.calculate(){ => function get call and function executed.
area = 3.14 * radius * radius; => area= 78.5
} => returns to main method
4. Next we call function display() in class circle using object
cr.display(){ => function get call and function executed.
cout<<“\n Area of Circle : “<<area; => Area of Circle : 78.5
} => returns to main method
5. End with the program.