DS &OOPS LAB MANUAL

LAB MANUAL

LIST OF EXPERIMENTS

1.    Basic Programs for C++ Concepts

1.    Area of a Circle Using Class
2.    Find even or odd number Using Class
3.    Print Fibonacci series using Constructors
4.    Implementation of unary operator overloading
5.    Implementation of Multiple inheritance
6.    Find the mean value of a given number using friend function.
7.    Implementation of  Virtual function
8.    To implement String Operations


2.  Array implementation of List Abstract Data Type (ADT)
3.  Linked list implementation of List ADT
4.  Cursor implementation of List ADT
5.  Stack ADT - Array and linked list implementations

The next two exercises are to be done by implementing the following source files
(a)   Program source files for Stack Application 1
(b)   Array implementation of Stack ADT
(c)   Linked list implementation of Stack ADT
(d)   Program source files for Stack Application 2
An appropriate header file for the Stack ADT should be #included in (a) and (d)

6.  Implement any Stack Application using array implementation of Stack ADT (by implementing files (a) and (b) given above) and then using linked list implementation of Stack ADT  (by using files (a) and implementing file (c))
7.   Queue ADT – Array and linked list implementations-
8.   Search Tree ADT - Binary Search Tree
9. Heap Sort
     10. Quick Sort
BASIC PROGRAMS

1.    AREA OF A CIRCLE USING CLASS

AIM:
            To write a C++ program to find the area of a circle by implementing Classes and Objects.

PROCEDURE:
           
Step 1: Start the program
Step 2: Create a class “circle” with 2 data members.
Step 3: Create a member function “area()” to compute the area of the
    Circle.
            Step 4: Create a main() function and create an object for the class.
Step 5: Call the function “area()” by using the object.
Step 6: Stop the Program

  
PROGRAM:

// Area Of a Circle using Class
#include<iostream.h>
#include<conio.h>
class circle
{
int r;
float ar;
public:
            void area(int r)
                        {
                        ar=3.14*r*r;
                        cout<<"Area of the Circle is "<<ar;
                        }
};

void main()
            {
            circle obj;                   //Object Creation
            clrscr();
            obj.area(2);                // Calling the function
            getch();
            }

OUTPUT:

            Area of the circle is 12.56

  
RESULT:
Thus the program to find the area of a circle by implementing classes and objects was written and executed.



2.    Find even or odd number Using Class

AIM:
            To write a C++ program to find whether the given number is odd or even by implementing Classes and Objects.


PROCEDURE:
           
Step 1: Start the program
Step 2: Create a class “number” a data member to read a number.
Step 3: Create a member function “read()” to read the input
Step 4: Create another member function “find()” to find whether the
    given number is odd or even  and print the result.
            Step 5: Create a main() function and create an object for the class.
Step 6: Call the function “read()” and “find()” by using the object.
Step 7: Stop the Program

 PROGRAM:

//To find the given number is Odd or Even

#include<iostream.h>
#include<conio.h>
class number
{
int num;

public:
void read()
            {
            cout<<"Enter a number      :           ";
            cin>>num;
            }
void find()
            {
            if(num%2==0)
                        {
                        cout<<"The given number is Even";
                        }
            else
                        {
                        cout<<"The given number is Odd";
                        }
            }
};
void main()
{
number obj;
clrscr();
obj.read();
obj.find();
getch();
}

OUTPUT 1:

Enter a Number        :           457
The Given number is Odd


OUTPUT 2:

Enter a Number        :           324
The Given number is Even


RESULT:

Thus the program to find whether the given number is odd or even by implementing Classes and Objects was written and executed.
3.    FIBONACCI SERIES

AIM:

            To write a C++ program to print Fibonacci Series by implementing constructors.
.
PROCEDURE:
           
Step 1: Start the program
Step 2: Create a class “demo” with 3 data members.
Step 3: Create a constructor demo for initializing the data members
Step 4: Create a member function “fibo()” to compute the Fibonacci series.                     Step 5: Create a main() function and create an object for the class.
Step 6: Call the function “fibo()” by using the object.
Step 7: Stop the Program

PROGRAM:

//Fibonacci Series Using Constructors

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

class demo
{
int a,b,t;

public:
            demo()                        // Constructor Definition
                        {
                        a=0;
                        b=1;
                        t=0;
                        }
            void fibo(int n)
                        {
                        cout<<"\n\nFibonacci Series...\n\n";
                        if(n<2)
                                    {
                                    cout<<a;
                                    }
                        else
                                    {
                                    cout<<a<<"\t"<<b;
                                    for(int i=2;i<n;i++)
                                                {
                                                t=a+b;
                                                cout<<"\t"<<t;
                                                a=b;
                                                b=t;
                                                }
                                    }
                        }


};
void main()
            {
            demo obj;
            int n;
            clrscr();
            cout<<"Enter No Of Terms :           ";
            cin>>n;
            obj.fibo(n);
            getch();
            }


OUTPUT:
           

RESULT:

            Thus program to print Fibonacci Series by implementing constructors was written and executed.


4. unary operator overloading

AIM:
To write a program to find the complex numbers using unary operator overloading.

ALGORITHM:

Step 1: Start the program.
Step 2: Declare the class.
Step 3: Declare the variables and its member function.
Step 4: Using the function getvalue() to get the two numbers.
Step 5: Define the function operator ++ to increment the values
Step 6: Define the function operator - -to decrement the values.
Step 7: Define the display function.
Step 8: Declare the class object.
Step 9: Call the function getvalue
Step 10: Call the function operator ++() by incrementing the class object and call the   function display.
Step 11: Call the function operator - -() by decrementing the class object and call the function display.
Step 12: Stop the program.                           

PROGRAM:

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

class complex
{
     int a,b,c;
    public:
        complex(){}
        void getvalue()
       {
                 cout<<"Enter the Two Numbers:";
                 cin>>a>>b;
       }
             
    

  void operator++()
      {
                 a=++a;
                 b=++b;
       }
             
       void operator--()
       {
                 a=--a;
                 b=--b;
        }
             
        void display()
        {
                 cout<<a<<"+\t"<<b<<"i"<<endl;
         }
};

void main()
{
     clrscr();
     complex obj;
     obj.getvalue();
     obj++;
     cout<<"Increment Complex Number\n";
     obj.display();
     obj--;
     cout<<"Decrement Complex Number\n";
     obj.display();
     getch();
}

Output:

Enter the two numbers: 3 6              
Increment Complex Number
4 +               7i
Decrement Complex Number
3     +               6i
  

5.Multiple inheritance.
AIM:
To find out the student details using multiple inheritance. 

ALGORITHM:

Step 1: Start the program.
Step 2: Declare the base class student.
Step 3: Declare and define the function get() to get the student details.
Step 4: Declare the other class sports.
Step 5: Declare and define the function getsm() to read the sports mark.
Step 6: Create the class statement derived from student and sports.
Step 7: Declare and define the function display() to find out the total and average.
Step 8: Declare the derived class object,call the functions get(),getsm() and display().
Step 9: Stop the program.

PROGRAM:

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

class student
{
    protected:
       int rno,m1,m2;
    public:
                void get()
              {
                            cout<<"Enter the Roll no :";
                            cin>>rno;
                            cout<<"Enter the two marks   :";
                            cin>>m1>>m2;
              }
};
class sports
{
    protected:
       int sm;                   // sm = Sports mark
    public:
                void getsm()
              {
                 cout<<"\nEnter the sports mark :";
                 cin>>sm;

              }
};
class statement:public student,public sports
{
    int tot,avg;
    public:
    void display()
              {
                 tot=(m1+m2+sm);
                 avg=tot/3;
                 cout<<"\n\n\tRoll No    : "<<rno<<"\n\tTotal      : "<<tot;
               cout<<"\n\tAverage    : "<<avg;
              }
};
void main()
{
   clrscr();
   statement obj;
   obj.get();
   obj.getsm();
   obj.display();
   getch();
}

Output:

              Enter the Roll no: 100

              Enter two marks
             
              90
              80

              Enter the Sports Mark: 90

              Roll No: 100
              Total    : 260
              Average: 86.66
  

6.Friend function
AIM:
To find the mean value of a given number using friend function.

ALGORITHM:

STEP 1:  Start the program.
STEP 2:  Declare the class name as Base with data members and member functions.
STEP 3:  The function get() is used to read the 2 inputs from the user.
STEP 4:  Declare the friend function mean(base ob) inside the class.
STEP 5:  Outside the class to define the friend function and do the following.
STEP 6:  Return the mean value (ob.val1+ob.val2)/2 as a float.
STEP 7:  Stop the program.

PROGRAM:

#include<iostream.h>
#include<conio.h>
class  base
{
    int val1,val2;
   public:
    void get()
    {
       cout<<"Enter two values:";
       cin>>val1>>val2;
    }
    friend float mean(base ob);
};
float mean(base ob)
{
   return float(ob.val1+ob.val2)/2;
}
void main()
{
    clrscr();
    base obj;
    obj.get();
    cout<<"\n Mean value is : "<<mean(obj);
    getch();
}          

Output:

Enter two values: 10, 20
Mean Value is: 15




7.Virtual functions.

AIM
Simple Example Program for virtual functions.

ALGORITHM:

Step 1: Start the program.
Step 2: Declare the base class base.
Step 3: Declare and define the virtual function show().
Step 4: Declare and define the function display().
Step 5: Create the derived class from the base class.
Step 6: Declare and define the functions display() and show().
Step 7: Create the base class object and pointer variable.
Step 8: Call the functions display() and show() using the base class object and pointer.
Step 9: Create the derived class object and call the functions display() and show() using the derived class object and pointer.
Step 10: Stop the program.

PROGRAM:

#include<iostream.h>
#include<conio.h>
class base
{
    public:
      virtual void show()
      {
                cout<<"\n  Base class show:";
      }
      void display()
      {
              cout<<"\n  Base class display:" ;
      }
};

class drive:public base
{
   public:
      void display()
      {
              cout<<"\n  Drive class display:";
      }
      void show()
      {
              cout<<"\n  Drive class show:";
      }
};

void main()
{
   clrscr();
   base obj1;
   base *p;
   cout<<"\n\t P points to base:\n"  ;

   p=&obj1;
   p->display();
   p->show();

   cout<<"\n\n\t P points to drive:\n";
   drive obj2;
   p=&obj2;
   p->display();
   p->show();
   getch();
}

Output:

              P points to Base

              Base class display
              Base class show

              P points to Drive

              Base class Display
              Drive class Show
  

8.String Operations

AIM:
            To write a C++ program to implement String Operations
.
PROCEDURE:

PROGRAM:

//String Operations

#include<iostream.h>
#include<string.h>
#include<conio.h>
void main()
{
char a[20]="HELLO",b[20]="welcome",c[20];
int i,j,m,n;
clrscr();
cout<<"\n\nString 1 =          "<<a;
cout<<"\n\nString 2 =          "<<b;
cout<<"\n\nLower Case of String 1 is "<<strlwr(a);
cout<<"\n\nUpper Case of String 2 is "<<strupr(b);
cout<<"\n\nConcatination of 2 Strings is "<<strcat(a,b);
cout<<"\n\nReverse of String 2 is "<<strrev(b);
cout<<"\n\nCopy of String 1 in String 2 is "<<strcpy(b,a);
getch();
}

OUTPUT:


RESULT:


            Thus the program to implement String Operations was created and executed.

Comments

Post a Comment

Popular posts from this blog

PROGRAMMING AND DATA STRUCTURES - 2 MARKS

CS2302 Computer Networks _16 Important Questions