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.
#include <iostream>
#include "dogclass.h"
usingnamespace 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
}
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;
}