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 :
Data Members :
Members Functions :
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.
/* 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; }
Area of Circle : Enter The Radius:12 452.16 Circumference of Circle : 75.36To download raw file Click Here
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions