Tuesday, December 25, 2012

OOP: Linkist

Posted by at 8:49 AM
Object:


Create linklist of four digits taking input from the user.


Source Code :

#include <iostream>
using namespace std;

class Node{
    public:
        int data;
    Node * next;
    Node(int x)
        {
        data = x;
        next = NULL;
        }
    Node(int x, Node * y)
        {
        data = x;
        next = y;
        }
    };


class linkedList{
Node *head;
public:
    linkedList()
        {
        head = NULL;
        }
    void addNode(int value)
    {
        Node *ptr;
        if(head == NULL)
            head = new Node (value, NULL);
        else{
            ptr=head;
            while(ptr->next !=NULL)
                ptr=ptr->next;
            ptr->next = new Node (value, NULL);
            }
        }

    void print()
    {
        Node * ptr1;
        ptr1 = head;
       
        while(ptr1 != NULL)
        {
            cout << ptr1->data << "\n";
            ptr1 = ptr1->next;
        }
    }
};


void main()
{
//////////////////// simple crete link list //////////////
linkedList test;
int num;
int input;

cout<<"linklist of four digits \n";
for(num=0;num<4;num++)
{

cout<<"input value "<<num+1<<" : ";
cin>>input;
test.addNode(input);


}
test.print();

}
Output:

OOP: Static function

Posted by at 7:31 AM
Object:

Create the Calender class in which user enter value of Day Month Year and if user not enter the values initialize the values of day month year by using Static function to the Default values.

Source Code:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

 class calender
{
private:
       int day,month,year;
        static  int dday,dmonth,dyear;// d stand for default
public:
    
      calender()
      {};

       void calculate(int a,int b,int c)
       {day=a;
       month=b;
       year=c;
       ///////////////////////// Days ////////////////////
             if(day>30)
                 {do
              {

              month=month+1;
              day=day-30;

              }while(day>30);
             }
       /////////////////////// Month ////////////////////
              if(month>12)
              {do
              {
              year=year++;
              month=month-12;
              }while(month>12);
            
              if(month==0)
              {
              month++;
              }
              }
       ////////////////////// Year //////////////////////

       }
       void showdata()
       {
              system("cls");
               cout<<"    Current Date:"<<endl;
              cout<<"\tMydays calender"<<endl;
       cout<<"\t"<<day<<" "<<month<<" "<<year<<" "<<endl;
       }
   
      ////////////////////// Static Class /////////////
      
   
       static void defauLT()
    {
      
            system("cls");
              cout<<"\tMydays calender\n"<<endl;
              cout<<"    Default Date:"<<endl;
       cout<<"\t"<<dday<<" "<<dmonth<<" "<<dyear<<" "<<endl;

    }

}; 

 int calender::dday=30;
 int calender::dmonth=12;
 int calender::dyear=2012;

void main()
{
    char select;
    cout<<"Press 1 for set Current Date "<<endl;
    cout<<"Press 2 for Defualt Date"<<endl;
    cin>>select;
    switch(select)
    {
    case '1':
        {
            calender cal;
       int dd,mm,yy;
              cout<<"Enter Days"<<endl;
       cin>>dd;
              cout<<"Enter Month"<<endl;
       cin>>mm;
              cout<<"Enter Year"<<endl;
       cin>>yy;
       cal.calculate(dd,mm,yy);
       cal.showdata();
            break;}

    case '2':
        {
            calender::defauLT();
       
        break;
       
        }
    }
      
}

Output:

Tuesday, December 18, 2012

OOP : Polymorphism

Posted by at 3:13 AM
Object:
By using the following class diagram write down class definitions in c++. In the main create an aray of 50 pointers to the Base class. The program keeps on creating new obkects(student,teacher,manager,labour) as user wants and each new object is referenced by pointers. Call setdata() function as the object is created. Once all objects are created,call the display function for each of them.

Source code:
#include<iostream>
#include <string>
using namespace std;

 class person
 {
private:
    string name;
    string age;
 public:
    virtual void getdata()
    {
    cout<<"Enter Name: "<<endl;
    cin>>name;
    cout<<"Enter age: "<<endl;
    cin>>age;
    }
    virtual void setdata()
    {
    cout<<"Name: "<<name;
    cout<<"Age: "<<age;
    }
 };

 ////////////////////////////////////////////////////

  class student : public person
 {
private:
    string id;
    string gpa;
 public:
     person::getdata;
     person::setdata;
    void getdata()
    {
    cout<<"Enter id: "<<endl;
    cin>>id;
    cout<<"Enter GPA: "<<endl;
    cin>>gpa;
    }
    void setdata()
    {
    cout<<"ID: "<<id<<endl;;
    cout<<"Gpa: "<<gpa<<endl;;
    }
 };
  ///////////////////////////////////////////////
   class teacher : public person
 {
private:
    string faculty;
    string publication;
 public:

      person::getdata;
     person::setdata;

    void getdata()
    {
    cout<<"Enter faculty: "<<endl;
    cin>>faculty;
    cout<<"Enter publication: "<<endl;
    cin>>publication;
    }
    void setdata()
    {
    cout<<"faculty: "<<faculty<<endl;;
    cout<<"publication: "<<publication<<endl;;
    }
 };
///////////////////////////////////////////////
 class employee : public person
 {
private:
    string e_id;
    string salary;
 public:
      person::getdata;
     person::setdata;
    void getdata()
    {
    cout<<"Enter employee id: "<<endl;
    cin>>e_id;
    cout<<"Enter Salary: "<<endl;
    cin>>salary;
    }
    void setdata()
    {
    cout<<"Employee iD: "<<e_id<<endl;
    cout<<"salary: "<<salary<<endl;
    }
 };

 /////////////////////////////////////////////////
  class manager:public employee
 {
private:
    string designation;

 public:
     employee::getdata;
     employee::setdata;
    void getdata()
    {
    cout<<"Enter DESIGNATION: "<<endl;
    cin>>designation;
   
    }
    void setdata()
    {
   
    cout<<"Designation: "<<designation<<endl;
   
    }
 };
  ////////////////////////////////////////////////
   class labour : public employee
 {
private:
    string overtime;
   
 public:

      employee::getdata;
     employee::setdata;

    void getdata()
    {
    cout<<"Enter Overtime: "<<endl;
    cin>>overtime;
   
    }
    void setdata()
    {
   
    cout<<"Overtime: "<<overtime<<endl;
    }
 };

   void main()
   {
    person obj;
    person *ptr;

    student obj1;
    teacher obj2;
    employee obj3;
    manager obj4;
    labour obj5;



cout<<"Class Person"<<endl;
ptr=&obj;
ptr->getdata();
cout<<endl;
ptr->setdata();

cout<<"Class Student"<<endl;
ptr=&obj1;
ptr->getdata();
cout<<endl;
ptr->setdata();

cout<<"Class teacher"<<endl;

ptr=&obj2;
ptr->getdata();
cout<<endl;
ptr->setdata();

cout<<"Class Employee"<<endl;
ptr=&obj3;
ptr->getdata();
cout<<endl;
ptr->setdata();

cout<<"Class Manager"<<endl;
ptr=&obj4;
ptr->getdata();
cout<<endl;
ptr->setdata();
      
cout<<"Class Labour"<<endl;
ptr=&obj4;
ptr->getdata();
cout<<endl;
ptr->setdata();
    }


Output
  

    

Popular Posts

Labels

© Codepirate is powered by Blogger - Template designed by Stramaxon - Best SEO Template