OOPS - [C++ Declaration of Class]

♠ Posted by Unknown in at 06:48

Explanation of Class


The single most important feature of C++ is the CLASS. The class is the mechanism that is used to create objects.

A class is declared using the “class” keyword. The syntax of c “class” declaration is similar to that of structure. The general form is shown here.

                                    class class_name{
                                                //private function and variables
                                                public:
                                                //public function and variables
                                    } object_list;

In a class declaration, the object_list is optional. As with a structure, you can declare class object later, as needed. While the class_name is also technically optional, from a practical point of view it is virtually always needed. The reason for this is that the class_name becomes a new type name that is used to declare objects of the class.

Function and variables declared inside a class declaration are said to be members of that class. By default, all function and variables declared inside a class are private to that class. This means that they are accessible only by other members of that class. To declare public class members, the “public” keyword is used, followed by colon. All functions and variables declared after the “public” specifier are accessible both by other members of the class and by any other part of the program that contains the class.
Notice, that function that are declared to be part of a class are called member functions. To define a member function out of class, you must link the type name of the class with the name of the function. You do this by preceding the function name with the class name followed by two colons. The two colons are called the “scope resolution operator”.

For example

#include<iostream.h>
#include<conio.h>
class Sum
{
            int a, b;
 public:
            void getdata();//Member Function Declaration
            int Answer(); //Member Function Declaration
};
void Sum::getdata()//Member Function Definition
{
 cout<<"Enter the First Value :";
 cin>>a;
 cout<<"Enter the Second Value :";
 cin>>b;
}
int Sum::Answer()//Member Function Definition
{return a + b;}

void main()
{
 Sum ss;//Object Created
 clrscr();
 ss.getdata();//Calling of member function
 int c = ss.Answer();
 cout<<"The Sum is :"<<c<<endl;
 getch();
}
Notice that both set_a() and get_a() have access to “a”, which is private to myclass. Because set_a() and get_a() are members of myclass, the can directly access its private data.
In general, to define a member function you must use this form:
                                    ret_type class_name :: func_name(parameter_list)
                                    {
                                                //body of function;
                                    }

Once an object of class has been created, your program can reference its public members by the dot (period) operator in much the same way that structure members are accessed. Each object contains its own copy of all data declared with in the class.

A class declaration is a logical abstraction that defines a new type. It determines what an object of that type will look like. An object declaration creates a physical entity of that type. That is, an object occupies memory space, but a type definition does not.

1 comments:

Post a Comment