Pages

January 30, 2013

Classes & Objects in C++ (OOPs)


practical use of classes and objects in C++ (OOPs) programming language

“C with classes” was the original name given by the originator Stroustrup initially, which now days is popular known form as C++ or OOPs (Object Oriented Programming). Classes and objects are the most important feature of C++. The Class implements OOPs features and ties then together. We have already seen that a structure groups different types of elements and a function organizes the program actions to perform the desired task.


What is a CLASS that makes C++ a necessary way to express?

A CLASS is a user defined data type. It contains member data and member functions to access that data. When we are defining a class we are defining the structure and behavior of a set of objects. All the objects belonging to a particular class will have same structure i.e. they will have same member variables and same behavior i.e. they will be using same set of functions.
Members of CLASS
1. Public
2. Private 
3. Protected

In C++, the keyword public, private and protected are called as Access Specifiers. The data hiding concept in C++ is achieved by using the keyword private. Private data and functions can only be accessed from within the class itself. Public data and function can be accessed anywhere.

Data Hiding does not mean the security technique used for protecting computer data-bases. The security measure is used to protect from unauthorized user from performing any of the operation on important data. The data declared under private section are hidden and safe from accidental manipulations. If the user doesn't defining any access specifier then by default its private.

Protected data are members declared as protected can be accessed with in the class and any class that inherits this class.

practical use of classes and objects in C++ (OOPs) programming language

What is an OBJECT and why a CLASS is nothing without its OBJECT?

An OBJECT is an instance of a CLASS.  It has the same relationship with the CLASS as a variable has with its data type. A CLASS is a template that defines the structure and behavior of set of objects. An OBJECT is the physical implementation of a CLASS, due to this CLASS is nothing without having its OBJECT.

Format of defining a CLASS and its OBJECT
class  class_name
{
    private:
            variable declaration;
            function declaration;
            executable statements;
    protected:
            variable declaration;
            function declaration;
            executable statements;
    public:
            variable declaration;
            function declaration;
            executable statements;
    };
main()
{
          class  class_name obj;
          object.function declaration heading(pass values);
           ………..;
          getch();
    }


Types of CLASS method definition:

1. Inside CLASS definition
2. Outside CLASS definition

Inside CLASS Definition

When a member function is defined inside a CLASS, we do not require to place a member name label along with the function name. We use only small functions inside the CLASS definition and such functions are classed INLINE functions.


INLINE FUNCTION
The powerful function used in OOPs in CLASS function or member function. CLASS function binds the data member into a single entity which is used again and again. CLASS functions are declared at any access level, but mostly these are declared under the keyword public.

The Inline Function is C++ enhancement designed module to speed up the program. All the functions defined inside the class specifiers are Inline by default. Note that an Inline function is a function used for special purpose, which tells computer to replace the function code, whenever the function is taken into existence. Actual application is that it reduces size of the program and increases the execution speed of the program.

Syntax for Inline Function
inline  data-type  function_name (argument lists)
{
       Body of inline function;
   }

Inline Function does not work if such conditions appears:
1. If a function is recursive.
2. If a function contains static variables.
3. For function that return value and are having loop statements, switch statements or go-to statements.
4. For a function with no return value, if return statement exists.


Some programs with Inside CLASS Definition:

/* Program to perform arithmetic operations using classes and objects*/

#include<iostream.h>
#include<conio.h>
int add,sub,mul,mod;
class operation
{
      public:
      void sum(int a,int b)
      {
           add=a+b;
           cout<<"\n Addition result is "<<add;
           }
      void subt(int a,int b)
      {
           sub=a-b;
           cout<<"\n Subtraction result is "<<sub;
           }
      void mult(int a,int b)
      {
           mul=a*b;
           cout<<"\n Multiplication result is "<<mul;
           }
      void divi(float a,int b)
      {
           float div=a/b;
           cout<<"\n Division result is "<<div;
           }
      void modu(int a,int b)
      {
           mod=a%b;
           cout<<"\n Remainder result is "<<mod;
           }
};
main()
{
      int c,d;
      cout<<"\n Enter any two integers";
      cin>>c>>d;
      class operation o;
      o.sum(c,d);
      o.subt(c,d);
      o.mult(c,d);
      o.divi(c,d);
      o.modu(c,d);
      getch();
      }
output of program to perform arithmetic operations using classes and objects in C++ (OOPs)
           

/* Program to show student details using classes and objects*/

#include<iostream.h>
#include<conio.h>
class stud
{
      public:
             char name[30],clas[10];
             int rol,age;
             float per;
   void enter()
   {
        cout<<"\n Enter Student Name, Age, Roll number";
        cin>>name>>age>>rol;
        cout<<"\n Enter Student Class and percentage in previous class";
        cin>>clas>>per;
           }
    void display()
    {
        cout<<"\n Age\tName\tR.No.\tClass\t%ge";
        cout<<"\n "<<age<<"\t"<<name<<"\t"<<rol<<"\t"<<clas<<"\t"<<per<<"%";
           }
};
main()
{
      class stud s;
      s.enter();
      s.display();
      getch();
      }
output of program to show student details using classes and objects in C++ (OOPs)

Outside CLASS Definition

Here operator ‘::’ is used which is Scope Resolution Operator which helps in defining the member function outside the CLASS. Earlier the SRO (::) was used in the situations where a Global Variables exists with the same name as a local variable and it identifies the global variable.
Some programs with Outside CLASS Definition:

/* Program to perform arithmetic operations using classes declared outside the class*/


#include<iostream.h>
#include<conio.h>
int add,sub,mul,mod;
class operation
{
      public:
      void sum(int a,int b);
      void subt(int a,int b);
      void mult(int a,int b);
      void divi(float a,int b);
      void modu(int a,int b);  
};
void operation::sum(int a,int b)
{
           add=a+b;
           cout<<"\n Addition result is "<<add;
           }
void operation::subt(int a,int b)
{
           sub=a-b;
           cout<<"\n Subtraction result is "<<sub;
           }
void operation::mult(int a,int b)
{
           mul=a*b;
           cout<<"\n Multiplication result is "<<mul;
           }
void operation::divi(float a,int b)
{
           float div=a/b;
           cout<<"\n Division result is "<<div;
           }
void operation::modu(int a,int b)
{
           mod=a%b;
           cout<<"\n Remainder result is "<<mod;
           }
           
main()
{
      int c,d;
      cout<<"\n Enter any two integers";
      cin>>c>>d;
      class operation o;
      o.sum(c,d);
      o.subt(c,d);
      o.mult(c,d);
      o.divi(c,d);
      o.modu(c,d);
      getch();
      }
output of program to perform arithmetic operation using classes declared outside the class in C++ (OOPs)
             

/* Program to show student details using classes declared outside the class*/

#include<iostream.h>
#include<conio.h>
class stud
{
      public:
             char name[30],clas[10];
             int rol,age;
             float per;
      void enter();
      void display();
};
void stud::enter()
{
     cout<<"\n Enter Student Name, Age, Roll number";
     cin>>name>>age>>rol;
     cout<<"\n Enter Student Class and percentage in previous class";
     cin>>clas>>per;
           }
void stud::display()
{
     cout<<"\n Age\tName\tR.No.\tClass\t%ge";
     cout<<"\n "<<age<<"\t"<<name<<"\t"<<rol<<"\t"<<clas<<"\t"<<per<<"%";
           }
main()
{
      class stud s;
      s.enter();
      s.display();
      getch();
      }
output of program of showing student details using classes declared outside the class in C++ (OOPs)



No comments:

Post a Comment