Question

Please answer the following question in C++.

(a) Define and implement a Person class, which has data members as described below: Name . Age (b) Next, you will create an S

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


#include <iostream>
#include <string.h>
#include <unistd.h>   //fo sleep func

#include <bits/stdc++.h>
using namespace std;

class Person       //person class
{
public:
char name[20];
int age;
  
Person()       //default constructor
{
strcpy(name,"ABC");
age = 73;
}
};

int main()
{
int m;           //user input
deque <Person> StoreDeque;   //deque
  
Person p1;
strcpy(p1.name,"A");
p1.age = 12;
  
Person p2;
strcpy(p2.name,"B");
p2.age = 14;
  
Person p3;
strcpy(p3.name,"C");
p3.age = 16;
  
Person p4;
strcpy(p4.name,"D");
p4.age = 62;
  
Person p5;
strcpy(p5.name,"E");
p5.age = 32;
  
StoreDeque.push_back(p1);       //initial insertion
StoreDeque.push_back(p2);
StoreDeque.push_back(p3);
StoreDeque.push_back(p4);
StoreDeque.push_back(p5);

cout<<"Enter m:\t";
cin>>m;
  
for (int i=0; i<m; i++)
{
usleep(1000);
if(rand()<100)       //if random value is < 100, push 1 element and pop 0 element.
{
Person p;
strcpy(p.name,"rafael");
p.age = rand();
  
StoreDeque.push_back(p);
}
  
else           //if random value is >= 100, push 0 element and pop 1 element.
{
StoreDeque.pop_front();
}
cout<<endl<<StoreDeque.size()<<endl;
}
  
return 0;
}

Enter m: 4 4 3 2 - . Program f exit code 0 Press ENTER to exit console.D inished with

COMMENT DOWN FOR ANY QUERIES AND

LEAVE A THUMBS UP IF THIS ANSWER HELPS YOU.

Add a comment
Know the answer?
Add Answer to:
(a) Define and implement a Person class, which has data members as described below: Name . Age (b...
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
  • C++ program Create a class Time having data members HH, MM, SS of type integer. Write...

    C++ program Create a class Time having data members HH, MM, SS of type integer. Write a C++ program to do the following: 1. Use a Constructor to initialize HH, MM, SS to 0. 2. Create two Objects of this class and read the values of HH, MM, SS from the user for both objects (Using proper member functions). 3. Use a binary + operator to add these two objects created (Store & display result using separate object). 4. Use...

  • c++ program 1a) Implement a class named ???????. Start by adding the following ?????? members: -...

    c++ program 1a) Implement a class named ???????. Start by adding the following ?????? members: - distance, a variable member of type double. - Non-default constructor which receives a value in order to initialize the variable distance. - Input stream operator to read a value from the command line into the variable distance. 1b) Write a test ???? as follows: - Create a vector to hold instances of type ??????? - Use the following ??? loop to read values from...

  • (java) Please implement your own Dequeue class which has following methods •boolean add(E e)= void addLast(E...

    (java) Please implement your own Dequeue class which has following methods •boolean add(E e)= void addLast(E e)  // two methods are the same. You could implement either one • •void addFirst(E e) • •E getFirst( ) = E peek( ) // two methods are the same. You could implement either one •E getLast( ) •E removeFirst() •E removeLast() Case 1 : Queue •Create a queue which is an instance of Dequeue. The queue should perform with given following input and print...

  • Implement an abstract class named Person and two subclasses named Student and Staff in Java. A person has a name, address, phone number and e-mail address. A student has a credit hour status (freshman...

    Implement an abstract class named Person and two subclasses named Student and Staff in Java. A person has a name, address, phone number and e-mail address. A student has a credit hour status (freshman, sophomore, junior, or senior). Define the possible status values using an enum. Staff has an office, salaray, and date-hired. Implement the above classes in Java. Provide Constructors for classes to initialize private variables. Getters and setters should only be provided if needed. Override the toString() method...

  • 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...

  • This is a repost of my question from before. I am having trouble writting the code...

    This is a repost of my question from before. I am having trouble writting the code for this project. Below is the project description with various methods and classes the code in C++ should implement. Project Description The goal of this project is to design and develop C++ code and algorithms to control a bank of elevators, which services many floors and transports people “efficiently ", that is, as per the given specifications. A second goal is to effectively employ...

  • project-8c Write a class named Employee that has data members for an employee's name, ID_number, salary,...

    project-8c Write a class named Employee that has data members for an employee's name, ID_number, salary, and email_address (you must use those names - don't make them private). Write a function named make_employee_dict that takes as parameters a list of names, a list of ID numbers, a list of salaries and a list of email addresses (which are all the same length). The function should take the first value of each list and use them to create an Employee object....

  • CS 373 Home Work project 11 Create a queue class by priviate inherting the unorderedArrayListType class....

    CS 373 Home Work project 11 Create a queue class by priviate inherting the unorderedArrayListType class. A queue class is a First-In-First-Out data structure. To understand a queue, think of a checkout line at a grocery store: the person at the front is served first (removed), and people are added to the line from the back end. class queue : private unorderedArrayListType { public: bool isEmpty() const; // test whether queue is empty // Post: returns true if queue is...

  • C++ Define the class HotelRoom. The class has the following private data members: the room number...

    C++ Define the class HotelRoom. The class has the following private data members: the room number (an integer) and daily rate (a double). Include a default constructor as well as a constructor with two parameters to initialize the room number and the room’s daily rate. The class should have get/set functions for all its private data members [3pts]. The constructors and the set functions must throw an invalid_argument exception if either one of the parameter values are negative. The exception...

  • Suppose we want to implement a circular queue using an array that has an initial capacity...

    Suppose we want to implement a circular queue using an array that has an initial capacity (maximum number of elements) MAX. A circular queue is like a regular queue except that elements can be enqueued or dequeued by wrapping around it. Assume we enqueue on the tail and dequeue from the head. An example circular queue with sample operations is shown below: head head tail head tail tail head Enqueue(9) a) Write a program in C that implements this circular...

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