What purpose does a class access specification have?

I have a program that has a "derived" class that inherits the public members of a "base" class. But I'm a little confused on what the class access specifier actually does.

Can anyone explain to me the purpose of a class access specification? (not member access specification)

The code below contains the derived class as well as the driver program.

(note. the program has no errors)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include "dogclass.h"

using namespace std;

class Rottweiler : public Dog         //What would happen if i changed "public" to "private"?
{
    int weight_of_dog;
    int height_of_dog;
    string color_of_dog;
    int age_of_dog;

public:

    Rottweiler()
    {

        weight_of_dog = 0;
        height_of_dog = 0;
        color_of_dog = "";
        age_of_dog = 0;
    }


    Rottweiler (int w, int h, string c, int a)
    {

        weight_of_dog = w;
        height_of_dog = h;
        color_of_dog = c;
        age_of_dog = a;
        setMember ("Rottweiler");       //passing variable to a function in base class
    }

};


int main ()
{
    string choice2;
    int choice3;
    int choice4;
    int choice5;

    cout << "How much does the dog weight in pounds?" << endl;
    cin >> choice3;

    cout << "What is the height of the dog in feet?" << endl;
    cin >> choice4;

    cout << "What is the color of the dogs fur?" << endl;
    cin >> choice2;

    cout << "How old is the dog?" << endl;
    cin >> choice5;

    Rottweiler object ( choice3, choice4, choice2, choice5);


    cout << object.getMember();    //calling function from base class
}







And here is the file that has the base class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#define DOGCLASS_H

using namespace std;

class Dog
{
    string breed_of_dog;

public:

    void setMember (string n)
    {
        cout << "Breed of dog has been stored" << "\n";
        breed_of_dog = n;
    }

    string getMember ()
    {
        return breed_of_dog;
    }

};

Last edited on
See http://www.tutorialspoint.com/cplusplus/cpp_inheritance.htm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Foo {
public:
  void foo();
};

class Bar : public Foo {
};

class Gaz : private Foo {
};

int main() {
  Bar b;
  b.foo(); // OK, b IS-A Foo object

  Gaz g;
  g.foo(); // Error.  g has member foo(), but it is private
  // Gaz HAS-A Foo (privately)

  return 0;
}

See: http://www.cplusplus.com/forum/beginner/12899/#msg61907
Topic archived. No new replies allowed.