Topics

6/recent/ticker-posts

Some important programs part 2

 Some important Programs


5. Class Template: 
Note: iostream.h will be used and using namespace std is removed when using this program in turbo c++

#include<iostream>
using namespace std;
template<class t1,class t2,class t3>
class addition
{
    t1 a;
    t2 b;
    t3 sum;
public:
    void getdata()
    {
        cout<<"Enter A and B values "<<endl;
        cin>>a>>b;
    }
    void showdata()
    {
        sum=a+b;
        cout<<"Sum of 2 Numbers"<<sum<<endl;
    }
};
int main()
{
    addition<int,float,float>a1;
    a1.getdata();
    a1.showdata();
    return 0;
}

6. Friend Function:
Note: iostream.h will be used and using namespace std is removed when using this program in turbo c++


#include<iostream>
using namespace std;
class second;
class first
{
private:
    int a;
public:
    void get()
    {
        cout<<"enter a"<<endl;
        cin>>a;
    }
    friend void sum(first s,second v)
};

class second
{
private:
    int b;
public:
    void get()
    {
        cout<<"Enter B:"<<endl;
        cin>>b;
    }
    friend void sum(first s,second v)
};

void sum(first s,second v)
{
    int c;
    c=s.a+v.b;
    cout<<"Sum:"<<c<<endl;
}

int main()
{
    first s;
    second v;
    s.get();
    v.get();
    sum(s,v);
    return 0;
}


7. Function Overloading
Note: iostream.h will be used and using namespace std is removed when using this program in turbo c++

#include<iostream>
#include<stdlib.h>
using namespace std;
#define pi 3.14
class FN
{
public:
    void area(int);
    void area(int,int);
    void area(float,int,int);
};
void FN::area(int a)
{
    cout<<"The Area of the Circle"<<3.14*a*a<<endl;
}
void FN::area(int a,int b)
{
    cout<<"Area of Rectangle"<<a*b<<endl;
}
void FN::area(float t,int a,int b)
{
    cout<<"Area of the triangle"<<t*a*b<<endl;
}

int main()
{
    int ch;
    int a,b,r;
    FN obj;
    cout<<"\t\t Function overloading in polymorphism"<<endl;
    cout<<"1. Area of circle"<<endl;
    cout<<"2. Area of Rectangle"<<endl;
    cout<<"3. Area of Triangle"<<endl;
    cout<<"4. Exit"<<endl;
    cout<<"Enter your choice"<<endl;
    cin>>ch;
    switch(ch)
    {
        case 1:
        cout<<"Enter Radius of cirle"<<endl;
        cin>>r;
        obj.area(r);
        break;
        
        case 2:
        cout<<"Enter sides of rectangle"<<endl;
        cin>>a>>b;
        obj.area(a,b);
        break;
        
        case 3:
        cout<<"Enter the sidesof triangle"<<endl;
        cin>>a>>b; 
        obj.area(0.5,a,b);
        break;
        
        case 4:
        exit(0);
    }
    return 0;
}



8. Inheritance
Note: iostream.h will be used and using namespace std is removed when using this program in turbo c++

#include<iostream>
using namespace std;
class Bio
{
protected:
    char name[50];
    int age;
};

class X: public Bio
{
private:
    float height,weight;
public:
    void read()
    {
        cout<<"Enter Your Name,Age,Height,Weight"<<endl;
        cin>>name>>age>>height>>weight;
    }
    void display()
    {
        cout<<"Bio data:"<<endl;
        cout<<"Name:"<<name<<endl;
        cout<<"Age:"<<age<<endl;
        cout<<"Height:"<<height<<endl;
        cout<<"Weight:"<<weight<<endl;
    }
};
int main()
{
    X ex;
    ex.read();
    ex.display();
    return 0;
}


9. Inline functions
Note: iostream.h will be used and using namespace std is removed when using this program in turbo c++

#include<iostream>
using namespace std;
class AccNum
{
private:
  long int accno;
public:
  char name[50];
  int amount;
  void input()
  {
      cout<<"Enter Your name and account Number"<<endl;
      cin>>name>>accno;
  }
  void select()
  {
      int option;
      cout<<"Enter any option 1 or 2"<<endl;
      cin>>option;
      switch(option)
      {
          case 1:
          {
              cout<<"deposit Section Here "<<endl;
              cout<<"\n Enter the amount you want to deposit"<<endl;
              cin>>amount;
              if(amount>0)
              {
                  cout<<"Your amount "<< amount << " is successfully Deposited."<<endl;
                  cout<<"Thank You! for Depositing"<<endl;
              }
              else{
                  cout<<"The Number you entered is too small"<<endl;
              }
          }
          break;
          case 2:
          {
              cout<<"WithDraw Section Here"<<endl;
              cout<<"Enter the amount you want to WithDraw"<<endl;
              cin>>amount;
              if(amount>=1)
              {
                  cout<<"Your Amount "<<amount<<" WithDrawed Successfully"<<endl;
              }
              else
              {
                  cout<<"\n Null Balnce"<<endl;
              }
          }
          break;
      }
  }
};

int main()
{
    AccNum A;
    A.input();
    A.select();
    return 0;
}



10. Pure virtual FunctionsNote: iostream.h will be used and using namespace std is removed when using this program in turbo c++

#include<iostream>
using namespace std;
class Polygon
{
protected:
        int width,height;
public:
        void setvalues(int a,int b)
        {
            width = a;
            height = b;
        }
        virtual int area()=0;
};

class Rectangle:public Polygon
{  
public:
    int area()
    {
        return(width*height);
    }
};

class Triangle:public Polygon
{  
public:
    int area()
    {
        return(width*height/2);
    }
};

int main()
{
    Rectangle rect;
    Triangle tri;
    Polygon *poly1=&rect;
    Polygon *poly2=&tri;
   
    poly1->setvalues(4,5);
    poly2->setvalues(4,5);
   
    cout<<"Rectangle:"<<poly1->area()<<endl;
      cout<<"Triangle:"<<poly2->area()<<endl;
   
    return 0;
}

Note: iostream.h will be used and using namespace std is removed when using this program in turbo c++

Note: And remaining programs will be uploaded in next parts

Post a Comment

1 Comments