Reading
---------------------------------------------------------------------------------------------------------------------------------------------------------------
private versus public versus protected
File: private_protected_private_members.h
class A
{
public:
int x;
protected:
int y;
private:
int z;
public:
void SetX(int in){x = in;}// any class function can access public members
void SetY(int in){y = in;}// any class function can access protected members
void SetZ(int in){z = in;}// any class function can access private members
};
class B : public A
{
// x is public
// y is protected
// z is not accessable at all even by member functions
// SetX, SetY and SetZ are sill accessable
};
int main()
{
A one;
one.x =55; // valid, its public
one.y =200; // not valid, protected members are not accessable outside the class;
one.z =300; // not valid, protected members are not accessable outside the class;
one.SetX(1);// valid, the function is public
one.SetY(2);// valid, the function is public
one.SetZ(3);// valid, the function is public
B two;
two.x = 5; // valid
two.y = 10; // not valid, protected members are not accessable outide the class
two.z = 15; // not valid we can even access z directory from functions of the class
two.SetX(5);// valid
two.SetY(10);// valid
two.SetZ(15);// valid
}
ONLY public data members and public functions can be called or accessed from outside the class ( in our main.cpp )
member functions have full access to all public , private and protected data members
---------------------------------------------------------------------------------------------------------------------------------------------------------------
Inheritance
when you define a class in c++ you can inherit from it and create similar classes
this is usefull to create something similar to a class you have already but add some more data members or change the behavior slightly
for example
---------------------------------------------------------------------------------------------------------------------------------------------------------------
File: Automobile.h
#ifndef AUTOMOBILE_H
#define AUTOMOBILE_H
#include <string>
using namespace std;
// The Automobile class holds general data
// about an automobile in inventory.
class Automobile
{
protected:
string make; // The auto's make
int model; // The auto's year model
int mileage; // The auto's mileage
double price; // The auto's price
public:
// Default constructor
Automobile()
{ make = "";
model = 0;
mileage = 0;
price = 0.0; }
// Constructor
Automobile(string autoMake, int autoModel,
int autoMileage, double autoPrice)
{ make = autoMake;
model = autoModel;
mileage = autoMileage;
price = autoPrice; }
// Accessors
string getMake() const
{ return make; }
int getModel() const
{ return model; }
int getMileage() const
{ return mileage; }
double getPrice() const
{ return price; }
};
#endif
---------------------------------------------------------------------------------------------------------------------------------------------------------------
now we can create the car class and use Automobile as its parrent class
the car will "inherit" from its parent
it will have all the data members and functions as an Automobile but we will add in
int doors as well as an accessor function and an additional constructor
File: Car.h
#ifndef CAR_H
#define CAR_H
#include "Automobile.h"
#include <string>
using namespace std;
// The Car class represents a car.
class Car : public Automobile
{
private:
int doors;
public:
// Default constructor
Car() : Automobile()
{ doors = 0; }
// Constructor #2
Car(string carMake, int carModel, int carMileage,
double carPrice, int carDoors) :
Automobile(carMake, carModel, carMileage, carPrice)
{ doors = carDoors; }
// Accessor for doors attribute
int getDoors()
{ return doors; }
};
#endif
---------------------------------------------------------------------------------------------------------------------------------------------------------------
now we can create the SUV class and use Automobile as its parrent class
the car will "inherit" from its parent
it will have all the data members and functions as an Automobile but we will add in
int passengers as well as an accessor function and an additional constructor
File: SUV.h
#ifndef SUV_H
#define SUV_H
#include "Automobile.h"
#include <string>
using namespace std;
// The SUV class represents a SUV.
class SUV : public Automobile
{
private:
int passengers;
public:
// Default constructor
SUV() : Automobile()
{ passengers = 0; }
// Constructor #2
SUV(string SUVMake, int SUVModel, int SUVMileage,
double SUVPrice, int SUVpassengers) :
Automobile(SUVMake, SUVModel, SUVMileage, SUVPrice)
{ passengers = SUVpassengers; }
// Accessor for passengers attribute
int getPassengers()
{ return passengers; }
};
#endif
---------------------------------------------------------------------------------------------------------------------------------------------------------------
data members can be public, protected or private
and a class has inheritance levels of
public, protected and private as well
File: private_protected_private_inheritance.h
you can think of the inheritance level in terms of escalation
if you use public inheritance
public and protected data members and functions will stay as they are
if you use protected inheritance... any inherited members or functions will be escalated to at least protected
if you use private inheritance... any inherited members or functions will be escalated to at least private
---------------------------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------------------------