0

My professor assigned a lab with the following requirements: Requirements

Now, I thought simple enough Ill make several derived classes from one to show the inheritance of the private, public, and protected data. Apon asking for clarification that this is what I was supposed to do I was told to use only a single class.

I then wrote this, which he O-K as the solution but its been bugging me because this just uses the public to access all the variables with a string argument(the string streams are returned because of some lab rules):

class cls {
public:
    stringstream ss;
    int data[4] = { 1,2,3,4 };
    void print(int d[]) {
        for (int i = 0; i < 4; i++)
            ss << i << ',';
    }
    string pass(string s) {
        if (s == "A") {
            print(data);
            print(pdata);
            print(prdata);
        }
        else if(s=="B"){
            print(pdata);
            print(prdata);
        }else if(s=="C"){
            print(prdata);
        }
        return ss.str();
    }
protected:
    int pdata[4] = { 10,20,30,40 };
private:
    int prdata[4] = { 100,200,300,400 };
};

How is that the solution to the lab? With my understanding of access modifiers, it isn't using any?

  • Let's just say bad teacher is everywhere. –  May 01 '17 at 16:01
  • This seems like sort of a shit lab assignment for getting you to understand access modifiers. The modifiers seem entirely superfluous, and would honestly just be confusing to someone looking at the class. I'd grab a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to compensate, if you don't have time just look up encapsulation and how to write a good code interface in C++. – George May 01 '17 at 16:04
  • 1
    Hi there - well yes, there *are* access modifiers in your program, but they don't play any vital role - *yet*. Probably this example is just to be seen as an early stage to what is to come. A possible next stage would be to introduce derived classes and teaching those derived classes to access on public, protected and private data of their base class, respectively. Regards, M. – Micha May 01 '17 at 16:35

0 Answers0