Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1.Object Oriented Programming c++ The following question investigates the Item c

ID: 3907545 • Letter: 1

Question

1.Object Oriented Programming c++

The following question investigates the Item class, which contains two member variables: string name and int price. We consider two Items to be equal if they have the same name and price.

Consider a function bool equal (Item& i1, Item& i2) that returns true if the two Items are equal. Explain how to give the equal function access to the private variables without using public functions, and declare the function.

2. Abstract Data Types c++

ii. Define a class called ListStack which inherits from StackInterface. ListStack uses a linked-list of Nodes for its stack.
struct Node{

Node* next;

int value; }

iii. Implement the function of ListStack that pushes new items onto the stack.

Explanation / Answer


1.
#include<iostream>
using namespace std;
class Item
{
string name;
int price;
friend bool equal(Item& i1, Item& i2);
friend void Setvalues(string name,int price);
};
bool equal(Item& i1, Item& i2)
{
if((i1.name=="Mango")||(i2.price==500))
return true;
else
return false;
}
void Setvalues(string name,int price)
{
name=name;
price=price;
}
int main()
{
Item obj;
Item obj2;
Setvalues("Mango",500);
bool result=equal(obj,obj2);
cout<<" The Result is:"<<result<<endl;
return 0;
}
//Friend Class A friend class can access private and protected members of other class in which it is declared as friend.
//It is sometimes useful to allow a particular class to access private members of other class


2.
// CPP program to illustrate
// concept of Virtual Functions
#include<iostream>
using namespace std;

class StackInterface
{
public:
struct Node
{
Node* next;
int value;
}
struct Node node;
public:
virtual void push(int data)
{
node->value=data;
cout<<"Data is Inserted "<<endl;
}
void print()
{
cout<<node->value<<endl;
}
};

class ListStack :public base
{
public:
void push(int data)
{
node->value=data;
cout<<"Data is Inserted "<<endl;
}
void print()
{
cout<<node->value<<endl;
}
};

int main()
{
StackInterface *bptr;
ListStack d;
bptr = &d;
//virtual function, binded at runtime
bptr->push(10);
bptr->print();
}