0% found this document useful (0 votes)
120 views

Experiment Number - 2.3: Aim of The Experiment - 7.1

The document describes 3 experiments related to object-oriented programming in C++. The first experiment involves creating a class to maintain records of people with name and age, and finding the eldest person by overloading the > operator. The second uses pointers to objects to access class members. The third designs classes for books and tapes with a base Media class, allowing new items to be added to a digital library.

Uploaded by

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

Experiment Number - 2.3: Aim of The Experiment - 7.1

The document describes 3 experiments related to object-oriented programming in C++. The first experiment involves creating a class to maintain records of people with name and age, and finding the eldest person by overloading the > operator. The second uses pointers to objects to access class members. The third designs classes for books and tapes with a base Media class, allowing new items to be added to a digital library.

Uploaded by

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

EXPERIMENT NUMBER -2.

STUDENT’S NAME – SAKSHI KUMARI


STUDENT’S UID – 21BCS9402
CLASS AND GROUP – 217B
SEMESTER -2 nd SEMESTER
SUBJECT: - OBJECT ORIENTEDPROGRAMMING USING C++
SUBJECT CODE: - 21CSH103
A DATE OF PERFORMANCE:-08/04/2022

AIM – Programs based on Run-time polymorphism.

AIM OF THE EXPERIMENT– 7.1


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
 Start
 Create a class Records. Declare some required variables such as name and
age.
 Variables will be private.
 These variables will be accessed by the member functions of the class
Records.
 Now we will create a constructor which will initialize the variables with
their required values.
 The constructor which we will create will be a parameterized constructor.
 Now we will define a functions whose return type will be Records.
 In the function we will check whose age is greater and will be printed that
age.
 In the function we will use ternary operator to find out the greatest of them
by using ‘this’ pointer that will hold the position of the greater age and then
again compare with the next age.

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)

{
return (o.age>age)? o: *this;

};

int main()

{
cout<<"SAKSHI KUMARI 21BCS9402"<<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;

PROGRAMS’EXPLANATION
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.

ENCOUNTERED DURING PROGRAM’S EXECUTION


NO ERROR|

OUTPUT:-

AIM OF THE EXPERIMENT– 7.2


WAP to access members using pointer to object members.

FLOWCHART/ALGORITHM –
 Include the required header files (iostream.h and conio.h).
 Create a class (Student) with the following class members as public
members.
stud_name, marks as data members.
getStudentInfo() and displayStudentInfo() as member functions.
 Implement the getStudentInfo() and displayStudentInfo() member
functions.
 Create a main() method.
 Create an array of stud object wigth max size and a pointer object of
Student class.
 Assign base address of object array to pointer object and make function
calls to getStudentInfo() and displayStudentInfo() using class pointer
object

PROGRAM CODE:
#include <iostream>

using namespace std;

class Number

private:

int num;

public:
//constructor

Number(){ num=0; };

//member function to get input

void inputNumber (void)

cout<<"Enter an integer number: ";

cin>>num;

//member function to display number

void displayNumber()

cout<<"Num: "<<num<<endl;

};

//Main function

int main()

{
cout<<"SAKSHI KUMARI 21BCS9402"<<endl;
//declaring object to the class number

Number N;

//input and display number using norn object

N.inputNumber();

N.displayNumber();

//declaring pointer to the object

Number *ptrN;

ptrN = new Number; //creating & assigning memory

//printing default value

cout<<"Default value... "<<endl;

//calling member function with pointer

ptrN->displayNumber();

//input values and print

ptrN->inputNumber();

ptrN->displayNumber();

return 0;

}
PROGRAMS’EXPLANATION
C++ allows us to create a class with its members such as data members and
member functions. One way to access the data members and member function
of a class is through the object of the class with a dot operator. C++ also
provides us a few operators through we could access the data members and
member functions of a class by using pointers. These operators are known as
dereferencing operators.

ENCOUNTERED DURING PROGRAM’S EXECUTION

NO ERROR |
OUTPUT:
AIM OF THE EXPERIMENT– 7.3
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 –
1. Start
2. Create a class Records. Declare some required variables such as name and age.
3. Variables will be private.
4. These variables will be accessed by the member functions of the class Records.
5. Now we will create a constructor which will initialize the variables with their
required values.
6. The constructor which we will create will be a parameterized constructor.
7. Now we will define a functions whose return type will be Records.
8. In the function we will check whose age is greater and will be printed that age.
9. in the function we will use ternary operator to find out the greatest of them by
using ‘this’ pointer that will hold the position of the greater age and then again
compare with the next age
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(){}

};

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<<"SAKSHI KUMARI 21BCS9402"<<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";

cout<<"\n..............Book. ";

list[0]->display ();

cout<<"\n..............Tape. ";

list[1]->display ();

return 0;

PROGRAMS’EXPLANATION
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.

ENCOUNTERED DURING PROGRAM’S EXECUTION


NO ERROR

OUTPUT:
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

You might also like