Operators and functions in oops

2
Operator:

An operator is a symbol that specifies an operation to be performed.

The basic types of operators are:
  • Arithmetic operator

  • Relational operator

  • Logical operator

  • Conditional operator

Arithmetic operators: +, - , *, /, %, ^

    They are used to perform some arithmetic operations.

Relational operators: <,>,<=,>=,!=, ==
    They are used to compare values.These operators return either true or false.

Logical operators: &&, ||, !

    These are the basic logical operators and they are used in decision making statements.

Conditional operator: ?:
     
    This operator is known as conditional operator. It may also be called as ternary operator.

A c++ program to perform arithmetic operations:
Code:
#include<iostream.h>
#include<conio.h>
class arith
{
 private:
    int opt,num1,num2,res;
 public:
    void getdata()
    {
      cout<<"enter your option\n 1.add\n 2.sub\n 3.mul\n 4.div";
      cin>>opt;
      cout<<"enter the first number";
      cin>>num1;
      cout<<"enter the second number";
      cin>>num2;
 
    }
};
void main()
{
  arith a;
  a.getdata();
  switch(opt)
  {
     case 1:
          cout<<"ADDITION";
          res=num1+num2;
          cout<<"result of addition is"<<res;
          break;
     case 2:
          cout<<"SUBTRACTION";
          res=num1-num2;
          cout<<"result of subtraction is"<<res;
          break;

      case 3:
          cout<<"MULTIPLICATION";
          res=num1*num2;
          cout<<"result of multiplication is"<res;
          break;
      case 4:
          cout<<"DIVISION";
          res=num1/num2;
          cout<<"result of division is"<<res;
          break;
      default:
         cout<<invalid choice";
         break;
   }
 getch();
}
     
Quote:Output of the above program:
enter your option
1. add
2. sub
3. mul
4. div
1
enter the first number
25
enter the second number
25
ADDITION
result of addition is 50


Functions:

   Functions are the building blocks of any program.The program which is implemented using functions will be more effective since it establishes reusability of code.
  
Parts of function:
  • Declaration

  • Definition

  • Calling
Here the declaration part is optional.The function may or may not be declared.But the when the functions are used in the program, then it must be defined and called.

Syntax:
  
 return_type fun_name(arguments)

Example:
 void fun()

Calling functions:
  
  The function may or may not contains arguments.The function may be called in three ways:
  •   Passing no arguments and returning nothing
  •   Passing arguments and returning nothing
  •   Passing arguments and returning something
A program implemented using functions:
Code:
#include<iostream.h>
#include<conio.h>
class fun
{
 private:
   int n;
 public:
   void getdata(); // Fnction declaration
   void showdata()
   {

     cout<<"REGISTER NUMBER:"<<regno;
     cout<<"BRANCH:"<<branch;
   }
};
void fun::getdata() // function definition
{
 cout<<"enter your register number";
 cin>>regno;
 cout<<"enter your branch";
 cin>>branch;
}
void main()
{
 fun f;
 f.getdata();
 f.showdata();
 getch();
}  

 
Fear is a reaction,but courage is a destination
Nicethread @arthi how did u insert the text box
Thanks to V+ Bye1
just click the "insert code" icon in the toolbar
Fear is a reaction,but courage is a destination

    Operators and functions in oops