Question

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 be constant functions.

  • Create a main function. Create three instances of Person. Assign a name, age, gender and height to each person by using the setters. And print out to the screen the information for each person using the getters.

Task 2: Create a private member variable called totalPerson that is shared by all the objects of the class Person. This member variable keeps track of how many instances of class Person has been created, thus this member variable should increment every time a new instance of class Person is created. You should also implement a member function that returns the value of the totalPerson. You should be able to call this member function even without making any instance of the class Person.

At this time, you should also create a destructor for class Person. Since all member variables are primitive types, there is really nothing to destroy or delete in terms of dynamic memory. However, what should you write in the destructor?

In the main function, add another code that prints out the total number of instances of the Person class. Create another instance of the Person class, this time, it should be dynamically allocated. Assign a name, age, gender and height to this person. And print out the person’s information to the screen.

Print out again the total number of instances of the Person class.

Delete the dynamically created instance of Person.

Print out again the total number of instances of the Person class.

Part 2 - Check Writing

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 two hundred three.

The class should have a single integer member variable

int number;

and a collection of static string members that specify how to translate key dollar amounts into the desired format. For example, you might use static strings such as

string lessThan20[] = {“zero”, “one”, …, “eighteen”, “nineteen”};

string hundred = “hundred”;

string thousand = “thousand”;

The class should have a constructor that accepts a non-negative integer and uses it to initialize the Numbers object. It should have a member function print() that prints the English description of the Numbers object. Demonstrate the class by writing a main program that asks the user to enter a number in the proper range and then prints out its English description.

Task 3: Create another instance of the Person class. After declaration, assign the first person to this new instance by using the assignment operator (=). Print out to the screen the information of this new instance. Is the information the same as the first person information? If it is, why?

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

Part 1

Task 1:

#include <bits/stdc++.h>

using namespace std;

class Person{
private:
string name,gender;
int age,height; // assumption : height is in cm(centimeter)
  
public:
Person(){
name="";
gender="";
age=0;
height=0;
}
  
/* Function to Set Name of a Person */
void setName(string name){
this->name=name;
}
  
/* Function to Set Gender of a Person */
void setGender(string gender){
this->gender=gender;
}
  
/* Function to Set Age of a Person */
void setAge(int age){
this->age=age;
}
  
/* Function to Set Height of a Person */
void setHeight(int height){
this->height=height;
}
  
/* Function to Get Name of a Person */
string getName(){
return name;
}
  
/* Function to Get Gender of a Person */
string getGender(){
return gender;
}
  
/* Function to Get Height of a Person */
int getHeight(){
return height;
}
  
/* Function to Get Age of a Person */
int getAge(){
return age;
}
  
};
int main()
{
Person p[3]; // creating 3 instance of Person class
  
// initialising 3 instance of Person class
p[0].setName("Tanuj");
p[0].setHeight(162);
p[0].setAge(21);
p[0].setGender("Male");
  
p[1].setName("Sonam");
p[1].setHeight(150);
p[1].setAge(20);
p[1].setGender("Female");
  
p[2].setName("Ajay");
p[2].setHeight(155);
p[2].setAge(25);
p[2].setGender("Male");
  
  
//displaying person information
for(int i=0;i<3;i++){
cout<<"Name : "<<p[i].getName()<<"\nGender : "<<p[i].getGender()<<"\nAge : "<<p[i].getAge()<<"\nHeight : "<<p[i].getHeight()<<" cm"<<endl<<endl;
}
  
return 0;
}

Task 2:

#include <bits/stdc++.h>

using namespace std;

class Person{
private:
string name,gender;
int age,height; // assumption : height is in cm(centimeter)
static int totalPerson; // static data member is used so that it can be used be shared by all the object of class
  
public:
Person(){
name="";
gender="";
age=0;
height=0;
totalPerson++;
}
  
/* Function to Set Name of a Person */
void setName(string name){
this->name=name;
}
  
/* Function to Set Gender of a Person */
void setGender(string gender){
this->gender=gender;
}
  
/* Function to Set Age of a Person */
void setAge(int age){
this->age=age;
}
  
/* Function to Set Height of a Person */
void setHeight(int height){
this->height=height;
}
  
/* Function to Get Name of a Person */
string getName(){
return name;
}
  
/* Function to Get Gender of a Person */
string getGender(){
return gender;
}
  
/* Function to Get Height of a Person */
int getHeight(){
return height;
}
  
/* Function to Get Age of a Person */
int getAge(){
return age;
}
  
// Function to Get Total Number of Person */
static int getTotalPerson(){
return totalPerson;
}
  
// Destructor of Person
~Person(){
cout<<"\nDestructor is called";
}
};

// initialising totalPerson
int Person::totalPerson=0;

int main()
{
Person* p= new Person[3]; // creating 3 object of Person Class Dynamically
  
// initialising 3 instance of Person class
p[0].setName("Tanuj");
p[0].setHeight(162);
p[0].setAge(21);
p[0].setGender("Male");
  
p[1].setName("Sonam");
p[1].setHeight(150);
p[1].setAge(20);
p[1].setGender("Female");
  
p[2].setName("Ajay");
p[2].setHeight(155);
p[2].setAge(25);
p[2].setGender("Male");
  
  
//displaying person information
for(int i=0;i<3;i++){
cout<<"Name : "<<p[i].getName()<<"\nGender : "<<p[i].getGender()<<"\nAge : "<<p[i].getAge()<<"\nHeight : "<<p[i].getHeight()<<" cm"<<endl<<endl;
}
  
// displaying total person
int totalPerson = Person::getTotalPerson();
cout<<"Total Person = "<<totalPerson<<endl;
  
  
// destroying an object of Person class
delete[] p;
  
return 0;
}

Add a comment
Know the answer?
Add Answer to:
About Classes and OOP in C++ Part 1 Task 1: Create a class called Person, which...
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...

  • Create a C++ project with 2 classes, Person and Birthdate. The description for each class is...

    Create a C++ project with 2 classes, Person and Birthdate. The description for each class is shown below: Birthdate class: private members: year, month and day public members: copy constructor, 3 arguments constructor, destructor, setters, getters and age (this function should return the age) Person class: private members: firstName, lastName, dateOfBirth, SSN public members: 4 arguments constructor (firstName, lastName, datOfBirth and SNN), destructor and printout Implementation: - use the separated files approach - implement all the methods for the 2...

  • 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++ Program 3 Create a class Person Include three(3) variables: firstName, lastName, YearOfBirth Write default and...

    C++ Program 3 Create a class Person Include three(3) variables: firstName, lastName, YearOfBirth Write default and parameterized constructors Write getters and setters for each variable. Declare 2 instances of the person class P1 and P2, one for both default and parm constructors Declare ptrPerson1 and ptrPerson2. Assign ptrPerson1 address of P1 Assign ptrPerson2 address of P2 Call all the getters and setters using the ARROW notation to test the class. Example:    ptrP1à getFirstName()

  • Design and implement a C++ class called Date that has the following private member variables month...

    Design and implement a C++ class called Date that has the following private member variables month (int) day (nt) . year (int Add the following public member functions to the class. Default Constructor with all default parameters: The constructors should use the values of the month, day, and year arguments passed by the client program to set the month, day, and year member variables. The constructor should check if the values of the parameters are valid (that is day is...

  • c++ please Given the following skeleton of an unsorted list class that uses an unsorted linked...

    c++ please Given the following skeleton of an unsorted list class that uses an unsorted linked list: template<class ItemType> struct NodeType {                 ItemType item;                 NodeType* next; }; template<class ItemType> class UList { public:                 UList(); // default constrctor                 UList(const UList &x); // we implement copy constructor with deep copy                 UList& operator = (UList &x); // equal sign operator with deep copy                 bool IsThere(ItemType item) const; // return true of false to indicate if item is...

  • Create a NetBeans project called "Question 1". Then create a public class inside question1 package called Author according to the following information (Make sure to check the UML diagram)

    Create a NetBeans project called "Question 1". Then create a public class inside question1 package called Author according to the following information (Make sure to check the UML diagram) Author -name:String -email:String -gender:char +Author(name:String, email:String, gender:char) +getName():String +getEmail):String +setEmail (email:String):void +getGender():char +tostring ):String . Three private instance variables: name (String), email (String), and gender (char of either 'm' or 'f'): One constructor to initialize the name, email and gender with the given values . Getters and setters: get Name (), getEmail() and getGender (). There are no setters for name and...

  • Create a class hierarchy to be used in a university setting. The classes are as follows:...

    Create a class hierarchy to be used in a university setting. The classes are as follows: The base class is Person. The class should have 3 data members: personID (integer), firstName(string), and lastName(string). The class should also have a static data member called nextID which is used to assign an ID number to each object created (personID). All data members must be private. Create the following member functions: o Accessor functions to allow access to first name and last name...

  • In C++ Write a program that contains a class called VideoGame. The class should contain the...

    In C++ Write a program that contains a class called VideoGame. The class should contain the member variables: Name price rating Specifications: Dynamically allocate all member variables. Write get/set methods for all member variables. Write a constructor that takes three parameters and initializes the member variables. Write a destructor. Add code to the destructor. In addition to any other code you may put in the destructor you should also add a cout statement that will print the message “Destructor Called”....

  • JAVA :The following are descriptions of classes that you will create. Think of these as service...

    JAVA :The following are descriptions of classes that you will create. Think of these as service providers. They provide a service to who ever want to use them. You will also write a TestClass with a main() method that fully exercises the classes that you create. To exercise a class, make some instances of the class, and call the methods of the class using these objects. Paste in the output from the test program that demonstrates your classes’ functionality. Testing...

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