Details of this Paper

Base Class Access Specifier on the Table

Description

solution


Question

Base Class Access Specifier on the Table 15-1;The base class access specifier determines the access privillege of a derived class to its base class.;In this exercise, we are going to inspect the access at three different scope;1. Inside the Base class by the base class constuctor.;2. Inside the Derived class by the derived class constructor.;3. At the application, by referencing to the instance methods.;Detail information on the requirement is inside the starter.;Starter: lab11_starter.cpp;Submit: lab11.cpp;Table 15-1;Base Class Access;Specification How Members of the Base Class Appear in the Derived Class.;private;Private members of the base class are inaccessible to the derived class.;Protected members of the base class become private members of the;derived class.;Public members of the base class become private members of the derived class.;protected;Private members of the base class are inaccessible to the derived class.;Protected members of the base class become protected members of the;derived class.;Public members of the base class become protected members of the derived;class.;public;Private members of the base class are inaccessible to the derived class.;Protected members of the base class become protected members of the;derived class.;Public members of the base class become public members of the derived class.;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~;lab11_starter.cpp is;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~;#include;class Base;public;int mPublic;private;int mPrivate;protected;int mProtected;public;Base(): mPublic(0), mPrivate(0), mProtected(0);std::cout << "nmPublic: " << mPublic << std::endl;<< "mPrivate: " << mPrivate << std::endl;<< "mProtected: " << mProtected << "n... created Basen;int get_mPublic() {return mPublic,};int get_mPrivate() {return mPrivate,};int get_mProtected() {return mProtected,};class Pub: public Base;public;// Public inheritance means;// mPublic stays public;int get_Pub_mPublic() { return mPublic, };// mPrivate becomes inaccessible;// mProtected stays protected;int get_Pub_mProtected() { return mProtected, };Pub();// The derived class always uses the immediate parent's class access specifications;// Thus, Pub uses Base's access specifiers;mPublic = 1, // okay: anybody can access public members;//mPrivate = 2, // not okay: derived classes can't access private members in the base class!;mProtected = 3, // okay: derived classes can access protected members;std::cout << "... inherited and modified by cPubn;<< "mPublic: " << mPublic << std::endl;<< "mPrivate not available, use get_mPrivate(): " << get_mPrivate() << std::endl;<< "mProtected: " << mProtected << "n... created cPubn;class Pri: private Base;// Fill the blank with;// 3 getters, if possible.;// Default constructor with same initialization values (1, 2, 3), if possible.;// Also, make the Default constructor display the private data values, either accessing directly or via base class getter.;// If the above required feature can not be done, explain why in comment.;class Pro: protected Base;// Fill the blank with;// 3 getters, if possible.;// Default constructor with same initialization values (1, 2, 3), if possible.;// Also, make the Default constructor display the private data values, either accessing directly or via base class getter.;// If the above required feature can not be done, explain why in comment.;int main();// Outside access uses the access specifiers of the class being accessed.;// In this case, the access specifiers of cPub. Because Pub has inherited publicly from Base;// no access specifiers have been changed.;std::cout << "n... creating cPub;Pub cPub;cPub.mPublic = 4, // okay: anybody can access public members;// cPub.mPrivate = 5, // not okay: can not access private members from outside class;//cPub.mProtected = 6, // not okay: can not access protected members from outside class;std::cout << "... access cPub by applicationn;<< "cPub.get_Pub_mPublic(): " << cPub.get_Pub_mPublic() << std::endl;<< "cPub.get_Pub_mPrivate(): " << "does not exist!" << std::endl;<< "cPub.get_mPrivate(): " << " inaccessible, a private Base method!" << std::endl;<< "cPub.get_Pub_mProtected(): " << cPub.get_Pub_mProtected() << "n;std::cout << "n... creating cPri;Pri cPri;// Fill the blank below;// 1. to modify the value of the private data memeber of the base class to (4, 5, 6);// no need to explore the modification by the setter() method.;// 2. to retrieve and display the value of base class data members via either;// cPro class methods, if possible.;// 3. If the private data member of the base class can not be accessed, explain.;std::cout << "n... creating cPro;Pro cPro;// Fill the blank below, same requirement as above.;// However, we can still can access Base members as normal through Base;Base cBase;cBase.mPublic = 4, // okay, mPublic is public;//cBase.mPrivate = 5, // not okay, mPrivate is private;//cBase.mProtected = 6, // not okay, mProtected is protected;std::cout << "... access cBase by applicationn;<< "cBase.mPrublic: " << cBase.mPublic << std::endl;<< "cBase.mPrivate: " << "inaccessible, a private member!" << std::endl;<< "cBase.mProtected: " << " inaccessible, a protected member!" << std::endl

 

Paper#67236 | Written in 18-Jul-2015

Price : $22
SiteLock