Question

Class Exercise Write a program that does the following: 1. Defines a class named Data that has the following members a. A vec

Please answer this question in C++.

Also, please write it with 'using namespace std' because the other ways some of you do it is confusing.

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

Code:

#include<iostream>
#include<vector>
using namespace std;
class Data
{
   vector<int> numbers;
   public:
       void display_menu();
       void input_numbers();
       void display_numbers();
       void search_numbers();
       void delete_numbers();
};
void Data::display_menu()
{
   while(1)
   {
       cout<<"Enter 0-Quit, 1. Input, 2. Display, 3. search, 4. delete: ";
       int ch;cin>>ch;
       if(ch == 0)
       {
           break;
       }
       else if(ch == 1)
       {
           input_numbers();
       }
       else if(ch == 2)
       {
           display_numbers();
       }
       else if(ch == 3)
       {
           search_numbers();
       }
       else if(ch == 4)
       {
           delete_numbers();
       }
       else
       {
           cout<<"Enter a valid choice"<<endl;
       }
   }
}
void Data::input_numbers()
{
   cout<<"Enter number: "<<endl;int num;cin>>num;
   numbers.push_back(num);//insert number
}
void Data::search_numbers()
{
   cout<<"Enter number to search: "<<endl;int num;cin>>num;
   vector<int>::iterator it;
   for(it = numbers.begin();it!=numbers.end();it++)
   {
       if(*it == num)
       {
           cout<<"Found at "<<it-numbers.begin()<<endl;
           break;
       }
   }
   if(it == numbers.end())//not found
   {
       cout<<"Number not found"<<endl;
   }
}
void Data::display_numbers()
{
   if(numbers.size() == 0)
   {
       cout<<"Vector empty"<<endl;
   }
   else
   {
       for(vector<int>::iterator it = numbers.begin();it!=numbers.end();it++)
       {
           cout<<*it<<" ";
       }
       cout<<endl;
   }
}
void Data::delete_numbers()
{
   numbers.clear();//delete contents of numbers
}
int main()
{
   Data obj;obj.display_menu();
   return 0;
}

Output:

media%2F133%2F13350aa8-4e8b-4da8-b46b-59

Add a comment
Know the answer?
Add Answer to:
Please answer this question in C++. Also, please write it with 'using namespace std' because the other ways some of you do it is confusing. Class Exercise Write a program that does the follow...
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
  • Write an object-oriented C++ program (i.e. a class and a main function to use it), using...

    Write an object-oriented C++ program (i.e. a class and a main function to use it), using pointer variables, that program that performs specific searching and sorting exercises of an array of integers. This program has six required outputs. Start by initializing an array with the following integers, in this order: 23, 17, 5, 90, 12, 44, 38, 84, 77, 3, 66, 55, 1, 19, 37, 88, 8, 97, 25, 50, 75, 61, and 49. Your array input may be hardcoded...

  • Please help with this assignment. Thanks. 1. Write a C++ program containing a class Invoice and...

    Please help with this assignment. Thanks. 1. Write a C++ program containing a class Invoice and a driver program called invoiceDriver.cpp. The class Invoice is used in a hardware store to represent an invoice for an item sold at the store. An invoice class should include the following: A part number of type string A part description of type string A quantity of the item being purchased of type int A price per item of type int A class constructor...

  • Need help doing program In bold is problem E2 #include<iostream> #include<string> #include<vector> using namespace std; //Car...

    Need help doing program In bold is problem E2 #include<iostream> #include<string> #include<vector> using namespace std; //Car class //Defined enum here enum Kind{business,maintenance,other,box,tank,flat,otherFreight,chair,seater,otherPassenger}; //Defined array of strings string KIND_ARRAY[]={"business","maintenance","other,box","tank,flat","otherFreight","chair","seater","otherPassenger"}; class Car { private: string reportingMark; int carNumber; Kind kind; //changed to Kind bool loaded; string destination; public: //Defined setKind function Kind setKind(string knd) { for(int i=0;i<8;i++) //repeat upto 8 kinds { if(knd.compare(KIND_ARRAY[i])==0) //if matched in Array kind=(Kind)i; //setup that kind } return kind; } //Default constructor Car() { } //Parameterized constructor...

  • Please write a c++ header file, class implementation file and main file that does all of...

    Please write a c++ header file, class implementation file and main file that does all of the following and meets the requirements listed below. Also include a Output of your code as to show that your program works and functions properly. EXERCISING A DOUBLY-LINKED LIST CLASS This project consists of two parts, the second of which appears below. For the first part, write a class that implements an unordered list abstract data type using a doubly-linked list with pointers to...

  • write it in c++ Question 6 Consider a case of single inheritance where Landline phone is a base class and Mobile phone is the derived class. Both the classes are as follow: (a) Landline: It has su...

    write it in c++ Question 6 Consider a case of single inheritance where Landline phone is a base class and Mobile phone is the derived class. Both the classes are as follow: (a) Landline: It has subscriber name and number as data members. The member functions are to provide the features of calling on a subscriber's number and receiving a call Void call (int sub_number) Void receivel (b) Mobile: Apart from inheriting the features of a Landline phone, it provides...

  • Overview: file you have to complete is WordTree.h, WordTree.cpp, main.cpp Write a program in C++ that...

    Overview: file you have to complete is WordTree.h, WordTree.cpp, main.cpp Write a program in C++ that reads an input text file and counts the occurrence of individual words in the file. You will see a binary tree to keep track of words and their counts. Project description: The program should open and read an input file (named input.txt) in turn, and build a binary search tree of the words and their counts. The words will be stored in alphabetical order...

  • Write the following program in C++. Review structures, pointers and dynamic memory allocation from CSIT 839....

    Write the following program in C++. Review structures, pointers and dynamic memory allocation from CSIT 839. Also, review pointers and dynamic memory allocation posted here under Pages. For sorting, you can refer to the textbook for Co Sci 839 or google "C++ sort functions". I've also included under files, a sample C++ source file named sort_binsearch.cpp which gives an example of both sorting and binary search. The Bubble sort is the simplest. For binary search too, you can refer to...

  • Write a C++ program for the instructions below. Please read the instructions carefully and make sure they are followed correctly.   please put comment with code! and please do not just copy other solu...

    Write a C++ program for the instructions below. Please read the instructions carefully and make sure they are followed correctly.   please put comment with code! and please do not just copy other solutions. Instructions 1. Read instructions carefully! 2. Use C++ syntax only, C syntax will not be accepted. 3. Always use braces to define blocks. 4. Indent all lines within a block. Each block requires one more tab. 5. Organize your code well with proper formatting and a single...

  • in c++ please include all of the following " template class, template function, singly linked list,...

    in c++ please include all of the following " template class, template function, singly linked list, the ADT stack, copy constructor, operator overloading, "try catch"and pointers Modify the code named "Stack using a Singly Linked List" to make the ADT Stack that is a template class has the code of the destructor in which each node is directly deleted without using any member function. As each node is deleted, the destructor displays the address of the node that is being...

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