Question

In c++, i have a class that has a data member that is a vector of...

In c++, i have a class that has a data member that is a vector of objects (vector<object> objects) and each object is composed of one integer value (int price) and one string value (string name). What would be my best approach to create a member function that searches the vector for a range of specific items? ex: i have $100 and i want to output the top 5 objects from the vector that i can afford with this $100.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

items.cpp

#include<iostream>
#include<vector>
using namespace std;

class Item
{
   //Private data members
   private:
       string name;
       int price;
       vector<Item> items;
   //public functions
   public:
       //Non-parameterized constructor
       Item()
       {

       }
       //parameterized constructor
       Item(string name, int price)
       {
           this->name=name;
           this->price=price;
       }
       //FUnction declarations
       void addItem(Item item);
       vector<Item> getTop5(int price);
       string getName();
       int getPrice();
};

void Item::addItem(Item item)
{
   //Add item object to vector of Items
   items.push_back(item);
}

vector<Item> Item::getTop5(int price)
{
   //declaration of vector
   vector<Item> top5;
   int count=0;
   //Iterate till end of list or till we got top5 elements
   for(int i=0;i<items.size() && count<5;i++)
   {
       //If the item is affordable with $100 then select that item
       if(items[i].price<=100)
       {
           top5.push_back(items[i]);
           //Increment the count
           count++;
       }
   }
   //return the resultant vecor
   return top5;
}

//return the name of object
string Item::getName()
{
   return name;
}

//return the price of object
int Item::getPrice()
{
   return price;
}

int main()
{
   //Declaration of objects
   Item item;
   Item item1("MObile" , 150);
   Item item2("Refrigirator" , 70);
   Item item3("Air Cooler" , 170);
   Item item4("clothes" , 30);
   Item item5("Calculator" , 20);
   Item item6("Home Theatre" , 60);
   Item item7("Headset" , 45);
   Item item8("Textbooks" , 101);
   Item item9("Tablet" , 80);
   Item item10("Laptop" , 500);
   Item item11("Belt" , 50);
   Item item12("Earphones" , 115);
   Item item13("SmartWatch" , 550);
   Item item14("Airpods" , 150);
  
   //Adding above objects to vector
   item.addItem(item1);
   item.addItem(item2);
   item.addItem(item3);
   item.addItem(item4);
   item.addItem(item5);
   item.addItem(item6);
   item.addItem(item7);
   item.addItem(item8);
   item.addItem(item9);
   item.addItem(item10);
   item.addItem(item11);
   item.addItem(item12);
   item.addItem(item13);
   item.addItem(item14);

   //functionc alling to get top 5 items
   vector<Item> top5=item.getTop5(100);
   //Output the result
   for(int i=0;i<top5.size();i++)
   {
       cout<<top5[i].getName()<<" "<<top5[i].getPrice()<<endl;
   }
   return 0;
}

output screenshot:

Add a comment
Know the answer?
Add Answer to:
In c++, i have a class that has a data member that is a vector of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using C++ Design a class called Numbers that can be used to translate integers in the range 0 to ...

    Using C++ Design a class called Numbers that can be used to translate integers in the range 0 to 9999 into an English description of the number. The class should have a single integer member variable: int number; and a static array of string objects that specify how to translate the number into the desired format. For example, you might use static string arrays such as: string lessThan20[20] = {"zero", "one", ..., "eighteen", "nineteen"}; string hundred = "hundred"; string thousand...

  • i need help making this C++ code. Lab #2 Assignments: Numbers Class that translate whole dollar...

    i need help making this C++ code. Lab #2 Assignments: Numbers Class that translate whole dollar amounts in the range 0 through 9999 into an English description of the number. Numbers Class Design a class numbers that can be used to translate whole dollar amounts in the range 0 through 9999 into an English description of the number. For example, the number 713 would be translated into the string seven hundred thirteen, and 8203 would be translated into eight thousand...

  • C++ This exercise will introduce static member variables and static methods in class to you. Class...

    C++ This exercise will introduce static member variables and static methods in class to you. Class Department contain information about universities departments: name students amount in addition it also stores information about overall amount of departments at the university: departments amount class Department { public: Department(string i_name, int i_num_students); ~Department(); int get_students(); string get_name(); static int get_total(); private: string name; int num_students; static int total_departments; }; Carefully read and modify the template. You have to implement private static variable "total...

  • I have a question in how to create a method. For example there is a LinkedList...

    I have a question in how to create a method. For example there is a LinkedList of Objects, say and Object is public class Dog; private String name; private int age I want my method to take a dog age, and return the position (int) of the dog with that age has in the list example; //takes a dog age and returns position in the list, if no age in the list of dogs with age, method returns -1 public...

  • Can someone help me with the main class of my code. Here is the assignment notes....

    Can someone help me with the main class of my code. Here is the assignment notes. Implement project assignment �1� at the end of chapter 18 on page 545 in the textbook. Use the definition shown below for the "jumpSearch" method of the SkipSearch class. Notice that the method is delcared static. Therefore, you do not need to create a new instance of the object before the method is called. Simply use "SkipSearch.jumpSearch(...)" with the appropriate 4 parameter values. When...

  • LE 7.1 Create a separate class called Family. This class will have NO main(). Insert a...

    LE 7.1 Create a separate class called Family. This class will have NO main(). Insert a default constructor in the Family class. This is a method that is empty and its header looks like this: public NameOfClass() Except, for the main(), take all the other methods in LE 6.2 and put them in the Family class. Transfer the class variables from LE 6.2 to Family. Strip the word static from the class variables and the method headers in the Family...

  • ​I have to create two classes one class that accepts an object, stores the object in...

    ​I have to create two classes one class that accepts an object, stores the object in an array. I have another that has a constructor that creates an object. Here is my first class named "ShoppingList": import java.util.*; public class ShoppingList {    private ShoppingItem [] list;    private int amtItems = 0;       public ShoppingList()    {       list=new ShoppingItem[8];    }       public void add(ShoppingItem item)    {       if(amtItems<8)       {          list[amtItems] = ShoppingItem(item);...

  • I need help implementing class string functions, any help would be appreciated, also any comments throughout...

    I need help implementing class string functions, any help would be appreciated, also any comments throughout would also be extremely helpful. Time.cpp file - #include "Time.h" #include <new> #include <string> #include <iostream> // The class name is Time. This defines a class for keeping time in hours, minutes, and AM/PM indicator. // You should create 3 private member variables for this class. An integer variable for the hours, // an integer variable for the minutes, and a char variable for...

  • A class allows a group of one or more member objects to have a series of...

    A class allows a group of one or more member objects to have a series of common characteristics and functions. Create a class (student) that contains the member characteristics: Student ID (string) Student Last Name (string) Student First Name (string) GPA (float) Number of enrolled classes (integer) The class functions are: setID (string passed to function to set Student ID) setLastName (string passed to function to set Student Last Name) setFirstName (string passed to function to set Student First Name)...

  • About Classes and OOP in C++ Part 1 Task 1: Create a class called Person, which...

    About Classes and OOP in C++ Part 1 Task 1: Create a class called Person, which has private data members for name, age, gender, and height. You MUST use this pointer when any of the member functions are using the member variables. Implement a constructor that initializes the strings with an empty string, characters with a null character and the numbers with zero. Create getters and setters for each member variable. Ensure that you identify correctly which member functions should...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT