C++ language - extract
© Lubos Jules Stary,  03/29/2002
CONTENTS
Functions (Methods) Declaration Overview	2
Operators Overloading Overview	2
Pointers, References, Arrays Overview	2
Derivation (Inheritance) Overview	3
Controlling Access to Class Members - PRIVATE, PROTECTED, PUBLIC	4
Controlling Access through Derivation - PRIVATE, PROTECTED, PUBLIC	4
Specialities Overview	4
Class Declaration Overview	5

© 2002, Lubos Jules Stary
Functions (Methods) Declaration Overview

Feature	Description	
default parameters	You can initialize a function parameter with a default value.
void MyFun(int Param1, int Param2=1, int Param3=4);
After that you can have variable number of parameters.
void MyFun(10);	
overloading
(polymorphism)	More functions with the same name - must differ in number or type of parameters.
int   MyFun(int x, int y);
float MyFun(float x, float y, float z);	
const	Const function == read-only == you promise no change for member data.
int GetAge() const{ return(itsAge); }	
init members	The constructor can have the initialization stage for data members.
Cat(int age,int weight):catAge(age), catWeight(weight) {}	
copy constructor	Copy constructor is called by object copying during another object creating.
Cat(const Cat & rhs){...} // definition
Cat Johny(Friskie);  // calls COPY CONSTRUCTOR	
Operators Overloading Overview

Feature	Description	
operator overloading	When you overload operators (=, +, [], ...), you can use them  directly with class.
Cat & operator=(const Cat & rhs){ ... }
Counter operator+ (const Counter & rhs) { ... }; //"+" 
char & operator[](unsigned short offset); // write data
char operator[](unsigned short offset) const; //READ-ONLY
const Counter & operator++ () { ... }    // PREFIX ++
const Counter   operator++ (int) { ... } // POSTFIX ++	
conversion operator	Conversion between class data member <-> fundamental types (int, char, ...)
// class_member <- int - constructor with init
Counter(int initVal):countVal(initVal){ }; // definition
Counter count1 = 4; // call
// int <- class_member - operator int()
operator int(){ return countVal; }; // definition
int x = count1;	

Pointers, References, Arrays Overview

Feature	Description	
array	Array of objects:
Cat CatArray[5]; // definition - constructor called 5 x
CatArray[0].GetAge(); // class access	

Feature	Description	
new,
delete	Allocates / frees memory for object. 
// --- Pointer to Object ---
Cat * pCat = new Cat(5); // 1 Cat, CONSTRUCTOR with init
if (pCat==NULL) ...  // successful memory allocation test
pCat->SetAge(1);         // function call
delete pCat;             // 1 Cat, DESTRUCTOR call
pCat=NULL;               // wild pointer protection	
	// --- Pointer to ARRAY of Objects ---
//4 x Cat, 4 x default CONSTRUCTOR call, NOT ALLOWED INIT
Cat * paCat = new Cat[4];
paCat[0].SetAge(2);   // function call - pointer to array
delete [] paCat;         // 4 x Cat, 4 x DESTRUCTOR call	
	// --- ARRAY of Pointers to Object ---
// NO memory allocation, NO CONSTRUCTOR call
Cat * paaCat[4];
//4 x Cat, 4 x CONSTRUCTOR with INIT call
for (xp=0;xp<4;++xp) paaCat[xp] = new Cat(1);
paaCat[0]->SetAge(3); // function call - array of pointers
// 4 x Cat, 4 x DESTRUCTOR call
for (xp=0;xp<4;++xp) delete paaCat[xp]; 	
this	this pointer points to object from its inside (also when the object isn't created by new).
int GetAge() const { return this->catAge; }	
& (reference)	Symbolic name with the same memory address as the target.
int X;
int & refX = X; // reference to variable X	
Derivation (Inheritance) Overview

Feature	Description	
derivation
(inheritance)	The derived class inherits the functionality of the base class.
class Cat : public Mammal // DERIVATION
Cat::Cat():Mammal(), ... // explicit constructor call
Cat Friskie;       // Mammal constructor(own memory part)
                   // Cat constructor
/* out of scope */ // Cat destructor
                   // Mammal destructor	
virtual,
overriding	Virtual function - a function in a base class that you expect to be overrided. The used variable must be a pointer or a reference!
The destructor must be virtual too!
Overrided function - a function in a derived class with the same name and the head as a function in a base class.
// Base_class - virtual function definition
virtual void Function1();
// Derived_class - function overriding
void Function1(); 
// Using: pointer definition
Base_class * pBase_class = new Derived_class; 
// Using: calls Derived_class.Function1()
pBase_class->Function1();	

Controlling Access to Class Members - PRIVATE, PROTECTED, PUBLIC

Type of Access	Access allowed for:	
private	internal member functions, friends	
protected	internal member functions, friends, derived class	
public	any function	
Controlling Access through Derivation - PRIVATE, PROTECTED, PUBLIC

Member Access in Base Class	Derivation Type	Access from derived class:	
private	no matter	always inaccessible	

protected	private	private	
	protected	protected	
	public	protected	

public	private	private   (next derivation - NO ACCESS)	
	protected	protected	
	public	public	
Specialities Overview

Feature	Description	
qualified call	Fully qualifying calling:
object_name.base_class_name::function_name();
Friskie.Mammal::Sound();   // object
pFriskie->Mammal::Sound(); // pointer	
Abstract Data Type (ADT)	A base, abstract concept class. Not allowed to make an object! Changing a class to ADT - use pure virtual function. All pure virtual functions must be overridden (or you will have an ADT again)!
virtual void Run() = 0 { }    // pure virtual function	
static members	A static member is one per class, belongs to the class not to the object. Static member functions have access only to a static member variables!
// Static Member Data
static int XCats; // declaration - no memory allocation
int Cat::XCats = 0; // definition - memory allocation 
i = Cat::XCats; // using - class::static_data
// Static Member Function
static int GetHowMany() { return XCats; }
i = Cat::GetHowMany(); // call - without object	
friend class

friend function	A friend class definition - your private data access establishing.
class C10
{
    friend class C20;
friend void C20::C21public();	

Class Declaration Overview

class Cat : public Mammal // DERIVATION
{
public:  // --- PUBLIC members ----------------------------------------
    // --- CONSTRUCTORS part ------------
    Cat():itsAge(1){ }                // default CONSTRUCTOR
    Cat(int initAge):itsAge(initAge){ }    // CONSTRUCTOR with init part
    Cat(const Cat & rhs);                  // COPY CONSTRUCTOR
    ~Cat(){}                               // DESTRUCTOR

    // --- OPERATORS OVERLOADING part ---------------
    Cat & operator=(const Cat & rhs)
        { itsAge = rhs.GetAge(); return *this; } // = operator overloading
    operator int(){ return(itsAge); }; // Conversion operator

    // --- ACCESSORS part ---------------
    int GetAge() const{ return(itsAge); }  // ACCESSOR function (function)
    void SetAge(int newAge) { itsAge = newAge; }

    // --- Other functions part -----------
    virtual void Speak()const{ }  // Virtual function
    virtual void Run() = 0 { }    // Pure Virtual function

protected:
private: // --- PRIVATE members ---------------------------------------
    int itsAge; // private data
    static int HowManyCats; // static member data declaration
};

int Cat::HowManyCats = 0; // definition

//***************************************************
// Copy Constructor - function definition
//***************************************************
Cat::Cat(const Cat & rhs)
{
    itsAge = rhs.GetAge();
}

//*********************************
// ClassTest - class functions test
//*********************************
void ClassTest()
{
    Cat Friskie(3);      // calls CONSTRUCTOR with init
    Cat Johny(Friskie);  // calls COPY CONSTRUCTOR
    Cat Sammy = Friskie; // calls COPY CONSTRUCTOR variant 2
    // without Copy Constructor declaration - calls default
    // Copy Constructor == hard copy 1:1
    
    Johny.SetAge(5);     // calls function
    Friskie = Johny;     // call = Equal operator overloading
    // without Equal operator overloading - calls default
    // Equal operator == hard copy 1:1

} // end of scope - calls DESTRUCTORs