PLC Anupam Samanta 2010JE0976

download PLC Anupam Samanta 2010JE0976

of 64

Transcript of PLC Anupam Samanta 2010JE0976

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    1/64

    COMPUTER SCIENCE AND ENGINNERING

    SOME RANDOM LAB

    ASSIGNMENTS

    MIGHT HELP YOU

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    2/64

    1. Write a C program to take Elements from a 3x3 matrix from a user and print

    I) Sum of the Diagonal matrix

    II) Sum of the lower triangle ElementsIII) Sum of the upper triangle Elements

    //PROGRAM TO SUM DIAGONAL ELEMENTS, UPPER TRIAGLE AND LOWER TRIANGLE

    ELEMENTS OF //A MATRIX

    #include

    int matrixDiagonal(int a[10][10],int);

    int lowerTriangle(int a[10][10],int);

    int upperTriangle(int a[10][10],int);

    int main()

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    3/64

    {

    int a[10][10];int choice;

    int size;int i,j;

    do

    {printf("\nMATRIX OPERATION PROGRAM");printf("\n1.Enter the matrix ");

    printf("\n2.Sum of diagonal elements ");

    printf("\n3.Sum of Upper triangle elements ");printf("\n4.Sum of Lower Triangle elements ");

    printf("\n0.Exit\nChoice: ");scanf("%d",&choice);

    switch(choice){

    case 1: printf("Enter the size of the matrix (maximum 10): ");scanf("%d",&size);

    printf("\nEnter the Matrix: \n");

    for(i=0;i

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    4/64

    int matrixDiagonal(int a[10][10],int size){

    int i;int sum=0;

    for(i=0;i

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    5/64

    Choice: 1

    Enter the size of the matrix (maximum 10): 3

    Enter the Matrix:Row 1 : 1 2 3

    Row 2 : 4 5 6

    Row 3 : 7 8 9

    MATRIX OPERATION PROGRAM1.Enter the matrix

    2.Sum of diagonal elements

    3.Sum of Upper triangle elements4.Sum of Lower Triangle elements

    0.ExitChoice: 2

    Sum of Diagonal elements : 15

    MATRIX OPERATION PROGRAM1.Enter the matrix

    2.Sum of diagonal elements

    3.Sum of Upper triangle elements4.Sum of Lower Triangle elements

    0.ExitChoice: 3

    Sum of Lower Triangle elements : 19

    MATRIX OPERATION PROGRAM1.Enter the matrix2.Sum of diagonal elements

    3.Sum of Upper triangle elements4.Sum of Lower Triangle elements

    0.Exit

    Choice: 4

    Sum if Upper Triangle elements : 11

    MATRIX OPERATION PROGRAM

    1.Enter the matrix2.Sum of diagonal elements

    3.Sum of Upper triangle elements4.Sum of Lower Triangle elements

    0.Exit

    Choice: 0

    THANK YOU

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    6/64

    2. Write a C program to take a number from user and tell whether it is a palindrome or not.

    //PROGRAMM TO CHECK WHETHER A NUMBER IS PALINDROME OR NOT

    #include

    void palindrome(int);

    int main(){

    int number;

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    7/64

    int choice;

    do{

    printf("\nPALINDROME CHECKING PROGRAM");

    printf("\n1.Enter a Number\n0.Exit \nchoice: ");

    scanf("%d",&choice);

    switch(choice)

    {

    case 1: printf("\nEnter the number: ");scanf("%d",&number);

    palindrome(number);

    break;

    case 0: printf("\nTHANK YOU\n");break;

    default:printf("\nInvalid Choice, please re-enter\n\n");break;

    }

    }while(choice);

    return 0;

    }

    void palindrome(int x)

    {

    int newNum=0,counter=0;int temp=x,powTen=1;

    int i;

    while(temp){

    temp=temp/10;counter++;

    }

    temp=x;

    for(i=1;i

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    8/64

    while(temp){

    newNum+=((temp%10)*powTen);

    temp=temp/10;

    powTen/=10;

    }

    if(x==newNum){

    printf("\nThis Number is a palindrome\n");}

    else

    {printf("\nThis number is not a Palindrome\n");

    }}

    OUTPUT:

    PALINDROME CHECKING PROGRAM1.Enter a Number

    0.Exitchoice: 1

    Enter the number: 12421

    This Number is a palindrome

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    9/64

    PALINDROME CHECKING PROGRAM

    1.Enter a Number0.Exit

    choice: 1

    Enter the number: 21332

    This number is not a Palindrome

    PALINDROME CHECKING PROGRAM

    1.Enter a Number

    0.Exitchoice: 0

    THANK YOU

    3. Write a program in C to take a string and print whether it is a Palindrome or not.

    //PROGRAM TO CHECK WHETHER A STRING IS PALINDROME OR NOT

    #include

    #include

    void palindrome();

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    10/64

    int main()

    {

    int choice;

    do

    {

    printf("\nPALINDROME CHECKING PROGRAM");printf("\n1.Enter a String\n0.Exit \nchoice: ");scanf("%d",&choice);

    switch(choice){

    case 1: palindrome();

    break;

    case 0: printf("\nTHANK YOU\n");

    break;

    default:printf("\nInvalid Choice, please re-enter\n\n");

    break;}

    }while(choice);

    return 0;

    }

    void palindrome(){

    int counter=0;

    int i;

    char a[10]="\0",b[10]="\0";

    printf("\nEnter the string: ");

    scanf("%s",a);

    counter=strlen(a);

    for(i=0;i

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    11/64

    else

    {printf("\nIt is not a palindrome\n");

    }

    }

    OUTPUT:PALINDROME CHECKING PROGRAM

    1.Enter a String0.Exit

    choice: 1

    Enter the string: malayalam

    It is a palindrome

    PALINDROME CHECKING PROGRAM1.Enter a String

    0.Exitchoice: 1

    Enter the string: computer

    It is not a palindrome

    PALINDROME CHECKING PROGRAM

    1.Enter a String0.Exit

    choice: 0

    THANK YOU

    4. Write a program in C using structure and union to store the information of an employee of anorganisation

    The details of the employees should be in the format of:

    NameAge

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    12/64

    Grade

    if Grade =1

    then ask for Card No and hobby

    if Grade = 2

    then ask for Vehicle No and Distance from the office

    //A PROGRAM TO STORE INFORMATION ABOUT AN EMPLOYEE//USING STRUCTURE AND UNION

    #include

    struct employee{

    char name[10];

    int age;int grade;

    union grade0{

    struct grade1

    {int cardNo;

    char hobby[10];}x;

    struct grade2{

    int vehicleNo;float distOffice;}y;

    }z;

    };

    typedef struct employee employee;

    int main(){

    employee emp[10];int choice;

    int i,num;

    do{

    printf("\n\nREAD AND WRITE FILES\n");printf("\n1.Enter information\n2.Read information");

    printf("\n0Exit\nChoice:");

    scanf("%d",&choice);

    switch(choice){

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    13/64

    case 1: printf("\nEnter the number of employee: ");

    scanf("%d",&num);

    for(i=0;i

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    14/64

    break;

    default:printf("\nInvalid choice");

    break;}

    }while(choice);

    return 0;}

    OUTPUT:

    READ AND WRITE FILES

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    15/64

    1.Enter information

    2.Read information0.Exit

    Choice:1

    Enter the number of employee: 3

    Employee 1:

    Name: John

    Age: 29

    Grade: 1

    Card No: 12332Hobby: Philately

    Employee 2:

    Name: SueAge: 26

    Grade: 2

    Vehicle No: 18293

    Distance from office: 12.00

    Employee 3:

    Name: Amy

    Age: 34Grade: 2

    Vehicle No: 12332Distance from office: 1.05

    READ AND WRITE FILES

    1.Enter information

    2.Read information

    0.ExitChoice:2

    Employee 1:

    Name: John

    Age: 29Grade: 1

    Card No: 12332

    Hobby: Philately

    Employee 2:

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    16/64

    Name: Sue

    Age: 26Grade: 2

    Vehicle No: 18293Distance from office: 12.000000

    Employee 3:

    Name: AmyAge: 34

    Grade: 2

    Vehicle No: 12332Distance from office: 1.050000

    READ AND WRITE FILES

    1.Enter information2.Read information

    0.ExitChoice:0

    THANK YOU

    5. Write a program in C to either take an integer and or two characters from the userIf the user enters an integer then print 2 characters whose ascii value is obtained

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    17/64

    by dividing the binary equivalent of the number in 8 bits and 8 bits.

    Else if the user enters 2 characters then print the integer formed by binary equivalent

    combination of ascii value of 2 characters.

    #include

    #include

    int main(){

    int choice,integer;

    int d,k,c=0,a[16];int binEq1=0,binEq2=0;

    int g[16],e,p,v,x,u=0,n1=0,n2=0,i;char ch1,ch2;

    do

    {printf("\nWELCOME");

    printf("\n1.Enter Integers\n2.Enter 2 characters\n0.Exit\nChoice: ");

    scanf("%d",&choice);

    if(choice!=1&&choice!=2&&choice){

    printf("Invalid input.");

    }

    switch(choice){case 1: printf("Enter the integer:");

    scanf("%d",&integer);k=integer;

    d=integer;

    while(k>0){

    k=k/2;c++;

    }

    for(i=0;i

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    18/64

    printf("%c\n",binEq1);

    for(i=8;i0){

    e=e/2;

    n1++;}

    while(x>0)

    {x=x/2;

    n2++;

    }

    for(i=0;i

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    19/64

    for(i=(8+n2);i

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    20/64

    6. Write a menu driven program in C to: Open a file in 'w' mode

    Write data in fileOpen file in read mode and print the contents of the file

    #include

    int main(){

    FILE *file;

    int counter=0;int choice;

    char fileName[20];char string[50];

    char c;

    do{

    printf("\nREAD AND WRITE FILES\n");printf("\n1.Read a file\n2.Write a file\nChoice:");

    scanf("%d",&choice);

    switch(choice)

    {case 1: printf("\nName of File : ");

    scanf("%s",fileName);

    file=fopen(fileName,"r");c=fgetc(file);

    do{printf("%c",c);

    c=fgetc(file);}while(c!=EOF);

    fclose(file);break;

    case 2: printf("\nName of File : ");

    scanf("%s",fileName);

    file=fopen(fileName,"w");

    printf("\nEnter the string to be written:\n ");

    scanf("%s",string);

    counter=0;while(string[counter]!='\0')

    {

    putc(string[counter],file);

    counter++;}fclose(file);

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    21/64

    break;

    case 0: printf("\nTHANK YOU\n");

    break;

    default:printf("\nInvalid choice");break;

    }

    }while(choice);

    return 0;

    }

    OUTPUT:

    READ AND WRITE FILES

    1.Read a file

    2.Write a fileChoice:2

    Name of File : abc.txt

    Enter the string to be written:hello

    READ AND WRITE FILES

    1.Read a file2.Write a file

    Choice:1

    Name of File : abc.txt

    helloREAD AND WRITE FILES

    1.Read a file2.Write a file

    Choice:0

    THANK YOU

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    22/64

    7. Write a program to count the number of lines and characters in a file.

    #include

    void count();

    int main()

    {

    int choice;

    do

    {printf("\nCOUNT THE NUMBER OF LINES IN A FILE\n");

    printf("\n1.Count the number of lines\n0.Exit\nChoice:");scanf("%d",&choice);

    switch(choice){

    case 1: count();break;

    case 0: printf("\nTHANK YOU\n");break;

    default:printf("\nInvalid choice");break;

    }}while(choice);

    return 0;}

    void count()

    {int counter=0,character=0;

    FILE *file;

    char c;

    char fileName[25];

    printf("\nName of File : ");scanf("%s",fileName);

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    23/64

    file=fopen(fileName,"r");

    c=fgetc(file);

    do{

    c=fgetc(file);

    character++;

    if(c=='\n'){

    counter++;

    }

    }while(c!=EOF);

    printf("\nThe Number of newlines: %d and characters in the program: %d \n",counter,character);

    fclose(file);

    }

    OUTPUT:

    COUNT THE NUMBER OF LINES IN A FILE

    1.Count the number of lines0.Exit

    Choice:1

    Name of File : def.txt

    The Number of newlines: 5 and characters in the program: 49

    COUNT THE NUMBER OF LINES IN A FILE

    1.Count the number of lines0.Exit

    Choice:0

    THANK YOU

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    24/64

    8. Write a Program in C to compare two files

    #include

    void compare();

    int main()

    {

    int choice;

    do

    {printf("\nCOMPARE TWO FILES\n");

    printf("\n1.Compare two files\n0.Exit\nChoice:");

    scanf("%d",&choice);

    switch(choice){

    case 1: compare();

    break;

    case 0: printf("\nTHANK YOU\n");break;

    default:printf("\nInvalid choice");break;

    }

    }while(choice);

    return 0;}

    void compare()

    {int flag;

    FILE *file1,*file2;

    char c1,c2;

    char fileName1[25],fileName2[25];

    printf("\nName of File 1: ");

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    25/64

    scanf("%s",fileName1);

    printf("\nName of File 2: ");

    scanf("%s",fileName2);

    file1=fopen(fileName1,"r");file2=fopen(fileName2,"r");

    if(file1==NULL||file2==NULL){

    printf("\nFiles not found");fclose(file1);

    fclose(file2);

    return;

    }

    c1=fgetc(file1);

    c2=fgetc(file2);

    do{

    c1=fgetc(file1);

    c2=fgetc(file2);

    if(c1!=c2){

    flag=0;

    break;

    }

    }while(c1!=EOF&&c2!=EOF);

    if(!flag)

    {printf("\nThe Files are not same\n");

    }else

    {

    printf("\nThe Files are same\n");}

    fclose(file1);

    fclose(file2);

    }

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    26/64

    OUTPUT:COMPARE TWO FILES

    1.Compare two files

    0.ExitChoice:1

    Name of File 1: abc.txt

    Name of File 2: def.txt

    The Files are not same

    COMPARE TWO FILES

    1.Compare two files

    0.ExitChoice:1

    Name of File 1: abc.txt

    Name of File 2: abc.txt

    The Files are same

    COMPARE TWO FILES

    1.Compare two files0.Exit

    Choice:0

    THANK YOU

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    27/64

    9. Write a program in C++ to show the friendship of a member functions of another class.

    //A PROGRAM TO SHOW FRIENDSHIP OF TWO CLASSES

    #include

    using namespace std;

    class second;

    class first{

    int i;

    public:

    void input();void output();

    void swap(second&);

    };

    class second{int j;

    public:

    void getdata(){

    coutj;

    }

    void showdata()

    {cout

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    28/64

    }

    void first::output()

    {cout

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    29/64

    default:cout

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    30/64

    Choice: 2

    First class

    i: 21

    Second Class:

    j: 12

    FRIENDSHIP OF MEMBER FUNCTIONS

    1.Input data

    2.Print data3.Swap data

    0.ExitChoice: 0

    Thank You

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    31/64

    10. Write a program in C++ to merge 2 arrays using friend function.

    //PROGRAMM TO MERGE TWO ARRAYS USING FRIEND FUNCTION

    #includeusing namespace std;

    class array2;

    class array1

    {int a[10];

    int m;

    public:

    void getarray1(){coutm;cout

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    32/64

    coutn;cout

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    33/64

    cout

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    34/64

    11. Write a program in C++ to show the significance of static and local variable

    #include

    using namespace std;

    class ABC

    {int a;

    static int b;public:

    void getdata()

    {cin>>a;

    b++;}void printdata()

    {cout

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    35/64

    p.printdata();

    cout

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    36/64

    12. Write a program to show the friendship of a non member function by doing sum of two numbers

    #include

    using namespace std;

    class me

    {int a;

    public:

    void getdata()

    { couta;

    }void print()

    {

    cout

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    37/64

    coutb;look.getdata();

    swapping(b,look);look.print();

    cout

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    38/64

    cin>>edge;

    coutheight;

    cout

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    39/64

    OUTPUT:

    PROGRAM TO IMPLEMENT FUNCTION OVERLOADING

    1.Calculate volume of cube

    2.Calculate volume of cuboid3.Calculate volume of sphere

    0.Exit

    Choice: 1

    Enter length of side of cube.12

    Volume: 1728

    PROGRAM TO IMPLEMENT FUNCTION OVERLOADING

    1.Calculate volume of cube2.Calculate volume of cuboid

    3.Calculate volume of sphere

    0.ExitChoice: 2

    Enter length,width,height of cuboid.

    12 3 4

    Volume: 144

    PROGRAM TO IMPLEMENT FUNCTION OVERLOADING

    1.Calculate volume of cube2.Calculate volume of cuboid3.Calculate volume of sphere

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    40/64

    0.Exit

    Choice: 3

    Enter the radius length.21

    Volume: 29079.5

    PROGRAM TO IMPLEMENT FUNCTION OVERLOADING

    1.Calculate volume of cube

    2.Calculate volume of cuboid3.Calculate volume of sphere

    0.ExitChoice: 0

    Thank You

    14. Write a program in C++ to perform operator overloading in 3 cases: '+' to add 2 complex numbers

    '-' to subtract 2 complex numbers '*' to multiply 2 complex Numbers

    #include

    using namespace std;

    class complex{

    float x;

    float y;public:

    complex(){

    }

    complex( float real,float imag){

    x=real;y=imag;

    }

    complex operator+(complex );complex operator-(complex);

    complex operator*(complex);void getdata();

    void display();

    };

    complex complex::operator+(complex c){

    complex temp;

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    41/64

    temp.x=x+c.x;

    temp.y=y+c.y;return(temp);

    }complex complex::operator-(complex c1)

    {

    complex temp;

    temp.x=x-c1.x;temp.y=y-c1.y;return(temp);

    }

    complex complex::operator*(complex c2){

    complex temp;temp.x=(((x*(c2.x))-(y*(c2.y))));

    temp.y=(((x*(c2.y))+(y*(c2.x))));

    return(temp);}

    void complex::getdata(){

    coutx;couty;}

    void complex::display()

    {cout

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    42/64

    break;

    case 2: cout

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    43/64

    OUTPUT:ADDITION SUBTRACTION AND MULTIPLICATION OF COMPLEX NUMBERS

    1.Add

    2.Subtract3.Multiply

    0.ExitChoice: 1

    Enter two complex numbers1st complex number:

    Real part:12

    Imaginary part:12

    2nd complex number:Real part:21

    Imaginary part:2

    Reuslt: 33+(14)i

    ADDITION SUBTRACTION AND MULTIPLICATION OF COMPLEX NUMBERS

    1.Add

    2.Subtract3.Multiply

    0.ExitChoice: 2

    Enter two complex numbers

    1st complex number:Real part:21Imaginary part:21

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    44/64

    2nd complex number:Real part:22

    Imaginary part:3

    Reuslt: -1+(18)i

    ADDITION SUBTRACTION AND MULTIPLICATION OF COMPLEX NUMBERS1.Add2.Subtract

    3.Multiply

    0.ExitChoice: 3

    Enter two complex numbers

    1st complex number:

    Real part:2Imaginary part:2

    2nd complex number:

    Real part:1

    Imaginary part:2

    Reuslt: 1+(0)i

    ADDITION SUBTRACTION AND MULTIPLICATION OF COMPLEX NUMBERS

    1.Add2.Subtract

    3.Multiply0.ExitChoice: 0

    THANK YOU

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    45/64

    15. Write a program in C++ to illustrate use of constructor & destructor by constructing and destructingthat will increase or decrease count.

    #include

    using namespace std;

    class cons_dest{

    static int count;public:

    cons_dest()

    {count++;

    cout

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    46/64

    int cons_dest::count=0;

    int main()

    {

    cons_dest obj1, obj2;cout

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    47/64

    16. Write a program in C++ to add 2 numbers by passing object as an argument.

    #include

    using namespace std;

    class data

    {public:

    int i;void getdata()

    {

    cin>>i;}

    };

    class cons_addition

    {public:

    cons_addition(data a,data b){

    int sum;

    sum = a.i + b.i;

    cout

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    48/64

    int main(){

    data obj1, obj2;cout

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    49/64

    class polar{

    float r;float angle;

    public:

    polar(cartesian c)

    {r = sqrt((c.getx()*c.getx())+(c.gety()*c.gety()));angle = atan((c.gety())/(c.getx()));

    cout

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    50/64

    18. Write a program in C++ to implement multilevel inheritance

    #include

    using namespace std;

    class student

    {char name[10];

    int adm_no;public:

    void getinfo()

    {coutname;coutadm_no;

    }

    void showinfo(){

    cout

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    51/64

    cout

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    52/64

    OUTPUT:

    PROGRAM FOR MULTILEVEL INHERITANCE

    Student 1:Enter name:John

    Enter admission no.:12122

    Enter marks: 12

    Enter degree: cbse

    Student 2:Enter name:amy

    Enter admission no.:12223

    Enter marks: 121

    Enter degree: btech

    Student 3:

    Enter name:sue

    Enter admission no.:12221

    Enter marks: 140

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    53/64

    Enter degree: icse

    Student 1:

    Name of student is JohnAdmission no. is 12122Marks are 12

    Degree is cbse

    Student 2:

    Name of student is amyAdmission no. is 12223Marks are 121Degree is btech

    Student 3:

    Name of student is sueAdmission no. is 12221Marks are 140

    Degree is icse

    19. Write a program in C++ to implement Hybrid Inheritance

    //.....Header files.....

    #include

    using namespace std;

    //...class declaration.....

    class person{

    private:char name[20];

    int code;

    public:

    void getdata(){

    cout

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    54/64

    cin>>name;

    coutcode;

    }void showdata()

    {

    cout

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    55/64

    void get()

    {cout

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    56/64

    20. Write a program in C++ using generic function to perform swapping of different datatype

    #include

    using namespace std;

    templatevoid swapping(T &x,T &y)

    {T temp;

    temp=x;

    x=y;y=temp;

    }

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    57/64

    int main(){

    int choice;

    int a,b;

    float c,d;char e,f;

    do{

    cout

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    58/64

    break;

    }

    }while(choice);

    return 0;

    }

    OUTPUT:

    GENERIC FUNCTION FOR SWAPPING1.Swap 2 integers

    2.Swap 2 Real numbers

    3.Swap 2 Characters0.Exit

    Choice: 1

    First Integer: 12Second Integer: 21

    After swapping:

    First Integer: 21

    Second Integer: 12

    GENERIC FUNCTION FOR SWAPPING1.Swap 2 integers

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    59/64

    2.Swap 2 Real numbers

    3.Swap 2 Characters0.Exit

    Choice: 2

    First number: 12.08

    Second number: 0.02

    After swapping:

    First Number: 0.02

    Second Number: 12.08

    GENERIC FUNCTION FOR SWAPPING1.Swap 2 integers

    2.Swap 2 Real numbers

    3.Swap 2 Characters0.Exit

    Choice: 3

    First Character: a

    Second Character: d

    After swapping:First Character: d

    Second Character: a

    GENERIC FUNCTION FOR SWAPPING

    1.Swap 2 integers2.Swap 2 Real numbers3.Swap 2 Characters

    0.ExitChoice: 0

    THANK YOU

    21. Write a function in C++ to implement generic class and perform addition of two complex numbers

    #include

    using namespace std;

    template

    class complexNumbers

    {

    T real,imaginary;

    public:

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    60/64

    void complexInput(){

    coutreal;

    coutimaginary;

    }

    void complexOutput()

    {

    cout

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    61/64

    switch(choice)

    {case 1: cout

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    62/64

    Enter imaginary part:-9

    The complex number is :34 + i 12

    OPERATIONS OF COMPLEX NUMBERS

    1.Add two integer type complex numbers

    2.Add 2 real type complex numbers

    0.ExitChoice: 2

    Complex number 1:

    Enter real part: 31.99Enter imaginary part:12.02

    Complex number 2:

    Enter real part: 21.09

    Enter imaginary part:-2.21

    The complex number is :53.08 + i 9.81

    OPERATIONS OF COMPLEX NUMBERS

    1.Add two integer type complex numbers2.Add 2 real type complex numbers

    0.ExitChoice: 0

    Thank You

    22. Write a program in C++ to implement virtual function

    #include

    using namespace std;

    class window{

    public:virtual void create()

    {

    cout

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    63/64

    class command_button:public window

    {public:

    void create(){

    cout

  • 8/3/2019 PLC Anupam Samanta 2010JE0976

    64/64

    couty;

    if(y==0){

    throw(z);

    }

    else{

    z=x/y;

    cout