Question

c++ program Implement a class called Person with the following members: 1a. ????, a private variable...

c++ program

Implement a class called Person with the following members:

1a. ????, a private variable of type ??????

1b. ???, a private variable of type ???

1c. Default constructor to set name to "" and age to 0

1d. Non-default constructor which accepts two parameters for name and age

1e. A copy constructor

1f. The post-increment operator

1g. The pre-decrement operator

1h. The insertion and extraction stream operators >>and <<

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

Source Code in C++:

#include <iostream>
using namespace std;

class Person
{
private:
int age; //class variables
string name;
public:
Person() //default constructor
{
age=0;
name="";
}
Person(int age,string name) //parametrized constructor
{
this->age=age;
this->name=name;
}
Person(const Person &p) //copy constructor
{
this->age=p.age;
this->name=p.name;
}
Person operator++() //overloading pre-increment operator
{
age++;
return *this;
}
Person operator++(int) //overloading post-increment operator
{
const Person old(*this);
++(*this);
return old;
}
Person operator--() //overloading pre-decrement operator
{
age--;
return *this;
}
//overloading stream operators
friend ostream& operator <<(ostream &out,const Person &p)
{
out << "Name: " << p.name << endl << "Age: " << p.age;
}
friend istream& operator >>(istream &in,Person &p)
{
cout << "Name: ";
in >> p.name;
cout << "Age: ";
in >> p.age;
}
};
int main()
{
//testing the class
Person p;
cin >> p;
cout << "Original: " << endl;
cout << p << endl;
cout << "Post Increment: " << endl;
cout << p++ << endl;
cout << "After Post Increment: " << endl;
cout << p << endl;
cout << "Pre Decrement: " << endl;
cout << --p << endl;
return 0;
}

Output:

Add a comment
Know the answer?
Add Answer to:
c++ program Implement a class called Person with the following members: 1a. ????, a private variable...
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 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...

  • Design a class named Month. The class should have the following private members:

    Design a class named Month. The class should have the following private members:   • name - A string object that holds the name of a month, such as "January", "February", etc.   • monthNumber - An integer variable that holds the number of the month. For example, January would be 1, February would be 2, etc. Valid values for this variable are 1 through 12.  In addition, provide the following member functions:• A default constructor that sets monthNumber to 1 and name...

  • 1a. Create a class named Inventory - Separate declaration from implementation (i.e. Header and CPP files)...

    1a. Create a class named Inventory - Separate declaration from implementation (i.e. Header and CPP files) 1b. Add the following private member variables - a int variable named itemNumber - an int variable named quantity - a double variable named cost - a double variable named totalCost. Total cost is defined as quantity X cost. - updateTotalCost() which takes no arguments and sets the values of the variable totalCost. 1c. Add the following public functions: - Accessor and Mutators for...

  • Language: C++ Create a class named 'Salesman' that shall inherit from a class called 'Person' and...

    Language: C++ Create a class named 'Salesman' that shall inherit from a class called 'Person' and will add various functionality. We will store the following private variables specific to Warehouse: . a std::vector of float values which indicate the monetary values of each sale made by this salesman . a std::string which stores that salesman's position title . a float which stores their commission percentage, i.e., the percentage of sales they receive as a paycheck . a float which stores...

  • Language: C++ Create an abstract base class person. The person class must be an abstract base...

    Language: C++ Create an abstract base class person. The person class must be an abstract base class where the following functions are abstracted (to be implemented in Salesman and Warehouse): • set Position • get Position • get TotalSalary .printDetails The person class shall store the following data as either private or protected (i.e., not public; need to be accessible to the derived classes): . a person's name: std::string . a person's age: int . a person's height: float The...

  • in c++ Define and implement the class Employee with the following requirements: private data members string...

    in c++ Define and implement the class Employee with the following requirements: private data members string type name a. b. double type hourlyRate 2. public member functions a. default constructor that sets the data member name to blank"and hourlyRate to zero b. A constructor with parameters to initialize the private data members c. Set and get methods for all private data members d. A constant method weeklyPay() that receives a parameter representing the number of hours the employee worked per...

  • C++ program to implement inheritance with following requirements, Classes :- Animal.cpp, bird.cpp, canine.cpp, main.cpp (to test...

    C++ program to implement inheritance with following requirements, Classes :- Animal.cpp, bird.cpp, canine.cpp, main.cpp (to test it) :- -You may need getters and setters also. 1. Declare and define an animal base class: animal stores an age (int), a unique long ID (a boolean that is true if it is alive, and a location (a pair of double). animal requires a default constructor and a 3 parameter constructor. Both constructors should set the unique ID automatically. They should also set...

  • Part 1. (60 pts) 1. Define an Address class in the file Address.h and implement the...

    Part 1. (60 pts) 1. Define an Address class in the file Address.h and implement the Address class in Address.cpp. a. This class should have two private data members, m_city and m_state, that are strings for storing the city name and state abbreviation for some Address b. Define and implement public getter and setter member functions for each of the two data members above. Important: since these member functions deal with objects in this case strings), ensure that your setters...

  • C++ NEED AS SOON AS POSSIBLE! BigInt class is used for the mathematical operations that involve...

    C++ NEED AS SOON AS POSSIBLE! BigInt class is used for the mathematical operations that involve very big integer calculations that are outside the limit of all available primitive data types. For example, factorial of 100 contains 158 digits in it so we can’t store it in any primitive data type available. We can store as large Integer as we want in it. Your goal is to overload the operators for a generic “BigInt” class. You will need to write...

  • implement a C++ class name Vector. The Vector class contains a private double array of length...

    implement a C++ class name Vector. The Vector class contains a private double array of length 2 named elements that represents a two-dimensional vector. We will also implement an overloaded multiplication operator (*) that accepts a single Vector variable by reference and returns the Dot Product between the current Vector object and the one pointed to by the function argument in form of a double. Please recall that the dot product of two vectors a =(21,92) and 5 = (b1,b2)...

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