Class is a key feature in C++. It makes programming much easier. To implement the class we need to create objects for that class. So, let's start with 'what is a class and an object?'
How to create a class
To create a class you need to use the 'class' keyword. After that you need to specify the name of the class. The body of the class should be enclosed with curly braces. At the end you have to add a semicolon. For example,
class Circle(){ private: int r; public: getdata(); area(); };
This is a way you create a class. You can write the function in the class and also outside the class. In the above example only the name of the function is written. This means that the function needs to be written outside the class. If you wanted you can write the function there itself like:
class Circle(){ private: int r; public: getdata(){ cout<<"Enter the radius: "<<endl; cin>>r; } area(){ float a; a=3.14*r*r; cout<<"The area is : "<<a<<endl; } };
If you want to write the function outside the class you have to use the scope resoulion operator (::). Consider the following example:
void Circle::getdata(){ cout<<"Enter the radius: "<<endl; cin>>r; }
Access Specifiers
They are used to specify which functions can access the data of a function. Private is an access specifier that is used to secure the data to restrict the other functions from accessing the data in a function. If you use the 'public' access specifier it can exchange the data with other functions and classes. If you don't specify anything, C++ will consider it as private.
Objects
Now to access those functions you need to create an object. To create an object you have to do it in the main function. You have to specify the name of the class. For example,
int main(){ Circle c1; c1.getdata(); c1.area(); getch(); return 0; }