oops lab manual.doc

Upload: ahmed-abdelrahman

Post on 14-Apr-2018

243 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 oops lab manual.doc

    1/59

    I N D E X - II N D E X - I

    S.NOS.NO NAME OF THE EXPERIMENTNAME OF THE EXPERIMENT PAGE NOPAGE NO11 To create the program to find total,

    average of given two numbers by using

    function with default arguments, static

    data members and friend function.

    11

    22 The program to implement complex

    number class with necessary operator

    overloading and type conversion such as

    integer to complex, complex to double.

    55

    33 To implement matrix class with dynamic

    memory allocation and necessary

    methods. With proper constructor,

    destructor, copy constructor and

    overloading of assignment operator.

    1010

    44 Overload the new and delete operators to

    provide custom dynamic allocation of

    memory.

    1515

    55 Develop a template of linked-list class and

    its methods.

    1919

    66 Develop templates of standard sorting

    algorithms such as bubble sort, insertion

    sort, merge sort and quick sort.

    2424

    77 Design stack and queue classes withnecessary exception handling.

    3131

    .

  • 7/30/2019 oops lab manual.doc

    2/59

    I N D E X - I II N D E X - I I

    S.NOS.NO NAME OF THE EXPERIMENTNAME OF THE EXPERIMENT PAGE NOPAGE NO8 Define point class and an Arc class.

    Define a Graph class which represents

    graph as collection of point objects and

    Arc objects. Write a method to find a

    minimum cost spanning tree in a graph.

    40

    9 Develop with suitable hierarchy, classes

    for Point, Shape, Rectangle, Square,

    Circle, Ellipse, Triangle, Polygon, etc.

    Design a simple test application to

    demonstrate dynamic polymorphism and

    RTTI.

    48

    10 Write a C++ program that randomly

    generates complex numbers and writes

    them two per line in a file along with an

    operator (+,-,*, or /). The numbers are

    written to file in the format (a + ib). Write

    another program to read one line at a

    time from this file, perform the

    corresponding operation on two complex

    numbers read, and write the result to

    another file ( one per line ).

    54

    EX. NO. 1

  • 7/30/2019 oops lab manual.doc

    3/59

  • 7/30/2019 oops lab manual.doc

    4/59

    {private:static int count;int v1,v2;public:

    void getdata(int n1=35,int n2=89);void getcount();friend void average(prog p);};int prog :: count;void prog :: getcount(){count++;cout

  • 7/30/2019 oops lab manual.doc

    5/59

    OUTPUT:

    1 OBJECT :

    GIVEN VALUES : 35 89Total = 124

    Average = 62

  • 7/30/2019 oops lab manual.doc

    6/59

    2 OBJECT :

    GIVEN VALUES : 100 350Total = 450

    Average = 225

    RESULT:Thus the program to find total, average of given two numbers by using

    function with default arguments, static data members and friend function is

    developed and executed successfully.

    EX. NO. 2AIM: The program to implement complex number class with necessary operator

    overloading and type conversion such as integer to complex, complex to double.

    ALGORITHM:

    1. Include the required header files for the program.

    2. Declare the class complex with two float variable in private access

  • 7/30/2019 oops lab manual.doc

    7/59

    specifier.

    3. In public access specifier declare default constructor, constructor

    overloading, Operator function for operator overloading, conversion

    function for converting class to basic type.Constructor with one

    argument used for converting basic to class type, argument is

    converted to class type.

    4. Declare display function with no argument and return type to display

    result.

    5. Define constructors, operator function, conversion function after class

    declaration with scope resolution operator( : : ).

    6. In main function first declare two objects of class complex c1, c2 with

    two float arguments, that will create two object and store float values

    in each object.

    7. Then create another object complex c3=c1+ c2 to call operator function

    for operator overloading. The real parts of c1, c2 and imaginary part of

    c1, c2 added separately put to c3 object.

    8. Display real, imaginary value of objects cl,c2 and c3 by calling display

    function.

    9. Create another object of the class complex, c4 then declare and assign

    value for the integer variable value, after that the statement c4 = value,

    this statement call the constructor with one integer argument for

    converting integer to complex.

    10. Then display the value of the object c4 by calling display function,

    c4.display().

    11. Create another object complex c5, after declare and assign the value

    for the variable of double, double num=c5, this statement call the

    conversion function to complex to double type.

  • 7/30/2019 oops lab manual.doc

    8/59

    12. After that display value of num.

    13. Close the main( ) function,

    PROGRAM:#include#includeclass complex{private:

    float real,imag;public:

    complex();complex(float,float);

    complex(int);complex operator+(complex);operator double();void display();};complex :: complex(){real=0.0;

  • 7/30/2019 oops lab manual.doc

    9/59

    imag=0.0;}complex :: complex(float x,float y){real=x;

    imag=y;}complex complex :: operator+(complex c){complex temp;temp.real=real+c.real;temp.imag=imag+c.imag;return temp;

    }void complex :: display(void){

    cout

  • 7/30/2019 oops lab manual.doc

    10/59

    complex c4;int value=9;c4 = value;c4.display();cout

  • 7/30/2019 oops lab manual.doc

    11/59

    RESULT:Thus the program to implement complex number class with

    necessary operator overloading and type conversion such as integer to complex,

    complex to double is successfully developed and executed.

    EX. NO. 3

    AIM:To implement matrix class with dynamic memory allocation and necessary

    methods. With proper constructor, destructor, copy constructor and overloading

    of assignment operator.ALOGORITHM:

    1. Include the required header files and one global variable, initialize to

    zero.

    2. Declare class matrix with two integer data members in public access

    specifier and one default constructor, constructor with argument, copy

    constructor with reference of the object as argument, destructor and

    display function in public access specifier.

    3. Define constructors, destructor and display function outside the class

    using scope resolution operator ( : : ) . In display function increment

    global variable value before showing which object of the class. In

    destructor decrement global variable after showing which object

    deleted.

  • 7/30/2019 oops lab manual.doc

    12/59

    4. In main( ) function create and initialize object of class matrix, m1 by

    parameterized constructor. Then create one pointer object of the class

    matrix, *ptr1 and assign address of the object m1, to the pointer object

    *ptr1.

    5. Call the display function by pointer object using ->( arrow operator) to

    show the values of the object, m1.

    6. Then create another object of the class matrix, m2 and initialize value

    of m1 to m2 by calling copy constructor, matrix m2(m1).

    7. Create another pointer object of the class matrix, *ptr2 and assign

    address of the object m2, to the pointer object *ptr2.

    8. Call the display function by pointer object using -> ( arrow operator) to

    show the values of the object, m2.

    9. Create another object of the class matrix m3, the assign m3 = m2.

    Now copy constructor not called. The assignment operator ( = ) copy

    the member of the object m2 to m1 one by one, here overloading of

    assignment operator ( = ) happening.

    10. Call the display function by the object m3 using dot operator, to show

    the values of object m3.11. Close the main( ) function.

  • 7/30/2019 oops lab manual.doc

    13/59

    PROGRAM:

    #include#include

    int count=0;class matrix{private:

    int m,n;public:

    matrix(){

    }matrix(int,int);

    matrix(matrix &x);~matrix();void display();

    };matrix :: matrix(int x,int y){m=x;n=y;}matrix :: matrix(matrix &x){

    m=x.m;n=x.n;

    }matrix :: ~matrix(){cout

  • 7/30/2019 oops lab manual.doc

    14/59

    void matrix :: display(){ cout

  • 7/30/2019 oops lab manual.doc

    15/59

    OUTPUT:

    1: OBJECT VALUESm= 45n=68

    2: OBJECT VALUESm= 45n=68

    3: OBJECT VALUESm= 45n=68

    DELETION OF OBJECTS BY DESTRUCTOR :

    3 Object destroyed2 Object destroyed1 Object destroyed

    RESULT:

    Thus the program to implement matrix class with dynamic memory allocation

    and necessary methods. With proper constructor, destructor, copy constructor

    and overloading of assignment operator.

  • 7/30/2019 oops lab manual.doc

    16/59

    EX. NO. 4

    AIM:

    To overload the new and delete operators to provide custom dynamic

    allocation of memory.

    ALGORITHM:

    1. Include the required header files in the program.

    2. Declare the class dynamic with integer variable in private access

    specifier, constructor, destructor, operator function, operator new with

    one argument of type size_t which is size of the class and void pointer

    as return type to overload the new operator.

    3. Include another operator function, to overload delete operator with no

    return type and void pointer as argument.

    4. In constructor initialize the value of integer variable, and display the

    statement, constructor is called.

    5. In operator function, void * operator new(size_t size), declare one void

    pointer and allocate memory through malloc( ) function by giving size

    of the class as argument. And check memory allotted or not. Then

    return void pointer.

    6. In operator function, void operator delete(void *p), delete the allocated

    memory by using function free( ) by giving void pointer as argument.

    7. In destructor display the statement, destructor is called.

    8. Define all these functions, constructor, destructor, operator function

    inside the class itself.

    9. In main( ) function, create one pointer object and allocate memorythrough new operator, dynamic *dptr = new dynamic, this statement

    call the operator function, void * operator new(size_t size) and it

    returns void pointer.

  • 7/30/2019 oops lab manual.doc

    17/59

    10.Next delete(dptr), this function call the operator function, void operator

    delete(void *p) to overload delete operator.

    11.Close the main( ) function.

    PROGRAM:

    #include

  • 7/30/2019 oops lab manual.doc

    18/59

    #include#includeclass dynamic{

    int i;

    public:dynamic(){i=1;cout

  • 7/30/2019 oops lab manual.doc

    19/59

    Destructor is called . .

    RESULT:Thus the program to overload the new and delete operators to provide

    custom dynamic allocation of memoryis developed and executed successfully.

    EX. NO .5

    AIM:

    Program to develop a template of linked-list class and its methods.

    ALGORITHM:

  • 7/30/2019 oops lab manual.doc

    20/59

    1. Include the required header files in the program.

    2. Declare the class linkedlist, with one variable of integer type, to store

    the value in each node and one pointer of class type to store address

    of the next node in each node in private access specifier.

    3. In public access specifier declare three methods, one for to create

    nodes at runtime and to store the value, address of another node in

    each node.

    4. Another function to display the value stored in each node.

    5. Another function to count the number of nodes created.

    6. All these three member functions of linkedlist class, have one

    argument, pointer object.

    7. Close the class linkedlist.

    8. All the member function of the template class linkedlist is defined

    outside the class. It is done by template function, so all the member

    functions declare in the class are template functions.

    9. In main( ) function declare two pointer objects. Initialize one pointer to

    null, for another pointer allocate dynamic memory by new operator.

    10.Call all the member function by pointer object initialized to null by

    arrow operator ( -> ) with another pointer object, which is dynamicallymemory allocated.

    11.First call the create function, next print function to display the value

    stored in each node. Finally call count function , this function return

    integer value to display the number of nodes created

    12.Close the main( ) function.

  • 7/30/2019 oops lab manual.doc

    21/59

    PROGRAM:

    #include#includetemplateclass linkedlist{private:

    int number;linkedlist *next;public:

    void create(linkedlist *node);void print(linkedlist *node);

  • 7/30/2019 oops lab manual.doc

    22/59

    int count(linkedlist *node);};templatevoid linkedlist::create(linkedlist *node){

    coutnext!=0){coutnext==0)coutnumber;print(node->next);

    } }

    templateint linkedlist::count(linkedlist *node){if(node->next==0)return 0;elsereturn(1+(count(node->next)));}

    void main(){linkedlist *node1=0;clrscr();linkedlist *node2;node2=new linkedlist;node1->create(node2);cout

  • 7/30/2019 oops lab manual.doc

    23/59

    node1->print(node2);cout

  • 7/30/2019 oops lab manual.doc

    24/59

    Enter the number(Enter -999 at an end):67Enter the number(Enter -999 at an end):

    75Enter the number(Enter -999 at an end):-999

    45-->89-->76-->234-->99-->67-->75-->-999

    Number of nodes: 7

    RESULT:The program to develop a template of linked-list class and its methods

    is developed, executed successfully.

    EX. NO. 6

    AIM:

  • 7/30/2019 oops lab manual.doc

    25/59

    To develop templates of standard sorting algorithms such as bubble sort,

    insertion sort, merge sort and quick sort.

    ALGORITHM:1. Include the required header files in the program

    2. Create the template function for bubble sort with two arguments, one is

    array of type T, another one is integer argument and define the function.

    3. Create another template function for insertion sort with two arguments,

    one is array of type T, another one is integer argument and define the

    function.

    4. Create another template function for merge sort with three arguments,

    one is pointer of type T, another two is integer argument and define the

    function.5. Create another template function, sort with two argument, one is pointer

    of type T, another one is integer type and define the function. Recursion

    happening in this function, merge function is called from this function.

    6. Create another template function for quick sort with three arguments,

    one is pointer of type T, another two is integer argument and define the

    function.

    7. Create another template function, swap with three arguments, one is

    pointer of type T and other two arguments of type integer. This

    function is called from quick sort to swap the values.

    8. Create another template function, print with two arguments, one is

    pointer of type T and another is integer type. This function going to

    display the values before and after sorting for both merge and quick

    sort.

    9. In main() function, first declare float array,x1[5] with five elements and

    initialize the values at design time itself.

    10.Display the values of the array x1 before and after calling bubble sort

    using for loop.

  • 7/30/2019 oops lab manual.doc

    26/59

    11.Next declare integer array, x2[5] with five elements and initialize the

    values at design time itself.

    12.Display the values of the array x1 before and after calling insertion sort

    using for loop.

    13.Next declare double array, x3 with five elements and initialize the

    values at design time itself.

    14.Display the values of the array x3 before sorting by calling print()

    function with corresponding arguments.

    15. Call the sort() function with corresponding arguments to execute the sort

    function, from the sort function, merge sort function is called.

    Display the values of the array x3 after merge sort function execution by

    calling print() function with corresponding arguments.

    16. Finally create char array, x4 with five elements and initialize the values

    at design time itself.

    17. Display the values of the array x4 before quick sorting by calling print()

    function with corresponding arguments.

    18.Call the quick() function with corresponding arguments, to execute the

    quick sort and display the values by calling print() function with

    corresponding arguments.19. Close the main () function.

    PROGRAM:#include#include

  • 7/30/2019 oops lab manual.doc

    27/59

    templatevoid bubble(T a[],int n){T temp;for(int i=0;i

  • 7/30/2019 oops lab manual.doc

    28/59

    }templatevoid sort(T *a,int n){if(n>1)

    {int n1=n/2;int n2=n-n1;sort(a,n1);sort(a+n1,n2);merge(a,n1,n2);}}templatevoid quick(T *a,int first,int last){

    int i,j,pivot;if(first

  • 7/30/2019 oops lab manual.doc

    29/59

    }templatevoid print(T *a,int n){cout

  • 7/30/2019 oops lab manual.doc

    30/59

    print(x4,5);quick(x4,0,4);cout

  • 7/30/2019 oops lab manual.doc

    31/59

    8.9 6.7 5.9 9.8 4.9

    AFTER BUBBLE SORT :

    4.9 5.9 6.7 8.9 9.8

    BEFORE INSERTION SORT :

    78 42 59 87 21

    AFTER INSERTION SORT :

    21 42 59 78 87

    BEFORE MERGE SORT:

    12.7865 11.6545 15.999 25.1789 2.333

    AFTER MERGE SORT:

    2.333 11.6545 12.7865 15.999 25.1789

    BEFORE QUICK SORT:

    h d a c f

    AFTER QUICK SORT:

    a d c f h

    RESULT:Thus the program to develop templates of standard sorting algorithms such as

    bubble sort, insertion sort, merge sort and quick sort is developed, executed

    successfully.

    EX. NO. 7

  • 7/30/2019 oops lab manual.doc

    32/59

    AIM:Design stack and queue classes with necessary exception handling

    ALGORITHM:

    1. Start the program.2. Include the required header files.

    3. Declare the global variables needed for the program.

    4. Declare the class stack with one array in private and five function

    for push, pop, display, length and isempty in public access

    specifier.

    5. Close the class stack. Define function members outside the class.

    6. Declare the class queue with four member functions, enqueue,

    dequeue, display and length in public access specifier.

    7. Close the class queue. Define function members outside the

    class.

    8. Open the main( ) function. Declare the object of stack class, and

    one integer variable count, another variable i=1.

    9. Display the required statements for stack operation.

    10.Inside the try block , use while loop check i12, display the statement Stack operation is stopped.

    14.Close the while loop and then try block.

    15.Thrown exception is caught by catch block and it takes

    appropriate action.

  • 7/30/2019 oops lab manual.doc

    33/59

    16.Initialize i =1, then create object of the class queue, display the

    required statements for queue operation.

    17.Inside the try block, use while loop check i12, display the statement Queue operation is stopped.

    21.Close the while loop and then try block.

    22.Thrown exception is caught by catch block and it takes

    appropriate action.

    23.Close the program.

    PROGRAM:

    #include

  • 7/30/2019 oops lab manual.doc

    34/59

    #include#include#include#define size 10int q[size],front=0,rear=0;

    int top=-1;class stack{private:

    int stk[size];public:

    void push();void pop();int length();int isempty();void display();

    };void stack::push(){

    int no;if(top==size-1)cout

  • 7/30/2019 oops lab manual.doc

    35/59

    top--;}void stack::display(){int i;

    if(isempty()){cout

  • 7/30/2019 oops lab manual.doc

    36/59

    void queue::dequeue(){int no,i;if(front==rear)cout

  • 7/30/2019 oops lab manual.doc

    37/59

    cin>>choice;if(choice==1)s1.push();else if(choice==2)s1.pop();

    else if(choice==3)s1.display();else if(choice==4)

    cout

  • 7/30/2019 oops lab manual.doc

    38/59

    else if(choice==4)cout

  • 7/30/2019 oops lab manual.doc

    39/59

    Enter your choice : 1

    Enter a Number : 89Enter your choice : 1

    Enter a Number : 45Enter your choice : 1

    Enter a Number : 90Enter your choice : 1

    Enter a Number : 39Enter your choice : 1

    Enter a Number : 72

    Enter your choice : 3

    Elements in the stack72 39 90 45 89 Enter your choice : 4

    Number of elements in the stack: 5 Enter your choice : 2

    72 is Popped from the stackEnter your choice : 2

    39 is Popped from the stack

    Enter your choice : 3

    Elements in the stack90 45 89 Enter your choice : 1

    Enter a Number : 1000Enter your choice : 3

    Elements in the stack1000 90 45 89Stack operation is stopped . . .

    QUEUE OPERATION : 1.ADD ELEMENT 2.REMOVE ELEMENT 3.DISPLAY4.LENGTH

    Enter your choice : 1

  • 7/30/2019 oops lab manual.doc

    40/59

    Enter Number : 67Enter your choice : 1

    Enter Number : 890

    Enter your choice : 1

    Enter Number : 244Enter your choice : 1

    Enter Number : 81Enter your choice : 1

    Enter Number : 39Enter your choice : 3

    Elements in the queue

    67 890 244 81 39 Enter your choice : 4

    Number of elements in the queue 5 Enter your choice : 267 is removed from the queueEnter your choice : 2

    890 is removed from the queueEnter your choice: 9

    Caught an exception 9

    RESULT:The program to design stack and queue classes with necessary

    exception handling successfully developed and executed.

    EX. NO. 8

    AIM:

  • 7/30/2019 oops lab manual.doc

    41/59

    To develop the method to find a minimum cost spanning tree in graph by

    using Kruskals Algorithm in graph class. Graph class is derived publicly from the

    point class and Arc class.

    ALGORITHM:1. Start the program, include required header files and constant using

    simple macro substitutions.

    2. Declare one global array by using one constant to denote number

    of values it will store.

    3. Declare the class point, declare one function wait( ) with no

    arguments and return value in public access specifier. Define inside

    the class itself.

    4. Close the point class.

    5. Declare the class arc, define one function makeset(int) with one

    integer argument and no return value in public access specifier.

    Define inside the class itself. Close the arc class.

    6. The class graph is publicly derived from the class point and arc.

    7. In this derived class, graph declare and define the fallowing

    methods i. void initial(int n), ii. int find(int i), iii. void merge(int p,int

    q), iv. int equal(int p,int q) and void testuniv(void) in public.

    8. Close the graph class, open the main( ) function.

    9. In main function declare and initialize one two dimensional integer

    array with constant inside brackets indicating rows and columns.

    10.Next declare other two, two dimensional integer array with

    constants indicating rows and give value 3 for indicating columns.

    11.Declare required variables, by using object of graph class call the

    required methods from point, arc and graph class since graph is

    derived publicly from two base class, point and arc.

  • 7/30/2019 oops lab manual.doc

    42/59

    12.Do the necessary coding as per kruskals algorithm to find minimum

    cost spanning tree.

    13.In coding show the set of edges before sorting and after sorting.

    14.Show all the disjoint subsets.

    15.After this, show Minimal Spanning Tree Edges.

    16.Finally show the Minimal Spanning Tree Weight.

    17.Close the program.

    PROGRAM:#include#include#include

  • 7/30/2019 oops lab manual.doc

    43/59

    #include#include#define N 6#define M 15int u[N];

    class point{public:

    void wait(void){int i;cout

  • 7/30/2019 oops lab manual.doc

    44/59

    {if(p

  • 7/30/2019 oops lab manual.doc

    45/59

    E[k][0]=i;E[k][1]=j;E[k][2]=w[i][j];k++;}

    cout

  • 7/30/2019 oops lab manual.doc

    46/59

    j=g.find(b);if(!g.equal(i,j)){g.merge(i,j);F[numedges][0]=E[nextedges][0];

    F[numedges][1]=E[nextedges][1];F[numedges][2]=E[nextedges][2];numedges++;g.testuniv();}nextedges++;}

    cout

  • 7/30/2019 oops lab manual.doc

    47/59

    0 4 30 5 21 2 11 3 11 4 4

    1 5 42 3 42 4 62 5 03 4 03 5 04 5 0

    Press Enter to continue . . .

    Set of edges - after sorting2 5 0

    3 4 03 5 04 5 00 3 11 2 11 3 10 1 20 5 20 4 30 2 41 4 4

    1 5 42 3 42 4 6

    Press Enter to continue . . .

    The disjoint subsets are : 0 1 2 3 4 5

    The disjoint subsets are : 0 1 2 3 4 2

    The disjoint subsets are : 0 1 2 3 3 2

    The disjoint subsets are : 0 1 2 2 3 2

  • 7/30/2019 oops lab manual.doc

    48/59

    The disjoint subsets are : 0 1 0 2 3 2

    The disjoint subsets are : 0 0 0 2 3 2

    Minimal Spanning Tree Edges:F =((V2,V5),(V3,V4),(V3,V5),(V0,V3),(V1,V2))Minimal Spanning Tree Weight =2

    RESULT:

    The program to develop the method to find a minimum cost spanning treein graph by using Kruskals Algorithm in graph class. Graph class is derived

    publicly from the point class and Arc class is developed, executed sucessfully.

    EX. NO. 9

    AIM:

  • 7/30/2019 oops lab manual.doc

    49/59

    To develop the program with suitable hierarchy, classes for point, shape,

    rectangle, square, circle, ellipse, triangle, polygon, etc. Design a simple test

    application to demonstrate dynamic polymorphism and RTTI.

    ALGORITHM:

    1. Open the program, include required header files.

    2. Create the base class shape, declare and define the virtual

    function void show( ) in public access specifier, this function

    prototype is using by all the intermediate base class and derived

    class. Close the base class.

    3. From the base class point, two derived class, shape, rectangle is

    publicly derived.

    4. In both this derived class void show( ) method is declared and

    defined.

    5. From the derived class shape, two other derived class, square, circle is

    publicly derived. So the derived class, shape becomes intermediate

    base class.

    6. In both this derived class, square, circle declare and define void

    show( ) method.7. From the derived class rectangle, two other derived class, ellipse,

    triangle is publicly derived. So the derived class, rectangle

    becomes intermediate base class.

    8. In both this derived class, ellipse, triangle declare and define

    void show( ) method.

    9. From the derived class square, two other derived class, polygon,

    oval is publicly derived. So the derived class, square becomes

    intermediate base class.

    10. In both this derived class, polygon, oval declare and define

  • 7/30/2019 oops lab manual.doc

    50/59

    void show( ) method.

    10.Open the main( ) function, declare object, pointer object of the

    base class point and declare object of all the intermediate base

    class and derived class.

    11.Assign the address of the object of base class to the base class

    pointer object. And show base class pointer object pointing which

    object by typeid operator with name( ) function. Call the show( )

    method by base class pointer object.

    12.Assign object address of the intermediate base class, derived

    class to the base class pointer object. And show base class

    pointer object pointing which object by typeid operator with

    name( ) function. Call the show( ) method by base class

    pointer object.

    14. Close the main( ) function. Stop the program.

    PROGRAM:

    #include#include#includeclass point

  • 7/30/2019 oops lab manual.doc

    51/59

    {public:

    virtual void show(){cout

  • 7/30/2019 oops lab manual.doc

    52/59

    public:void show(){cout

  • 7/30/2019 oops lab manual.doc

    53/59

    pointptr=&rt;cout

  • 7/30/2019 oops lab manual.doc

    54/59

    Shape is drawn

    Base class pointer object now pointing class rectangle

    Rectangle is drawn

    Base class pointer object now pointing class square

    Square is drawn

    Base class pointer object now pointing class circle

    Circle is drawn

    Base class pointer object now pointing class ellipse

    Ellipse is drawn

    Base class pointer object now pointing class triangle

    Triangle is drawn

    Base class pointer object now pointing class polygon

    Polygon is drawn

    Base class pointer object now pointing class oval

    Oval is drawn

    RESULT:Thus the program with suitable hierarchy, classes for point, shape,

    rectangle, square, circle, ellipse, triangle, polygon, etc. Design a simple test

    application to demonstrate dynamic polymorphism and RTTI is successfully

    developed and executed.

    EX. NO. 10

    AIM:To develop a program for reading complex numbers and writes them two

    per line in a file along with an operator. Read the complex numbers from the file

    and perform the corresponding operations.

  • 7/30/2019 oops lab manual.doc

    55/59

    ALGORITHM:

    1. Open the program, include required header files.

    2. Declare the class, complex. In public access specifier declare two

    float variables real, imag. Declare and define the default

    constructor.

    3. After the default constructor, declare and define write method, this

    method as output file stream class object reference as argument

    and no return type.

    4. Next declare and define read method, this as input file stream class

    object reference as argument, integer value as a return type.

    5. Next declare operator function, operator> as friend function with

    two argument one is reference of object of input file stream class,

    another one is reference of the object of the complex class. And itreturns reference of object of input file stream class. Define this

    method outside the class.

    7. Next declare and define another operator function, operator> as

    friend function with two argument one is reference of object of input

  • 7/30/2019 oops lab manual.doc

    56/59

    file stream class, another one is reference of the object of the

    complex class. And it returns reference of object of input file stream

    class.

    9. Next declare Add function as friend function. With two arguments

    and no return value. The two arguments are object of the class

    complex. Define this function outside the class. Close the class.

    10.Open the main function, declare variables, objects required. Create

    the file complex.txt with constructor of output file stream class.

    11.Enter two complex numbers into the file by using do-while loop.

    12.Close the file.

    13.Get the complex numbers given to the file complex.txt by opening

    the file by input file stream class.

    14.Call the add( ) function with two objects as argument of the

    complex class to do the addition of the two complex numbers.

    15.Close the program.

    PROGRAM:

    #include#include#includeclass complex{public:

  • 7/30/2019 oops lab manual.doc

    57/59

    float real,imag;complex(){real=0;imag=0;}

    void write(ofstream &os){os.write((char *)&real,sizeof(real));os.write((char *)&imag,sizeof(imag));}int read(ifstream &is){is.read((char *)&real,sizeof(real));is.read((char *)&imag,sizeof(imag));return is.good();}

    friend ostream &operator(istream &is, complex &c);friend ofstream &operator(ifstream &fos,complex &c){c.read(fos);return fos;

    }friend void add(complex,complex)};

    istream &operator>>(istream &is, complex &c){coutws>>c.real;coutws>>c.imag;return is;}ostream &operator

  • 7/30/2019 oops lab manual.doc

    58/59

    return os}void add(complex c1,complex c2){cout

  • 7/30/2019 oops lab manual.doc

    59/59

    }

    OUTPUT:Enter 2 complex numbersReal? 1.3Imag?: 1.4Real? 4.3Imag?: 2.3

    objects written to the file were1.3 +i 1.4 4.3 +i 2.3Result is

    5.6 +i 3.7

    RESULT:The program for reading complex numbers and writes them two per line in a

    file along with an operator. Read the complex numbers from the file and perform

    the corresponding operations is successfully developed, executed.