Question

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()

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

#include <iostream>

#include <string>

using namespace std;

class Person {

private:

//data members

string firstName;

string lastName;

int yearOfBirth;

public:

//default constructor

Person() :firstName(""), lastName(""), yearOfBirth(0) {}

/*Year of birth can not be zero

so is the name

So basically we are placing a dummy data for default constructor*/

//parameterised constructor

Person(string fName, string lName, int year) : firstName(fName), lastName(lName), yearOfBirth(year) {}

//setter function for first name

void setFirstName(string fName) {

firstName = fName;

}

//getter function for first name

string getFirstName() {

return firstName;

}

//setter function for last name

void setLastName(string lName) {

lastName = lName;

}

//getter function for last name

string getLastName() {

return lastName;

}

//setter function for year of birth

void setYearOfBirth(int year) {

yearOfBirth = year;

}

//getter function for year of birth

int getYearOfBirth() {

return yearOfBirth;

}

};

int main() {

Person person1; //person class object , default constructor

Person person2("Green", "Man", 2000); //object by parameterised constructor

//Two Person type pointer

Person *ptrPerson1;

Person *ptrPerson2;

//assigning pointer to address

ptrPerson1 = &person1;

ptrPerson2 = &person2;

ptrPerson1->setFirstName("Red");

ptrPerson1->setLastName("Women");

ptrPerson1->setYearOfBirth(2005);

cout << "Person one detail : ";

cout << ptrPerson1->getFirstName() << " " << ptrPerson1->getLastName() << " " << ptrPerson1->getYearOfBirth() << endl;

cout << "Person two detail : ";

cout << ptrPerson2->getFirstName() << " " << ptrPerson2->getLastName() << " " << ptrPerson2->getYearOfBirth() << endl;

}

Add a comment
Know the answer?
Add Answer to:
C++ Program 3 Create a class Person Include three(3) variables: firstName, lastName, YearOfBirth Write default and...
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++ create a class called person. A person can have a name, address, age. For...

    Using C++ create a class called person. A person can have a name, address, age. For this class create a default constructor and parameterized constructor. Show the main as int main() { } ----------------------------------------------------- Show how you will define the class and call it in the main. Only create two instances/objects in main using the two created constructors.

  • In Java: Develop a simple class for a Student. Include class variables; StudentID (int), FirstName, LastName,...

    In Java: Develop a simple class for a Student. Include class variables; StudentID (int), FirstName, LastName, GPA (double), and Major. Extend your class for a Student to include classes for Graduate and Undergraduate. o Include a default constructor, a constructor that accepts the above information, and a method that prints out the contents FOR EACH LEVEL of the object, and a method that prints out the contents of the object. o Write a program that uses an array of Students...

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

  • A hard c++ problem ◎Write a c++ program”Student.h”include Private data member, string firstName; string lastName; double...

    A hard c++ problem ◎Write a c++ program”Student.h”include Private data member, string firstName; string lastName; double GPA(=(4.0*NumberOfAs+3.0*NumberOfBs+2.0*NumberOfCs+1.0*NumberOfDs)/( As+Bs+Cs+Ds+Fs)); Public data member, void setFirstName(string name); string getFirstName() const; void printFirstName() const; void getLastName(string name); string getLastName() const; void printLastName() const; void computeGPA(int NumberOfAs,int NumberOfBs,int NumberOfCs,int NumberOfDs,int NumberOfFs); double getGPA() const; double printGPA() const; A destructor and an explicit default constructor initializing GPA=0.0 and checking if GPA>=0.0 by try{}catch{}. Add member function, bool operator<(const Student); The comparison in this function based on...

  • C++ program Create a Student class that contains three private data members of stududentID (int), lastName...

    C++ program Create a Student class that contains three private data members of stududentID (int), lastName (string), firstName(string) and a static data member studentCount(int). Create a constructor that will take three parameters of studentID, lastName and firstName, and assign them to the private data member. Then, increase the studentCount in the constructor. Create a static function getStudentCount() that returns the value of studentCount. studentCount is used to track the number of student object has been instantiated. Initialize it to 0...

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

  • create a java class named Person that has 3 attributes: name, age and weight. include a...

    create a java class named Person that has 3 attributes: name, age and weight. include a default and parametrize constructor. include all setters and getters needed and a method that is used to display the information about the person

  • Help with C++ please Create a class called ClassQuiz. ClassQuiz will maintain quiz grades for students...

    Help with C++ please Create a class called ClassQuiz. ClassQuiz will maintain quiz grades for students in a class. The class has the following member variables: int numStudents // holds the number of students in the class double* grades // to point to a dynamically allocated array of grades The class has the following public functions: +default constructors //set numStudents = 0; and grades = nullptr; +parameterized constructors //accepts numStudents and creates an array with size = numStudents; +AcceptGrades //...

  • answer in c# console application. File l/O Create a Person class (firstName, lastName, phoneNumber, gender (Use...

    answer in c# console application. File l/O Create a Person class (firstName, lastName, phoneNumber, gender (Use radioButtons for gender in the GUI) ) Create a List<Person> Create a GUI that will collect the Person information from the user and have 3 buttons: Add, Display, Read. Add button Add the just created person to List<Person>, and append to a text file Display button > print out all persons currently in the List<Person> in a label in a visually attractive way Read...

  • Design a class for python named PersonData with the following member variables: lastName firstName address city...

    Design a class for python named PersonData with the following member variables: lastName firstName address city state zip phone Write the appropriate accessor and mutator functions for these member variables. Next, design a class named CustomerData , which is derived from the PersonData class. The CustomerData class should have the following member variables: customerNumber mailingList The customerNumber variable will be used to hold a unique integer for each customer. The mailingList variable should be a bool . It will be...

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