0% found this document useful (0 votes)
55 views14 pages

C++ Worksheet 7

The document describes three 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 among them using runtime polymorphism. The second experiment accesses class members like data members and member functions using pointers to objects. The third experiment designs a class representing a digital library with books and tapes as separate classes derived from a base media class.

Uploaded by

Aswin
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)
55 views14 pages

C++ Worksheet 7

The document describes three 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 among them using runtime polymorphism. The second experiment accesses class members like data members and member functions using pointers to objects. The third experiment designs a class representing a digital library with books and tapes as separate classes derived from a base media class.

Uploaded by

Aswin
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/ 14

EXPERIMENT NUMBER –Practical 7.

Name: Loga Aswin R. Student Uid : 21BCS10573


Section: ON21AML109 B Date of performance: 19.04.22
Course: BE CSE AIML. Subject name: Oops c++

Topic : Program based 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

1. Create a class Records. Declare some required variables such as name


and age.
2. Variables will be private.
3. These variables will be accessed by the member functions of the class
Records.
4. Now we will create a constructor which will initialize the variables with their
required values.
5. The constructor which we will create will be a parameterized constructor.
6. Now we will define a functions whose return type will be Records.
7. In the function we will check whose age is greater and will be printed that
age.
8. 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.

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


SUBJECT CODE-CSP-
USING C++ LAB
152
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()

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;

}
SUBJECT NAME- OBJECT ORIENTED PROGRAMMING
SUBJECT CODE-CSP-
USING C++ LAB
152
PROGRAMS’ EXPLANATION (in brief)
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.

OUTPUT:

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


SUBJECT CODE-CSP-
USING C++ LAB
152
EXPERIMENT NUMBER –Practical 7.2

AIM OF THE EXPERIMENT –WAP to access members using pointer to object


members.

FLOWCHART/ ALGORITHM –

• Step 1 - Include the required header files (iostream.h and conio.h).


• Step 2 - Create a class (Student) with the following class members
as public members.
o stud_name, marks as data members.

o getStudentInfo() and displayStudentInfo() as member

functions.
• Step 3 - Implement the getStudentInfo() and displayStudentInfo()
member functions.
• Step 4 - Create a main() method.
• Step 5 - Create an array of stud object wigth max size and a pointer
object of Student class.
• Step 6 - Assign base address of object array to pointer object and
make function calls to getStudentInfo() and displayStudentInfo()
using class pointer object.

PROGRAM CODE

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


SUBJECT CODE-CSP-
USING C++ LAB
152
#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;

};

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


SUBJECT CODE-CSP-
USING C++ LAB
152
//Main

function int main()

//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 (in brief)


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
SUBJECT NAME- OBJECT ORIENTED PROGRAMMING
SUBJECT CODE-CSP-
USING C++ LAB
152
data members and member functions of a class by using pointers.
These operators are known as dereferencing operators.

Output:

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


SUBJECT CODE-CSP-
USING C++ LAB
152
EXPERIMENT NUMBER –Practical 7.3

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 –
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

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


SUBJECT CODE-CSP-
USING C++ LAB
152
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:
SUBJECT NAME- OBJECT ORIENTED PROGRAMMING
SUBJECT CODE-CSP-
USING C++ LAB
152
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()

{ char *

title =

new

char[30];

float

price,

time; int

pages;

cout<<"\

n Enter

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


SUBJECT CODE-CSP-
USING C++ LAB
152
Book

Details \

n";

cout<<"\

n Title:";

cin>>title

cout<<"\

n Price:";

cin>>pric

e;

cout<<"\

Pages:";

cin>>pag

es; book

book1(titl

e, price,

pages);

cout<<"\

n Enter

Tape

Details";

cout<<"\
SUBJECT NAME- OBJECT ORIENTED PROGRAMMING
SUBJECT CODE-CSP-
USING C++ LAB
152
n

Title:";

cin>>title

cout<<"\

n Price:";

cin>>pric

e;

cout<<"\

n Play

Times(mi

ns):";

cin>>tim

e; tape

tape1(titl

e, price,

time);

media*

list[2];

list[0] =

&book1;

list[1] =

&tape1;

cout<<"\
SUBJECT NAME- OBJECT ORIENTED PROGRAMMING
SUBJECT CODE-CSP-
USING C++ LAB
152
n Media

Details";

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

list[0]->display (); cout<<"\

n..............Tape. "; list[1]-

>display (); return 0;

PROGRAMS’ EXPLANATION (in brief)


A class representing the information regarding digital library (books, tape: book & tape should
be separate classes having the base class asmedia). The class should have the functionality for
adding new item, issuing, deposit etc. The program should link the objects with concerned
function by theconcept of runtime polymorphism.

Output:

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


SUBJECT CODE-CSP-
USING C++ LAB
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


SUBJECT CODE-CSP-
USING C++ LAB
152

You might also like