Assignment 4B: Complete either question 1 or question 2 below. 1. Design a class
ID: 642184 • Letter: A
Question
Assignment 4B: Complete either question 1 or question 2 below.
1. Design a class of type family and define an object from this class.
Include in the class:
private members of strings for husband, wife, son, daughter1, daughter2, and a pointer to family. (Hint regarding the private pointer to family: All you are doing is making a pointer to a class family. By making it private you are deciding that you cannot change the class.)
public void function without arguments called initialize.
void friend function called output with a pointer to family as an argument.
Declare a void function prototype (that will be used for output) for output. Have the output function output on the screen husband is xxxx, wife is xxxx, son is xxxx, daughter1 is xxxx, and daughter2 is xxxx on separate lines. (The xxxx are the respective names used in initialize below.)
Declare a family type object called Simpson.
Include all of the above in a function that uses the public function initialize in Simpson to initialize the pointer to the address of Simpson, and initializes husband as "Donald Simpson," wife as "Shirley Simpson," son as "Horatio Simpson," daughter1 as "Harriet Simpson," and daughter2 as "Hillary Simpson." Also
include in the public function family:
initialize the output function.
Run the program. Explain in a comment at the top of your program why the program would not work as is if the output
function was private and not a friend.
2. Create a person class to represent a person. (You may call the class
personType.) To simplify things, have the class have 2 variable members for the person
Explanation / Answer
//QUESTION : 2
#include <iostream>
using namespace std;
//Both the constructors can be replaced one constructor as shown below;
/*PersonType(string first="abc",string last="def")
{
firstName = first;
lastName = last;
}
*/
class PersonType
{
private:
string firstName;
string lastName;
public:
PersonType() //Default constructor
{
firstName = "abc";
lastName = "def";
}
PersonType(string first,string last) //Parameterized Constructor
{
firstName = first;
lastName = last;
}
void setName(string first,string last)
{
firstName = first;
lastName = last;
}
string getName()
{
string name = firstName+" "+lastName;
return name;
}
void printName()
{
cout<<" Name : "<<firstName<<" "<<lastName<<endl;
}
};
int main()
{
PersonType person1; //Default Constructor called for object person1
person1.printName(); //Display default names i.e. "abc def"
person1.setName("Michael","Jordan"); //setName(string first,string last) called
cout<<" You Entered : "<<person1.getName(); //getName() function called and returned concatenated name i.e. //firstname+lastname
PersonType person2("Emily","Jackson"); //Parameterised constructor called for object person2
//with default parameters "Emily" and "Jackson"
person2.printName(); //printName() called by person2 object to display the name
return 0;
//Hope it was helpful :)
//Feel free to ask any doubts
}