3. Based on quiz 2, add a global friend function and a friend class to Person. The friend function and friend class just

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

3. Based on quiz 2, add a global friend function and a friend class to Person. The friend function and friend class just

Post by answerhappygod »

3 Based On Quiz 2 Add A Global Friend Function And A Friend Class To Person The Friend Function And Friend Class Just 1
3 Based On Quiz 2 Add A Global Friend Function And A Friend Class To Person The Friend Function And Friend Class Just 1 (11.38 KiB) Viewed 27 times
Here is the quiz 2 code:
// C++ program to Count the number of objects
// using the Static member function
#include <iostream>
using namespace std;
class Person {
//private variables
private :
int objNo;
string name;
static int objCnt;
//public member methods
public:
// default constructor to assign the number to
each person object.
Person( )
{
objNo = ++objCnt;
}
// constructor to also add a name to
the object
Person(string n){
name = n;
objNo = ++objCnt;
}
//destructor to reduce the object counts at the
end
~Person()
{
--objCnt;
}
//non static getter for object
number
void printObjNumber(void)
{
cout
<< "object number :" << objNo << "\n";

}
// non static getter for object
name;
void printObjName(void){
cout << name
<< " is my name.\n";
}
//static getter for object count
static void printObjCount(void)
{
cout
<< "count:" << objCnt<< "\n";
}

};
int Person::objCnt;
// initialize the object count for counting all the object's
created after this line this needs to be outside of main since
Member function definitions belong in the scope where the class is
defined. In this case, global scope.
//driver code
int main()
{
// let's create 2 objects
Person p1, p2;
//checking the count
// calling the static method with the classname
Person::printObjCount();
//next object
Person p3;
//and does the count update?
Person::printObjCount();
// let's check thier numbers and calling the non
static's with object name
p1.printObjNumber();
p2.printObjNumber();
p3.printObjNumber();

//similarly we can also assign
names to these objects
Person p4("rick");
//check
p4.printObjName();
//updated count
Person::printObjCount();
return 0;
}
please use the quiz 2 code and follow the quiz 3 rule to
finish the quiz 3 question.
3. Based on quiz 2, add a global friend function and a friend class to Person. The friend function and friend class just read the static member variable in Person and output the value. In main(), test the friend function and friend class
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply