New Android apps UniqueKey

Showing posts with label C plus. Show all posts
Showing posts with label C plus. Show all posts

Tuesday, 12 May 2015

Class Program in C++

This is a C++ program to illustrate the class and its use in C++ programming language. Class is a main feature in object oriented programming language like C++.

#include<iostream>
#include<string.h>
using namespace std;

class employee
{
  char name[50];
  int number;
  float basic_pay,allowance,it,gross,net;
  public:
    void read(char e_name[],int e_number,float e_basic_pay);
    void calculate(void);
    void display(void);
};

void employee::read(char e_name[],int e_number,float e_basic_pay)
{
  strcpy(name,e_name);
  number=e_number;
  basic_pay=e_basic_pay;
  
}

void employee::calculate(void)
{
  allowance=(123*basic_pay)/100;
  gross=basic_pay+allowance;
  it=(30*gross)/100;
  net=basic_pay+allowance-it;
}

void employee::display(void)
{
  cout<<"Name: "<<name<<endl;
  cout<<"Number: "<<number<<endl;
  cout<<"Basic pay: "<<basic_pay<<endl;
  cout<<"Allowance: "<<allowance<<endl;
  cout<<"Income tax: "<<it<<endl;
  cout<<"Net Salary: "<<net<<endl;
}

main()
{
  employee e1;
  char e_name[50];
  int e_number;
  float e_basic_pay;
  cout<<"Name: ";
  cin>>e_name;
  cout<<"Employee number: ";
  cin>>e_number;
  cout<<"Basic Pay: ";
  cin>>e_basic_pay;
  e1.read(e_name,e_number,e_basic_pay);
  e1.calculate();
  e1.display();
}

Saturday, 21 March 2015

Inline Function

Inline function: whenever we use inline function in our program, and whenever compiler executes inline function, each time it create a copy of function code and execute the functions.
for use inline function you just have to write "inline" in front of function name.  

#include<iostream>
using namespace std;

inline float area(float radius)
{
  return 3.14*radius*radius;
}

inline float peri(float radius)
{
  return 2*3.14*radius;
}

inline float simple(float amount,float interest,int year)
{
  return amount*(interest/100)*year;
}

main()
{
  int year;
  float amount,radius,interest;
  cout << "Enter radius: ";
  cin >> radius;
  cout << "Area : " << area(radius)<<endl;
  cout << "Perimeter : " << peri(radius)<<endl;
  cout << "Simple Interest Calculator" << endl;
  cout << "Amount: "<<endl;
  cin >> amount;
  cout << "Interest (in %): ";
  cin >> interest;
  cout << "Year: ";
  cin >> year;
  cout << "Simple interest: "<< simple(amount,interest,year)<<endl;
}

Swapping using Function Overloading


#include<iostream>
#include<string.h>
using namespace std;
class swapp{
        public:
        int itemp;
        float ftemp;
        char ctemp[100];

        void swap_it(int num1,int num2)
        {
                cout<<"\nBEFORE SWAPPING :"<<"\n"<<"NUM1: "<<num1<<"\nNUM2 :"<<num2;
                itemp=num1;
                num1=num2;
                num2=itemp;
                cout<<"\nAFTER SWAPPING :"<<"\nNUM1: "<<num1<<"\nNUM2 :"<<num2;
        }
        void swap_it(float num1,float num2)
        {
                cout<<"\nBEFORE SWAPPING :"<<"\n"<<"NUM1: "<<num1<<"\nNUM2 :"<<num2;
                ftemp=num1;
                num1=num2;
                num2=ftemp;
                cout<<"\nAFTER SWAPPING :"<<"\n"<<"NUM1: "<<num1<<"\nNUM2 :"<<num2;
        }
        void swap_it(char c1[100],char c2[100])
        {
                cout<<"\nBEFORE SWAPPING :"<<"\n"<<"CHAR1 :"<<c1<<"\nCHAR2 :"<<c2;
                strcpy(ctemp,c1);
                strcpy(c1,c2);
                strcpy(c2,ctemp);
                cout<<"\nAFTER SWAPPING :"<<"\n"<<"CHAR1 :"<<c1<<"\nCHAR2 :"<<c2;
        }
};
int main()
{
        swapp swapp;
        int i1,i2;
        float f1,f2;
        char c1[100],c2[100];
        cout<<"\nENTER THE INTEGER VARIABLES";
        cin>>i1>>i2;
        swapp.swap_it(i1,i2);
         cout<<"\nENTER THE FLOAT VARIABLES";
        cin>>f1>>f2;
        swapp.swap_it(f1,f2);
         cout<<"\nENTER THE CHARACTER VARIABLES";
        cin>>c1>>c2;
        swapp.swap_it(c1,c2);
        return 0;
}

Stack operations


#include<iostream>
using namespace std;
int size;
class stack{
        public:
                int stck[100];
                int top;
                int i;
                int element;


                void push(void)
                {
                        cout<<"\nenter the element to push into th stack\n";
                        cin>>element;
                        stck[++top]=element;

                }
                void display(void)
                {
                        for(i=top;i>=0;i--)
                        {
                        if(i==top)
                        {
                        cout<<"TOP OF STACK-->"<<stck[i];
                        }
                        else
                        {
                        cout<<"\t\n"<<stck[i];
                        }
                        }
                }
                int pop(void)
                {
                        return(stck[top--]);
                }
int isfull()
                {
                        if(top==size-1)
                        {
                        return 1;
                        }
                        else
                        {
                        return 0;
                        }
                }
                int isempty()
                {
                        if(top==-1)
                        {
                                return 1;
                        }
                        else
                        {
                                return 0;
                        }
                }
                stack()
                {
                        stck[100]={0};
                        top=-1;
                }

};
int main()
{
        int choice;
        stack stack;
        cout<<"\nenter the size of stack\n";
        cin>>size;
        do
        {
        cout<<"\n..MENU..\n1.push\n2.pop\n3.display.\n4.EXIT";
        cin>>choice;
        switch(choice)
        {
        case 1:
                if(!(stack.isfull()))
                {
                        stack.push();
                }
                else
                {
                        cout<<"\nSTACK IS FULL\n";
                }
                break;
        case 2:
                if(!(stack.isempty()))
                {
                        cout<<"\nTHE POPPED OUT ELEMENT IS :"<<stack.pop();
                }
                else
                {
                        cout<<"\nSTACK IS EMPTY\n";
                }
                break;
        case 3:
                stack.display();
                break;
        case 4:
                cout<<"\nYOU HAVE EXITED\n";
                break;

        }
 }while(choice!=4);
        return 0;
}