About classes:
This will give some important information about classes. To know more please consult with any C++ books.
The class keyword declares a class type or defines an object of a class type.
- Four pillars of OOP are achieved from classes
- Class
- Class
-Encapsulation
-Data hiding
-Inheritance
-Polymorphism ( See this link )
-Consists of both data and functions/methods
-Defines properties and behavior of set of entities
-Defines a new type (like int, float etc.)
-An instance of class
-Has identity, state, and behavior
How to define a class is shown in the following diagram. The syntax for a class declaration is as follows:
class : base-list
{
member-list
};
- class
- base-list
- member-list
-The class keyword.
-Optional list of classes this class will derive its members from. Each base class name can be preceded by an access specifier (public, private, protected) and the virtual keyword.
-The member list of a class may be divided into private, protected and public sections using keywords known as access specifiers. A colon : must follow the access specifier. These sections need not be contiguous, that is, any of these keywords may appear several times in the member list. The keyword designates the access of all members up until the next access specifier or the closing brace.
// class.cpp
// compile with: /EHsc
// Example of the class keyword
// Exhibits polymorphism/virtual functions.
#include <iostream.h>
#include <string.h>
#define TRUE = 1
using namespace std;
class dog
{
public:
dog()
{
_legs = 4;
_bark = true;
}
void setDogSize(string dogSize)
{
_dogSize = dogSize;
}
virtual void setEars(string type) // virtual function
{
_earType = type;
}
private:
string _dogSize, _earType;
int _legs;
bool _bark;
};
class breed : public dog
// breed is derived from dog with public keyword
{
public:
breed( string color, string size)
{
_color = color;
setDogSize(size);
}
string getColor()
{
return _color;
}
// virtual function redefined
void setEars(string length, string type)
{
_earLength = length;
_earType = type;
}
protected:
string _color, _earLength, _earType;
};
int main()
{
dog mongrel;
breed labrador("yellow", "large");
mongrel.setEars("pointy");
labrador.setEars("long", "floppy");
cout << "Cody is a " << labrador.getColor() << " labrador" << endl;
return 0;
}
Member access control:
Following table describes when we use different control access (for both methods and attributes).
Access control helps prevent you from using objects in ways they were not intended to be used. This protection is lost when explicit type conversions (casts) are performed.
The default access to class members (members of a class type declared using the class keyword) is private; the default access to struct and union members is public.
Type of Access
Meaning
private
Class members declared as private can be used only by member functions and friends (classes or functions) of the class.
protected
Class members declared as protected can be used by member functions and friends (classes or functions) of the class. Additionally, they can be used by classes derived from the class.
public
Class members declared as public can be used by any function.
Base class access control:
Two main factors control how base class properties and attribute will be accessed (referring to MSDN).
- Whether the derived class declares the base class using the public access specifier
- What the access to the member is in the base class
private
protected
Public
Always inaccessible regardless of derivation access
Private in derived class if you use private derivation
Private in derived class if you use private derivation
Protected in derived class if you use protected derivation
Protected in derived class if you use protected derivation
Protected in derived class if you use public derivation
Public in derived class if you use public derivation
The following example illustrates this:
// access_specifiers_for_base_classes.cpp
class BaseClass
{
public:
int PublicFunc(); //Declare a public member.
protected:
int ProtectedFunc(); // Declare a protected member.
private:
int PrivateFunc(); // Declare a private member.
};
// Declare two classes derived from BaseClass.
class DerivedClass1 : public BaseClass
{
};
class DerivedClass2 : private BaseClass
{
};
int main()
{
}
In DerivedClass1, the member function PublicFunc is a public member and ProtectedFunc is a protected member because BaseClass is a public base class. PrivateFunc is private to BaseClass, and it is inaccessible to any derived classes.
In DerivedClass2, the functions PublicFunc and ProtectedFunc are considered private members because BaseClass is a private base class. Again, PrivateFunc is private to BaseClass, and it is inaccessible to any derived classes.
You can declare a derived class without a base-class access specifier. In such a case, the derivation is considered private if the derived class declaration uses the class keyword.
