0% found this document useful (0 votes)
239 views8 pages

EXPERIMENT NUMBER - Practical 7.1: Aim of The Experiment

This document describes 3 programming experiments done by the student Nayanika Baweja related to runtime polymorphism in C++. The first program maintains records of persons with name and age and finds the eldest using operator overloading. The second program accesses class members using pointers to objects. The third program represents a digital library using inheritance with books and tapes as derived classes of a base media class.

Uploaded by

Lakshay Chauhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
239 views8 pages

EXPERIMENT NUMBER - Practical 7.1: Aim of The Experiment

This document describes 3 programming experiments done by the student Nayanika Baweja related to runtime polymorphism in C++. The first program maintains records of persons with name and age and finds the eldest using operator overloading. The second program accesses class members using pointers to objects. The third program represents a digital library using inheritance with books and tapes as derived classes of a base media class.

Uploaded by

Lakshay Chauhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

EXPERIMENT NUMBER –Practical 7.

1
STUDENT’S NAME – Nayanika Baweja

STUDENT’S UID – 20BCS5348

CLASS AND GROUP – 20/C

SEMESTER – 2nd

TOPIC OF EXPERIMENT – Programs based on Run-time polymorphism.

AIM OF THE EXPERIMENT: WAP to create a class that will maintain the records of person with details

(Name and Age) and find the eldest among them. The program must use this pointer to return the result by

overloading> operator among two objects.

FLOWCHART/ ALGORITHM
Step 1: Start
Step 2: Create class Records.
Step 3: Declare name and age variables.
Step 4: Create show() function to display name and age.
Step 5: Create eldest() function and by overloading > operator find out the eldest and return using
pointer.
Step 6: Create main() function.
Step 7: Enter the data.
Step 8: Find out the eldest.
Step 9: Print the result.
Step 10: End.

PROGRAM CODE
#include<iostream>
using namespace std;

class Records
{
    int age;
    string name;
    public:
    Records() {};
    Records(string n,int a): name(n),age(a) {}
    void show()
    {
        cout<<name<<" : "<<age<<endl;
    }
    Records eldest(Records o)
SUBJECT NAME- OBJECT ORIENTED PROGRAMMING
USING C++ LAB
SUBJECT CODE-CSP-152
    {
        return (o.age>age)? o: *this;
    }
};

int main()
{  
    cout<<"Nayanika Baweja"<<endl;
    cout<<"UID: 20BCS5348"<<endl;
    cout<<"\n\n"<<endl;
    Records ob[3]={Records("Ani",21),Records("Arka",50),Records("Ram",30)};
    Records res;
    for(int i=0;i<2;i++)
    res = ob[i].eldest(ob[i+1]);
    res.show();
    return 0;  
}

ERRORS ENCOUNTERED DURING PROGRAM’S EXECUTION


(Kindly jot down the compile time errors encountered)
-NA-

PROGRAMS’ EXPLANATION (in brief)


This program maintains the records of person with details (Name and Age) and find the eldest
among them. This uses this pointer to return the result by overloading> operator among two
objects.

OUTPUT

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


USING C++ LAB
SUBJECT CODE-CSP-152
EXPERIMENT NUMBER –Practical 7.2
STUDENT’S NAME – Nayanika Baweja

STUDENT’S UID – 20BCS5348

CLASS AND GROUP – 20/C

SEMESTER – 2nd

TOPIC OF EXPERIMENT – Programs based on Run-time polymorphism.

AIM OF THE EXPERIMENT: WAP to access members using pointer to object members.

FLOWCHART/ ALGORITHM
Step 1: Start
Step 2: Create class Number
Step 3: Declare variable num.
Step 4: Declare constructor and set num as 0.
Step 5: Create member function to get input from the user.
Step 6: Create member function to display the result.
Step 7: Create main function.
Step 8: Declare object to the class number.
Step 9: Input and display number
Step 10: Declare pointer to the object
Step 11: Print default value
Step 12: Call member function using pointer.
Step 13: Input values and print
Step 14: End.

PROGRAM CODE
#include <iostream>
using namespace std;

class Number
{
    private:
    int num;
    public:
    Number(){ num=0; };
    void inputNumber (void)
    {
        cout<<"Enter an integer number: ";
        cin>>num;
    }
    void displayNumber()
    {
    cout<<"Num: "<<num<<endl;
SUBJECT NAME- OBJECT ORIENTED PROGRAMMING
USING C++ LAB
SUBJECT CODE-CSP-152
    }
};

int main()
{
    cout<<"Nayanika Baweja"<<endl;
    cout<<"UID: 20BCS5348"<<endl;
    cout<<"\n\n"<<endl;
    Number N;
    N.inputNumber();
    N.displayNumber();
    Number *ptrN;
    ptrN = new Number;
    cout<<"Default value... "<<endl;
    ptrN->displayNumber();
    ptrN->inputNumber();
    ptrN->displayNumber();
    return 0;
}

ERRORS ENCOUNTERED DURING PROGRAM’S EXECUTION


(Kindly jot down the compile time errors encountered)
-NA-

PROGRAMS’ EXPLANATION (in brief)


The program shows how to access members using pointer to object members.

OUTPUT

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


USING C++ LAB
SUBJECT CODE-CSP-152
EXPERIMENT NUMBER –Practical 7.3
STUDENT’S NAME – Nayanika Baweja

STUDENT’S UID – 20BCS5348

CLASS AND GROUP – 20/C

SEMESTER – 2nd

TOPIC OF EXPERIMENT – Programs based on Run-time polymorphism.

AIM OF THE EXPERIMENT: WAP to design a class representing the information regarding digital library
(books, tape: book & tape should be separate classes having the base class as media).The class should have the
functionality for adding new item, issuing, deposit etc. The program should link the objects with concerned
function by the concept of runtime polymorphism.

FLOWCHART/ ALGORITHM
Step 1: Start
Step 2: Create class media
Step 3: Declare variables title and price, and parameterized constructor.
Step 4: Declare member function display()
Step 5: Create derived class book.
Step 6: Declare member function page. Also, declare the parameterized constructor for the same
class.
Step 7: Create derived class taper.
Step 8: Declare member function time. Also, declare the parameterized constructor for the same
class.
Step 9: Declare main function.
Step 10: Take input from the user and then print the output.
Step 11: End.

PROGRAM CODE
#include<iostream>
#include<string.h>
using namespace std;

class media
{
    protected:
    char title[50];
    float price;
    public:
    media(char *s, float a)
    {
        strcpy(title, s); price = a;
    }
    virtual void display(){}
    };
SUBJECT NAME- OBJECT ORIENTED PROGRAMMING
USING C++ LAB
SUBJECT CODE-CSP-152
    class book : public media
    {
        int pages; public:
        book(char *s, float a, int p) : media(s,a)
        {
            pages = p;
        }
        void display();
    };
    class tape : public media
    {
        float time; public:
        tape(char * s, float a, float t):media(s,a)
        {
            time =t;
        }
        void display();
    };
    void book ::display()
    {
        cout<<"\n Title:"<<title;
        cout<<"\n Pages:"<<pages; cout<<"\n Price:"<<price;
    }
    void tape ::display ()
    {
        cout<<"\n Title:"<<title;
        cout<<"\n Play Time:"<<time<<"mins"; cout<<"\n Price:"<<price;
    }

int main()
{
    cout<<"Nayanika Baweja"<<endl;
    cout<<"UID: 20BCS5348"<<endl;
    cout<<"\n\n"<<endl;
    char * title = new char[30]; float price, time;
    int pages;
    cout<<"\n Enter Book Details \n"; cout<<"\n Title:";
    cin>>title; cout<<"\n Price:"; cin>>price; cout<<"\n Pages:"; cin>>pages;
    book book1(title, price, pages);
    cout<<"\n Enter Tape Details";
    cout<<"\n Title:";
    cin>>title;
    cout<<"\n Price:";
    cin>>price;
    cout<<"\n Play Times(mins):";
    cin>>time;
    tape tape1(title, price, time);
    media* list[2];
    list[0] = &book1;
    list[1] = &tape1; cout<<"\n Media Details";
SUBJECT NAME- OBJECT ORIENTED PROGRAMMING
USING C++ LAB
SUBJECT CODE-CSP-152
    cout<<"\n..............Book.        ";
    list[0]->display ();
    cout<<"\n..............Tape.        ";
    list[1]->display ();
    return 0;
}

ERRORS ENCOUNTERED DURING PROGRAM’S EXECUTION


(Kindly jot down the compile time errors encountered)
-NA-

PROGRAMS’ EXPLANATION (in brief)


This program represents the data of digital library, using books and tape as separate derived
classes. The class here have the functionality for adding new item, deposit, issuing, etc. The program
links the objects with concerned function by the concept of runtime polymorphism.

OUTPUT

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


USING C++ LAB
SUBJECT CODE-CSP-152
LEARNING OUTCOMES
 Identify situations where computational methods would be useful.
 Approach the programming tasks using techniques learnt and write pseudo-code.
 Choose the right data representation formats based on the requirements of the problem.
 Use the comparisons and limitations of the various programming constructs and choose the
right one for the task.

EVALUATION COLUMN (To be filled by concerned faculty only)


Sr. No. Parameters Maximum Marks
Marks Obtained
1. Worksheet Completion including writing 10
learning objective/ Outcome
2. Post Lab Quiz Result 5

3. Student engagement in Simulation/ 5


Performance/ Pre Lab Questions
4. Total Marks 20

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


USING C++ LAB
SUBJECT CODE-CSP-152

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy