Showing posts with label oop. Show all posts
Showing posts with label oop. Show all posts

Monday, 20 February 2017

C++ program of polymorphism

In this program we will clear the concept of polymorphism, that how a single function can work for all classes.
The code is given below



//Code Starts

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

using namespace std;

class apply{
public:
virtual void brak()=0;  // Creating pure virtual function
};

class bike:public apply  {   //inheriting a class
public:
virtual void brak()
{
cout<<"Break applied to Bike"<<endl;
}
};

class car: public apply{
public:
virtual void brak()   //calling virtual function
{
cout<<"Break applied to Car"<<endl;
}
};

class truck: public apply{
public:
virtual void brak()
{
cout<<"Break applid to Truck"<<endl;
}
};

class train: public apply
{
public:
virtual void brak()
{
cout<<"Break applied to Train";
}
};

void stop(apply* abc[], int size)  //creating a function of data type void
{
for(int i=0;i<size;i++)
{
abc[i] -> brak();  //storing value in pure virtual function by this pointer ->
}
}

int main()
{
int size=4;

apply* xyz[size];

xyz[0]= new bike();  // calling class at 0 index of loop
xyz[1]= new car();
xyz[2]= new truck();
xyz[3]= new train();

stop(xyz,size); //calling void function

getch();
}
//Code Ends

OUTPUT:




Share:

Class inheritance in C++

In this program we will simply inherit two classes and clear the concept of inheritance of two classes.
The code is given below. 

//Code Starts
#include<iostream>
#include<conio.h>

using namespace std;

class woman{

public:
void eat()
{
cout<<"Woman Eat Function"<<endl;

}
};

class fish{

public:

void swim()
{
cout<<"Fish Swim Function ";

}
};

class mermaid:public woman, public fish{

};

int main()
{

mermaid m1;

m1.eat();
m1.swim();

getch();
}
//Code ends

OUTPUT:

Share:

Saturday, 18 February 2017

Class of bank account with A/C holder data

In this program we will create class of bank and enter A/C holder data and information about account also withdraw or deposit money by asking user.
The code is given below.


//Code Starts

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

using namespace std;

struct coustomer{
         
          char name[50],address[100],cnic[13],mon[10],actype[100];
          int age,bal,acno,dep,draw,rem,ammount,x,day,year;
         
};

class bank{
          private:
          char account,achold[100];
          int deposite,withdraw;
          coustomer c;
         
          public:
                   
            char setachold();
            char  setaccount();
            int  setdeposite();
      int  setdate();
            int  setwithdraw();
           
           
                      char*         getachold();
                      char getaccount();
                      int  getdeposite();
                      int  getwithdraw();
                     
                      void display();
         
};

int main()
{
          bank b;

          b.display();
         
          getch();
         
}



char bank::setachold()
{
         
          cout<<"Enter Account holder name : ";
          cin.getline(c.name,50);
         
          cout<<"Enter "<<c.name<<"'s' age  : ";
          cin>>c.age;
                   
          cout<<"Enter "<<c.name<<"'s address : ";
          cin>>c.address;
         
          cout<<"Enter "<<c.name<<"'s' CNIC : ";
          cin>>c.cnic;
}

int bank::setdate()
{
          cout<<"Please enter date in following format dd/mm/yy"<<endl;
          cout<<"Enter Day : ";
          cin>>c.day;
         
          cout<<"Enter month : ";
          cin>>c.mon;
         
          cout<<"Enter year : ";
          cin>>c.year;
         
}
char bank::setaccount()
{
          cout<<"Enter "<<c.name<<"'s account type : ";
          cin>>c.actype;
         
          cout<<"Enter "<<c.name<<"'s account number : ";
          cin>>c.acno;
         
          cout<<"Enter "<<c.name<<"'s balance in "<<c.actype<<" account : ";
          cin>>c.bal;
}

int bank::setdeposite()
{

          cout<<"How many amount you want to deposite in "<<c.acno<<" : ";
          cin>>c.dep;
         
          c.rem=c.bal+c.dep;
         
          cout<<c.name<<"'s new balance after depositing "<<c.dep<<" RS in account no :"<<c.acno<<" === "<<c.rem<<endl;
}

int bank::setwithdraw()
{
          cout<<"How many amount you want to withdarw from "<<c.acno<<" : ";
          cin>>c.draw;
          if(c.draw>c.bal)
                    {
                              cout<<"You don't have sufficient ammount to withdraw";
                    }
else{

          c.ammount=c.rem-c.draw;
         
          cout<<c.name<<"'s new balance after withdraw of  "<<c.draw<<" RS from account no :"<<c.acno<<" === "<<c.draw<<endl;;
}

}


char* bank::getachold()
{
          return achold;
}

char bank::getaccount()
{
          return account;
}

int bank::getdeposite()
{
          return deposite;
}

int bank::getwithdraw()
{
          return withdraw;
}



void bank::display()
{
    bank b;

          b.setachold();
          b.setdate();
          b.setaccount();

         
abc:   cout<<"For deposite press 1 and for withdraw Press 2 ";
          cin>>c.x;
         
          if(c.x==1)
          {
                    b.setdeposite();
          }
         
          else if(c.x==2)
          {
                    b.setwithdraw();
         
          }
          else{
                    cout<<"Invalid Entry please re-enter"<<endl;
                    goto abc;
          }
          cout<<" "<<endl;
         


}

//Code Ends

OUTPUT:

Share:

Class of bank management with password

In this program we will create a class of bank management system to access the system user needs to enter password then the result of deposit and withdraw will be shown at the end.
The code is given below.

//Code starts
#include<iostream>
#include<conio.h>
using namespace std;

class account{
         
          private:
              int bal,dep,withdraw,acno;
              char name[100];
             
              public:
                   
                    int setbal();
                    int setdep();
                    int setwithdraw();
                    int setacno();
                    char setname();
                   
                    int getbal();
                    int getdep();
                    int getwithdraw();
                    int getacno();
                    char* getname();
                   
                    void display();
                   
};

char account::setname()
{
          cout<<"Enter your name : "<<endl;
          cin>>name;
}

int account::setacno()
{
          cout<<"Enter your Account Number: "<<endl;
          cin>>acno;
}

int account::setbal()
{
          cout<<"Enter Your Account Balance: "<<endl;
          cin>>bal;
}

int account::setdep()
{
          cout<<"Enter Amount to be deposite : "<<endl;
          cin>>dep;
}

int account::setwithdraw()
{
          cout<<"Enter Ammount to be withdraw :"<<endl;
          cin>>withdraw;
}

char* account::getname()
{
          return name;
}

int account::getacno()
{
          return acno;
}

int account::getbal()
{
          return bal;
}

int account::getdep()
{
          return dep;
}

int account::getwithdraw()
{
          return withdraw;
}

void account::display()
{
          cout<<"Name : "<<getname()<<endl;
          cout<<"A/C No :  "<<getacno()<<endl;
          cout<<"Balance : "<<getbal()<<" RS "<<endl;
          cout<<"Deposte : "<<getdep()<<" RS "<<endl;
          cout<<"Withdraw :  "<<getwithdraw()<<" RS "<<endl;
}

int main()
{    
       int pass,a,b;
      
         
          account a1;
abc:   cout<<"Enter Password: "<<endl;
                    cout<<"Hint: 12345"<<endl;
                    cin>>pass;
         
          if(pass==12345)
{

cout<<"Welcome to Bank Management System: "<<endl;

}
else{
          cout<<"Re-enter Password:  "<<endl;
          goto abc;
}

          a1.setname();
          a1.setacno();
          a1.setbal();
          a1.setdep();
          a=a1.getbal()+a1.getdep();
          cout<<"Amount after deposite : "<<a<<" RS."<<endl;
         
def:    a1.setwithdraw();
                   
          if(a1.getwithdraw()>a)
          {
                    cout<<"Enter amount less than "<<a<<" RS. "<<endl;
                    goto def;
          }
          else{
                    b=a-a1.getwithdraw();
                              cout<<"Amount after withdraw : "<<b<<" RS."<<endl;
          }

         
    cout<<"===============OUTPUT=================="<<endl;

a1.display();

getch();


}

OUTPUT:




Share:

Class of student data

In this program we will create class of student and enter his record using setter and getter function with one extra function of display.
The code is given below.

//Code starts 
#include<iostream>
#include<conio.h>
using namespace std;

class stu
{
          private:
                    char nam[30],id[30];
                    float cgpa;
          public:
                    char setname();
                    char setid();
                    float setcgpa();
                   
                    char* getname();
                    char* getid();
                    float getcgpa();
                   
                    void display();     
};

char stu::setname()
{
          cout<<"Enter Student's name: ";
          cin>>nam;
}
char stu::setid()
{
          cout<<"Enter Student's ID: ";
          cin>>id;
}

float stu::setcgpa()
{
          cout<<"Enter CGPA of Student: ";
          cin>>cgpa;
}

char* stu::getname()
{
          return nam;
}

char* stu::getid()
{
          return id;
}

float stu::getcgpa()
{
          return cgpa;
}

void stu::display()
{
stu  s;     //creating object of class
          s.setname();
          s.setid();
          s.setcgpa();
}

int main()
{
          stu s1; //creating object of class
         
          s1.display();
         
          getch();

}
//code ends


Share:

Class of employee entering data in functions of class

In this program we will create a class of employee, create its setter and getter functions and call them in main().

//Code Starts
#include<iostream>
#include<conio.h>

using namespace std;

class emp{
         
          private : int salary;
                    char name[20],desig[50],resp[50];
                   
                    public:
                             
              void setsalary();
              void setname();
               void setdesig();
               void setresp();
                                                 
                 int getsalary();
                 int getname();
                 int getdesig();
                 int getresp();
                                                 
               void display();                                             
};
 void emp::setsalary()  {
                                                                               
 cout<<"Enter Salary of Employee: ";
 cin>>salary;
           }
                                                                     
void emp::setname() {
                                                                               
cout<<"Enter name of employee : ";
 cin>>name;
                \   }
                                                                     
void emp::setdesig()
 {
 cout<<"Enter designation of employee : ";
 cin>>desig;
           }
                                                                     
 void emp::setresp()
  {
  cout<<"Enter Resposibility of "<<name<<" : ";
  cin>>resp;
             }
                                                           
 int emp::getsalary()
  {
         return salary;
   }
  
void emp::display()
{
 cout<<name<<" works in company as "<<desig<<" "<<resp<<" on salary of RS."<<salary;
 if(salary<=20000)
  {
     cout<<"Salary is average";
          }
else{
   cout<<"Above average salary";
       }
                 }

int main()
{
          emp e1;
         
          for(int i=1;i<=2;i++)
          {
         
          e1.setname();
         
          e1.setdesig();
         
          e1.setresp();
         
          e1.setsalary();
         
          e1.display();
         
          cout<<endl;
         
          }
          getch();

}

//Code ends


Share: