C++: The OOP features
Data Abstraction:
- Objective
- C++ Support
- Encapsulation - The Class construct
- Data Hiding - Access
- 'this' - the implicit argument
Objective
- to be able ato use programmer defined types - abstract data types (ADTs) - in the same way as fundamental data types
- eg (from Gorlen, K.E)
using FDTs
main (){
int a = 193;
int b = 456;
int c;
c = a + b + 47;
printf("%d\n",c);
}
using ADTs
main (){
BigInt a = "1232545678893";
BigInt b = "2344567812356";
BigInt c;
c = a + b + 47;
c.print(); printf("\n");
}
C++ Support
- Data abstraction allows the programmer to describe a data type and the operations which can be performed on that type.
- In C related data can be organized in 'struct's and treated as a logical unit (only the data).
- In C++ the struct can be used and is extended to include the operations that can be performed on the data.
struct point{
int xVal,yVal; // members
void SetPt(int,int);//member functions
void OffsetPt(int,int);
};
Encapsulation with the class construct
- In C++ the data and functions of the abstract data type can also be declared using the class keyword.
- Access to the data and functions of the class is controlled by using the keywords public, private, protected to specify the type of the access for each component.
- Class declaration in C++
class point{
int xVal,yVal; // members
public:
void SetPt(int,int);//member functions
void OffsetPt(int,int)
};
Data Hiding - Types of Access:
- C++ provides three levels of protection for members and member functions:
- PRIVATE - the default, means that the member or member function cannot be accessed (data hiding).
- PROTECTED - only accessible to member functions of the class & classes derived from that class.
- PUBLIC - can be accessed by any other function (interface to class)
-
- The keywords Private, Protected & Private followed by a colon are inserted in your class declaration.
- If you want to use a function with an object that is not a member function then it must be declared as a friend.
Derived Classes & Virtual Functions
- Derive one class from another to inherit all the members & member functions of the parent class.
- eg. class TSet: TList{
......};
- New Members & member functions defined for the subclass are added to the existing ones defined for the parent class.
- Member functions can be modified by supplying new definitions for existing member functions. The original definition is still valid for the parent class.
- Use the keyword virtual in the original definition to indicate that the member function can be overridden.
Using Virtual functions
- When you make a member function virtual you are telling the compiler to check the actual class of the object at runtime to ensure that the correct definition of the member function is invoked.
- It is a good idea to make a member function virtual if you think you will ever override it in a derived class.
An Example: Class Declaration using
Protected, Public, Friend & Virtual
class TList{
protected:
friend class TIterator;
Tlink * fLink; // first link to list
int fNumItems // No. of elems in list
public:
TList(void); // constructor
virtual void Additem(void* item);
virtual void Remove(void* item);
int NumItems() {return fNumItems;}
};
'this' - the implicit argument
- Every member function for a class is declared with a function prototype in the class declaration. C++ adds an additional argument 'this' to the beginning of every argument list which is a pointer to the current object being used to access the member function.
- Why? - So that other members and member functions can be accessed within another member function.
- eg.
TList::AddItem(void* item){
...
this.NumItems();
...
}