How To Make A Class in C++



HOW TO MAKE CLASS
·                   A complete description of class for biggners who have no knowledge about classes and for software engineers and for computer engineers.

Defining the Class
Here the class rational is defined as fellow......

class rational                                                   // define a class
{
private:
            int num;                                             //  class data
            int dnum;                                           //  class data
public:
            void setval(int n,int d)                         // member function to set data
            {
                        num=n;
                        dnum=d;
            }
            void get ( )                                       // member function to get data
            {
                        cout<<"enter numerator";
                        cin>>num;
                        cout<<"enter denomenator";
                        cin>>dnum;
            }
            void print ()                                   // member function to display or print data
            {
                        cout<<num<<"/"<<dnum<<endl;
            }
};                                                          // class end


Access Control in a Class
The keyword public looks a bit like a label, but in fact it is more than that. It determines the access attributes of the members of the class that follow it. Specifying the data members as public means that these members of an object of the class can be accessed anywhere within the scope of the class object. You can also specify the members of a class as private or protected. In fact, if you omit the access specification altogether, the members have the default attribute, private. We shall look into the effect of these keywords in a class definition a bit later.
Remember that all we have defined so far is a class, which is a data type. We haven't declared any objects of the class. When we talk about accessing a class member, say height, we're talking about accessing the data member of a particular object, and that object needs to be declared somewhere.


Declaring Objects of a Class
We declare objects of a class with exactly the same sort of declaration that we use to declare objects of basic types. . So, we could declare objects of our class, rational, with these statements:
rational  r1, r2 ;  
or
rational r1 ;
rational r2;

Accessing the Data Members of a Class
The data members of objects of a class can be referred to using the direct member access operator (.). So,
r1.get();
r2.get();
r1.print();
r2.print();
Functions are public and data is private
..........................................................................................................................................................................................
A Sample of Class
Try It Out - Your First Use of Classes

Lets see a example of class rational that will clear your idea about class making
#include <iostream>
using namespace std;
class rational
{
private:
            int num;
            int dnum;
public:
            void setval(int n,int d)
            {
                        num=n;
                        dnum=d;
            }
            void get ()
            {
                        cout<<"enter numerator";
                        cin>>num;
                        cout<<"enter denomenator";
                        cin>>dnum;
            }
            void print ()
            {
                        cout<<num<<"/"<<dnum<<endl;
            }
};
void main ()
{
            rational r1,r2;
            r1.get();
            r2.setval(2,3)
            r1.print();
            r2.print ();
}



How It Works
Everything here works as we would have expected from our experience with structures. The definition of the class appears outside of the function main() and, therefore, has global scope. This enables objects to be declared in any function in the program and causes the class to show up in the ClassView once the program has been compiled.
We've declared two objects of type Box within the function main(), rational r1,r2. Of course, as with variables of the basic types, the objects rational r1,r2 are local to main(). Objects of a class obey the same rules with respect to scope as variables declared as one of the basic types .
The first two assignment statements set the values of the data members of rational r1. We define the values of the data members of rational r2 in terms of the data members of rational r1 in the same  assignment statements.
Now the calculation work is done and output is in your way.