OOPc using C++

With this blog I want to share my knowledge of OOPs and C++. I Hope this can quench your thirst for learning.Any suggestions and comments are welcome.

Monday, August 23, 2010

Program 13 - C++ program to illustrate the usage of this pointer

/*C++ program to illustrate the usage of this pointer*/
#include<iostream.h>
#include<conio.h>
#include<string.h>

class person
{
       char name[20];
       float age;
public:
      person(char *s,float a)
     {
             strcpy(name,s);
             age=a;
       }
     person &person::greater(person &x)
    {
           if(x.age>=age)
                 return x;
          else
                 return *this;
     }
   void display()
   {
          cout<<"Name: "<<name<<"\nAge: "<<age<<endl;
     }
};
void main()
{
     clrscr();
     person p1("John",37.50),p2("Ahmed",29.0),p3("Heffer",40.25);
     person p=p1.greater(p3);
     cout<<"Elder person is:\n";
     p.display();
     p=p1.greater(p2);
     cout<<"Elder person is\n";
     p.display();
   
    getch();
}

OUTPUT:
Elder person is:
Name: Heffer
Age: 40.25
Elder person is
Name: John
Age: 37.5

Program 12 - C++ program to perform arithmetic operations on complex numbers using operator overloading

/*C++ program to perform arithmetic operations on complex numbers using operator overloading*/
#include<iostream.h>
#include<conio.h>
class complex
{
          float x,y;
          public:
              complex() {}
              complex(float real,float img)
               {
                       x=real; y=img;
                }
complex operator+(complex);
complex operator-(complex);
complex operator*(complex);
void operator/(complex);
void display()
{
        cout<<x<<" + "<<y<<"i"<<endl;
}
};
complex complex::operator+(complex c)
{
         complex temp;
         temp.x=x+c.x;
         temp.y=y+c.y;
         return(temp);
}
complex complex::operator-(complex d)
{
         complex temp;
         temp.x=x-d.x;
         temp.y=y-d.y;
         return(temp);
}
complex complex::operator*(complex e)
{
        complex temp;
        temp.x=x*e.x+y*e.y*(-1);
        temp.y=x*e.y+y*e.x;
        return(temp);
}
void complex::operator/(complex f)
{
       complex temp;                               //Numerator
       temp.x=x*f.x+y*(-f.y)*(-1);        //Real number of the numerator
       temp.y=x*(-f.y)+y*f.x;               //Imaginery number of the numerator
       float deno;                                  //Denominator
       deno=f.x*f.x-f.y*f.y*(-1);        //Similar to (a+b)(a-b)=a2-b2
       cout<<temp.x<<" + "<<temp.y<<"i / "<<deno;
}

void main()
{
      clrscr();
      complex c1(5,3),c2(3,2),c3=c1+c2,c4=c1-c2,c5=c1*c2;
      c1.display();
      c2.display();
      cout<<"Addition"<<endl;
      c3.display();
      cout<<"Subtraction"<<endl;
      c4.display();
      cout<<"Multiplication"<<endl;
      c5.display();
      cout<<"Division"<<endl;
      c1/c2;
     getch();
}

OUTPUT:
5 + 3i
3 + 2i
Addition
8 + 5i
Subtraction
2 + 1i
Multiplication
9 + 19i
Division
21 + -1i / 13

Monday, August 9, 2010

Program 11 : A simple program illustrating Constructor and Destructor

#include < iostream >
using namespace std;
// class declaration
class myclass 
{
               int a;
     public:
               myclass( ); //constructor
              ∼myclass( ); //destructor
              void show( );
};

myclass::myclass( ) 
{
     cout << "In constructor\n";
     a=10;
}
myclass::∼myclass( )
{
    cout << "Destructing...\n";
} // ...



void main( )
{
          myclass c;
}

OUTPUT

Note:
You will notice that initially you will only get the output

In Constructor

But,After exiting the output screen come back to program and refresh ( Alt+F5 ), Then you will the output 

Destructing...

The reason being that the constructor is called when the object is created and destructor is called when the object is destroyed.The object will be destroyed only when we have exited the main program. So we need to refresh after exiting the program and Tada!! You get the output "Destructing..."

Friday, August 6, 2010

Program 10 - Simple Interest (Asked in the test on 6/8/10)


This program takes in the prinicipal, rate and time as a screen input from the user.
The program is executed (run) 5 times using the 'FOR' loop.
It calculates the simple interest using the formula I = PTR/100.
The principal, rate, time and the simple interest are then outputted using the 'cout' command.

#include <iostream.h>
#include <conio.h>

void main()
{
     clrscr();
     int i;
     float  fsinterest,fprincipal,frate,ftime;
     for(i=4;i>=0;i--)
       {
          cout << "Enter the principal, rate & time : " << endl;
          cin>>fprincipal>>frate>>ftime;
          fsinterest=(fprincipal*frate*ftime)/100;
          cout << "Principal = $" << fprincipal << endl;
          cout << "Rate = " << frate << "%" << endl;
          cout << "Time = " << ftime << " years" << endl;
          cout << "Simple Interest = $" << fsinterest << endl;
        }
getch();
}

Program 9 - Employee Class advanced


The following program which utilizes the same class Employee with a few extensions:

class Employee
{
     int Empno;
     char *Ename;
     char *Add;
     float Salary, Comm, TotSal;

     public:
            void Input( );
            void Output( );
            void ChangeEmpno(int);                //function with argument of type int
            void ChangeEmpno2(int =2);         //function with default argument
            float Tot_sal( );                           //function with return type float
};

int main()
{
           Employee Emp1;
           Emp1.Input( );
           Emp1.Output( );

           cout<<Emp1.Tot_Sal( );
           
           Emp1.ChangeEmpno( );
           Emp1.ChangeEmpno(458);
           Emp1.ChangeEmpno2(100);

          return 0;
}

void Employee::Input()
{
          cin>>Empno>>Ename>>Add>>Salary>>Comm;
          TotSal=Tot_Sal( );               //Input() is calling Tot_Sal()
}
void Employee::Output( )
{
          cout<<Empno<<Ename<<Add<<Salary<<Comm<<TotSal;
}
void Employee::ChangeEmpno( )
{
          int NewEmpno;
          cin>>NewEmpno;

          Empno=NewEmpno;
}
void Employee::ChangeEmpno(int NewEmpno)
{
        Empno=NewEmpno;
}
void Employee::ChangeEmpno2(int IncEmpno)
{
        Empno+=IncEmpno;
}
float Employee::Tot_sal( )
{
        return Salary+Comm;
}

Program 8 - Employee Class program

This program takes the name,age, and salary of the employee as input and just displays the entered data.

#include<iostream.h>


class Employee
{
            private: 
                       char cname[30];
                       int iage,isalary;
            public:
                       void getdata( )                    //Function to get input from the user
                        {
                                 cout<<"Enter the Name of the Employee"<<endl;
                                 cin>>cname[30];
                                 cout<<"Enter the Age and salary of the employee"<<endl;
                                 cin>>iage>>isalary;
                            }


                       void display( )              //Function to display the entered data
                       {
                                 cout<<"***************************"<<endl; 
                                 cout<<"Name of the Employee : "<<name[30];
                                 cout<<"Age : "<<endl;
                                 cout<<"Salary : "<<endl;
                                 cout<<"***************************"<<endl;
                         }


};                                                                        //End of class


void main( )
{
             Employee e1,e2,e3;                 //Creating objects e1,e2,e3
               
             e1.getdata( );                           // Calling getdata function using object e1
             e2.getdata( );
             e3.getdata( );
          
             e1.display( );                          //Calling getdata function using object e1
             e2.display( );
              e3.display( );
}