CS2312-c++ lab

download CS2312-c++ lab

of 35

Transcript of CS2312-c++ lab

  • 8/12/2019 CS2312-c++ lab

    1/35

    1

    M.A.M SCHOOL OF ENGINEERING

    Siruganur , Tiruchirappalli.

    Department of Computer Science and EngineeringCS1313-Object Oriented Programming Laboratory

    LAB MANUAL

    M.A.M.SCHOOL OF ENGINEERING

    Siruganur Tiruchirappalli.

  • 8/12/2019 CS2312-c++ lab

    2/35

    2

    COMPUTER SCIENCE ENGINEERING LABORATORY

    CERTIFICATE

    Certified that this is the bonafide record of practicals done by

    Selvan / Selvi. _________________________________

    Reg.No.______________ of First Year B.E., during the

    academic Year 2008 2009.

    Station:

    Date: HOD Staffin - Charge

    Submitted for the university practical Examination held on

    Date: _________ Internal Examiner External Examiner

  • 8/12/2019 CS2312-c++ lab

    3/35

    3

    CS1313 OBJECT ORIENTED PROGRAMMING LABORATORY( Common to EEE, EIE and ICE)

    L T P C

    0 0 3 2

    1. Programs Using Functions- Functions with Default Arguments

    - Implementation of Call by Value, Call by Address

    2. Simple Classes for understanding objects, member functions and Constructors

    - Classes with Primitive Data Members

    - Classes with Arrays as Data Members

    - Classes with Pointers as Data MembersString Class- Classes with Constant Data Members

    - Classes with Static Member Functions

    3. Compile Time Polymorphism- Operator Overloading including Unary and Binary Operators

    - Function Overloading

    4. Runtime Polymorphism

    - Inheritance

    - Virtual Functions- Virtual Base Classes

    - Templates

    5. File Handling

    - Sequential Access- Random Access

    Total: 45

  • 8/12/2019 CS2312-c++ lab

    4/35

    4

    INDEX

    EXPT

    NO

    DATE NAME OF THE EXPERIMENT PAGE

    NO

    MARKS

    OBTAINED

    SIGNATURE

    1 Function with default argument

    2A Call By Value

    2B Call By Reference

    2C Call By Address

    3A Classes and Objects

    3B Creating marklist using array

    3C String Manipulation

    4A Static Member function

    4B Constant Member Function

    5A Unary Operator overloading

    5B Binary Operator overloading

    6 Function overloading

    7A Single Inheritance

    7B Multiple Inheritance

    8A virtual function

    8B virtual base class

    9A Function Template

    9B Class Template

    10A Sequential File Access

    10B Random File Access

  • 8/12/2019 CS2312-c++ lab

    5/35

    5

    1.Function with default argumentEx No:1

    AIM:To write a c++ program for function with default arguments

    ALGORITHM:

    1.

    Start the program.

    2. Declare all the header file.

    3. Declare printline function and set default values for arguments.4. Define the printline function.

    5. printline function will print character n times.

    6. call the function within main().7. stop the execution.

    PROGRAM:

    #include

    #includevoid printline(char='_',int=70);

    void main(){

    clrscr();

    printline();printline('!');

    printline('*',40);

    printline('r',55);

    getch();}

    void printline(char ch,int count){int i;

    cout

  • 8/12/2019 CS2312-c++ lab

    6/35

    6

    Ex No:2A

    Call By Value

    AIM:To write a c++ program to illustrate the concept of call by value.

    ALGORITHM:1. Declare necessary variables.

    2. Get the values for swapping.3. Define a function which swaps value that are passed as arguments to itusing temporary

    variable.

    4. Invoke that function by passing the given values

    PROGRAM:

    #include#include

    void swap(int x,int y){

    int t;

    cout

  • 8/12/2019 CS2312-c++ lab

    7/35

    7

    Ex No:2B

    Call By Reference

    AIM:To write a c++ program to illustrate the concept of call by reference.

    ALGORITHM:

    1. Declare necessary variables.2. Get the values for swapping.

    3. Define a function which swaps value using their reference which are passed as

    arguments to it .

    4. Invoke that function with or without arguments.

    PROGRAM:#include

    #includevoid swap(int &x,int &y)

    {

    int t;

    t=x;x=y;

    y=t;

    }void main()

    {

    clrscr();

    int a,b;cout>a>>b;

    swap(a,b);cout

  • 8/12/2019 CS2312-c++ lab

    8/35

    8

    Ex No:2C

    Call By Address

    AIM:To write a c++ program to illustrate the concept of call by address.

    ALGORITHM:1. Declare necessary variables.

    2. Get the values for swapping.

    3. Define a function which swaps address by using temporary variable t.

    4. Invoke that function by passing address of variable.

    PROGRAM:#include

    #includevoid swap(int*x,int*y)

    {

    int t;t=*x;

    *x=*y;

    *y=t;

    }void main()

    {int a,b;clrscr();

    couta>>b;swap(&a,&b);

    cout

  • 8/12/2019 CS2312-c++ lab

    9/35

    9

    Ex No:3A

    Classes and Objects

    AIM:

    To write a c++ program to illustrate the concept of Classes and objects.

    ALGORITHM:

    1. Declare all the header files.2. Declare the class and define all the data types.

    3. Define sub function of getdata( ) and putdata( ).

    4. Define main() function, declare the values for getdata( ).

    5. Obtain Output and display using putdata( ).6. Stop the execution.

    PROGRAM:

    #include

    #includeclass item

    {

    int number;float cost;

    public:

    void getdata (int a,float b);void putdata()

    {

    cout

  • 8/12/2019 CS2312-c++ lab

    10/35

    10

    Ex No:3BCreating marklist using array

    AIM:To create a mark list using arrays in c++ programming language.

    ALGORITHM:1.

    Declare all the header files.

    2. Declare the class and define the data types with arrays.

    3. Define the functions and get the details.4. Create student mark list.

    PROGRAM:

    #include

    #include

    class student

    {char name[20];

    int i,m;int a[5],tot;

    float avg;public:

    void get();

    void cal();};

    void student::get()

    {

    coutname;

    coutm;cout

  • 8/12/2019 CS2312-c++ lab

    11/35

    11

    getch();}

    OUTPUT:

    give the nameisac

    give no. of subjects5

    enter the marks:8987

    84

    8490

    tot=434

    avg=86

    RESULT:

    Thus the C++ program for creating mark list using arrays was executed and output verified.

    Ex No:3C

    String ManipulationAIM:

    To write a c++ program to perform operation on string class.

    ALGORITHM:

    1. Create a string class with string as its data members and getdata(), display(),

    string length, string copy as its member functions.2. getdata() member function is used to get the values of the string.

    3.

    display() member function is used to display the string.4. string length() member function calculate the length of the string by inspecting its

    characters one by one till NULL(\0).5. string copy() member function copies the string value which is passed as an

    argument to the invoking object.

    PROGRAM:

    #include

    #include

    #includeclass str

    {private:char str[40];

    char*n;

    public:void get();

    void disp();

    int strlen();

  • 8/12/2019 CS2312-c++ lab

    12/35

    12

    void strcpy(char*n);};

    void str::get()

    {

    coutstr;

    }

    void str::disp(){

    cout

  • 8/12/2019 CS2312-c++ lab

    13/35

    13

    RESULT:

    Thus the C++ program for string manipulation was executed and output verified.

    Ex No:4A

    Static Member function

    AIM:To write a c++ program to implement static member function.

    ALGORITHM:1. Create a class with static data member and static member function.

    2. Static member function only accesses the static data members.

    3. Static member function displays the static data member (path of the directory).

    4. Invoke that function with object.PROGRAM:

    #include#include

    #includeclass directory

    {

    public:static char path[];

    static void spath(char const*newpath);

    };

    void directory::spath(char const*newpath){ strcpy(path,newpath);}

    char directory::path[199]="/user/ashok";void main(){

    clrscr();

    cout

  • 8/12/2019 CS2312-c++ lab

    14/35

    14

    Ex No:4B

    Constant Member Function

    AIM:

    To write a c++ program to display the details of a person using constant member function.

    ALGORITHM:1. Create a class person with name, address and phone as pointer variable in the

    private section and constant function to get and display the details.2. Gets the name of the person using constant function.

    3. Gets the address of the person using get address function.

    4. Get the phone of the person using get phone function.5. Print the details using print function.

    6. Invoke that function with creating object.

    PROGRAM:#include

    #include#include

    class person{

    char *name;

    char *add;char *ph;

    public:

    void init();

    void clear();void sname(char const *str);

    void sphone(char const *str);void sadd(char const *str);char const *gname(void)const;

    char const *gadd(void)const;

    char const *gphone(void)const;};

    inline void person::init()

    {

    name=add=ph=0;}

    inline void person::clear()

    {delete name;delete add;

    delete ph;

    }void person ::sname(char const *str)

    {

    if (name)

  • 8/12/2019 CS2312-c++ lab

    15/35

    15

    delete name;name=new char[strlen (str)+1];

    strcpy(name,str);

    }

    void person::sadd(char const *str){

    if(add)

    delete add;add=new char[strlen(str)+1];

    strcpy(add,str);

    }void person::sphone(char const *str)

    {

    if(ph)

    delete add;ph=new char[strlen(str)+1];

    strcpy(ph,str);

    }

    inline char const *person::gphone()const{return ph;

    }

    inline char const *person::gadd()const{return add;

    }

    inline char const *person::gname()const{

    return name;

    }

    void print(person const &p)

    {if(p.gname())

    cout

  • 8/12/2019 CS2312-c++ lab

    16/35

    16

    p2.sadd("madurai");p2.sphone("9239232090");

    print(p2);

    p1.clear();

    p2.clear();getch();

    }

    OUTPUT:

    name:isac

    address:theni

    ph:no:923942090

    name:newton

    address:madurai

    ph:no:9239232090

    RESULT:

    Thus the program to implement using contant member function was executed and output verified.

    Ex No:5A

    Unary Operator overloading

    AIM:To write a c++ program to illustrate the concept of unary operator overloading.

    ALGORITHM:1. Create a class space which has the data member a, b, c.

    2. Declare the operator function by void operator-( ).

    3. Define operator function which is used to change the sign of data members.4. Invoke that operator function inside the main().

    PROGRAM:

    #include#include

    class s

    {int a,b,c;public:

    void getdata(int x,int y, int z);

    void display(void);void operator-();

    };

    void s::getdata(int x,int y,int z)

  • 8/12/2019 CS2312-c++ lab

    17/35

    17

    {a=x;b=y;c=z;

    }

    void s::display()

    {cout

  • 8/12/2019 CS2312-c++ lab

    18/35

    18

    #includeclass num

    {

    private:

    int a,b,c,d;public:

    void input (void);

    void show(void);num operator+(num);

    };

    void num ::input(){

    couta>>b>>c>>d;

    }void num::show()

    {

    cout

  • 8/12/2019 CS2312-c++ lab

    19/35

    19

    object y enter the values for a,b,c,d: 5 6 4 9

    x: A=5 B=6 C=9 D=8

    y: A=5 B=6 C=4 D=9

    z: A=10 B=12 C=13 D=17

    RESULT:

    Thus the C++ program to implement Binary operator overloading was executed and output verified.

    Ex No:6

    Function overloading

    AIM:To write a c++ program to illustrate the concept of function overloading.

    ALGORITHM:1. Declare necessary variables.

    2.

    Create a class with add(int,int) ,add(float,float) asmember functions andnecessary variable.

    3. add(int,int) is used to add two integer values.4. add(float,float) is used to add two float values.

    5. Using object call the required function with corresponding input.

    6. Display the output.

    PROGRAM:

    #include#include

    class fover{public:

    int a,b,c;

    float x,y,z;fover()

    {

    cout

  • 8/12/2019 CS2312-c++ lab

    20/35

    20

    }~fover()

    {

    coutb;

    f.add(a,b);coutx>>y;

    f.add(x,y);

    getch();}

    OUTPUT:object is created:

    enter the integer value3 4

    the addtion values:7

    enter the float values:2.4

    2.6

    the addtion values5the object is deleted

    RESULT:

    Thus the C++ program using function overloading was executed and output verified.

    Ex No:7A

    Multiplication of Postive numbers using Single Inheritance

    AIM:

    To write a c++ program tomultiply the positive numbers using single inheritance.

    ALGORITHM:

    1. Define a Nase class Ba. Class B has one private data member ,one public data member and three

    member functions.

    2. Define Derived class D.All the Properties of B is privately inherited.3. Create a derived class object.

  • 8/12/2019 CS2312-c++ lab

    21/35

    21

    4. Access the base class data members .5. Do the multiplication.

    6. And display the results.

    DIAGRAM:B

    D

    PROGRAM:#include

    #include

    class B

    {int a;

    public:int b;

    void getab();

    int geta(void);

    void showall(void);};

    class D:private B

    {int c;

    public:void mul(void);

    void display(void);};

    void B::getab()

    {couta>>b;

    }

    int B::geta(){

    return a;}void B::showall()

    {

    cout

  • 8/12/2019 CS2312-c++ lab

    22/35

    22

    getab();c=b*geta();

    }

    void D::display()

    {showall();

    cout

  • 8/12/2019 CS2312-c++ lab

    23/35

    23

    EMPLOYEE PERSON

    MANAGER

    PROGRAM:

    #include

    #include

    class person{

    int age,empno;

    char name[15];

    public:void getdata()

    {

    coutname;

    coutage;}

    void display()

    {

    cout

  • 8/12/2019 CS2312-c++ lab

    24/35

    24

    class manager:public person,public employee{

    char position[26];

    public:

    void read(){

    get();

    getdata();cout

  • 8/12/2019 CS2312-c++ lab

    25/35

    25

    Ex No:8A

    Calculation of area of shapes using virtual functionAIM:

    To write a c++ program for calculation of area of shapes using virtual functions.

    ALGORITHM:1. Define a shape class .

    2. Define a virtual function show().

    3. Define Rectangle class and inherit the base class.

    4. Create a pointer and object for base class.5. Display the rectangle and triangle information.

    PROGRAM:

    #include#include

    #includetemplate

    t max(t a,t b)

    {

    if (a>b)return a;

    else

    return b;}

    void main(){

    clrscr();char ch1,ch2,ch;

    coutch1>>ch2;ch=max(ch1,ch2);

    cout

  • 8/12/2019 CS2312-c++ lab

    26/35

    26

    }OUTPUT:

    enter two characters(ch1,ch2):a s

    max(ch1,ch2):s

    enter 2 integers2 4max(a,b):4

    enter 2 floats:2.6 4.5

    max(f1,f2):4.5RESULT:

    Thus the C++ program to implement virtual function was executed and output verified.

    Ex No:8B

    Student marklist using virtual base class

    AIM:

    To write a c++ program for a student mark list processing using virtual base class.

    ALGORITHM:

    1. Define a student class and make this as virtual.2. Define marks class and Inherit the student class virtually.

    3. Define sports class and inherit properties of student virtually.

    4. Define result class and inherit marks, sports into result.

    5. Display student information with marks.

    DIAGRAM:

    STUDENT

    TEST SPORTS

    RESULT

    PROGRAM:

    #include#include

    class student

    {

  • 8/12/2019 CS2312-c++ lab

    27/35

    27

    public:char name[10];

    int rollno;

    void gets()

    {coutname;

    coutrollno;

    }};

    class marks:virtual public student{

    public:

    int m1,m2,m3;

    void details(){

    coutm1>>m2>>m3;

    }};class sportsmarks:virtual public student

    {

    public:int m4;

    void puts()

    {coutm4;

    }};

    class result:virtual public marks,virtual public sportsmarks

    {public:

    int tot;float avg;

    void display()

    {

    tot=m1+m2+m3+m4;avg=tot/4;

    }

    void demo()

    {

    cout

  • 8/12/2019 CS2312-c++ lab

    28/35

    28

    result r;r.gets();

    r.details();

    r.puts();

    r.display();r.demo();

    getch();

    }

    //output//

    student name: isac

    enter student roll no: 10

    enter the 3 subjects marks: 7878

    82

    enter the sports marks: 65

    m1=78m2=78m3=82m4=65

    the sports markstotal= 303

    avg is 75

    RESULT:

    Thus the C++ program to implement virtual base class was executed and output verified.

    Ex No:9A

    Function Template

    AIM:To write a c++ program using function template to find the maximum of two datas.

    ALGORITHM:

    1. Create the function template, to find the maximum of two datas.2.

    Get different data type values like integer, float and character.

    3. Find maximum of those data using template function.

    4. Finally print the result.

    PROGRAM:#include

    #include

    #include

  • 8/12/2019 CS2312-c++ lab

    29/35

    29

    templatet max(t a,t b)

    {

    if (a>b)

    return a;else

    return b;

    }void main()

    {

    clrscr();char ch1,ch2,ch;

    coutch1>>ch2;

    ch=max(ch1,ch2);cout

  • 8/12/2019 CS2312-c++ lab

    30/35

    30

    ALGORITHM:1. Create the class template, to find the greatest of two datas.

    2. Get different data type values like integer, float and character.

    3. Use a function to find the maximum of two values for both int, float .4. Find maximum of those data using template function.

    5.

    Finally print the result.

    PROGRAM

    #include#include

    #include

    template

    class demo{

    public:

    int i;

    t a,b;void gets()

    {

    couta>>b;

    }

    void gets1(){

    couta>>b;

    }

    void operation(){

    if(a>b)cout

  • 8/12/2019 CS2312-c++ lab

    31/35

    31

    }}};void main()

    {

    clrscr();

    demod1;d1.gets();

    d1.operation();

    demod2;d2.gets1();

    d2.operation();

    demod3;d3.compare();

    getch();

    }

    OUTPUT:

    enter 2 integers:2 3

    b is greater:3enter 2 floats:5 4

    a is greater:5enter 2 strings:

    5 6b1 has the highest length6

    RESULT:

    Thus the C++ program to implement class template was executed and output verified.

    Ex No:10 A

    Sequential File Access

    AIM:To write a c++ program for creating student data using sequential file access.

    ALGORITHM:1. Define student class and create the function get and show.

    2. Using ofstream and ifstream get and display the file information.

    3. Invoke function using sequential file access.

    PROGRAM

    #include

  • 8/12/2019 CS2312-c++ lab

    32/35

    32

    #include#include

    class student

    {

    protected:char name[10];

    int rollno;

    public:void get()

    {

    coutname;

    coutrollno;

    }void show()

    {

    cout

  • 8/12/2019 CS2312-c++ lab

    33/35

  • 8/12/2019 CS2312-c++ lab

    34/35

    34

    PROGRAM

    #include

    #include

    #includeclass student

    {

    protected:char name[10];

    int rollno;

    public:void get()

    {

    coutname;coutrollno;

    }

    void show(){

    cout

  • 8/12/2019 CS2312-c++ lab

    35/35

    int pos=(n-1)*sizeof(student);in.seekg(pos);

    in.read((char*)&s,sizeof(s));

    s.show();

    getch();}

    OUTPUT:

    enter student data

    enter the name:ashok

    enter roll no:5

    enter another student data(y/n)?y

    enter student data

    enter the name:hakeem

    enter roll no:11

    enter another student data(y/n)?n

    there are2person in file

    enter person no:2

    name:hakeem

    roll no:11

    RESULT:

    Thus the C++ program to create student data using Randoml file access was executed and output

    verified.