Sunday, July 06, 2008

C++ Sample Code - 01

C++ Sample Code - 01
Skill Level: Intermediate

This sample explains the need of the virtual functions. Scroll down for the download links.

//============================================================================
// Name : Sample-01.cpp
// Author : Raghavendra Nayak
// Version :
// Date : 07-June-2008
// Copyright : (c) 2008, Raghavendra Nayak. All rights reserved.
// Description : Sample C++ program. This sample explains the need of
// virtual functions.
//============================================================================

/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

/*
* Case: 01
* Please note that, in this program by default we don't use virtual functions,
* due to which always base class functions are called!
*
* To change this behavior define the value of the macro USE_VIRTUAL_FUNC as 1.
* This will add "virtual" to the base class function prototype.
*
* ie; virtual void setData(int inData);
*
* After this, you will notice that, proper functions are called..
*
* Case: 02
* Also, note that, virtual functions are hierarchical, ie, if the very first base
* class is virtual, all the subsequent derived classes will automatically get that
* property.
*
* ie, In this example, myClassC inherits from myClassB and if you
* define USE_VIRTUAL_FUNC as 1 . This will add virtual keyword to the functions
* of myClassA (Base class).
*
* After this, when you call setData/getData functions, you will see that proper
* functions are called!
*/

/*
* Conclusions:
*
* 01. If you don' use virtual functions, always base call functions are called.
* (When called through base class pointer/reference).
*
* 02. virtual functions are hierarchical, if the base class has virtual property,
* all the derived classes automatically get that property..
*/


#include <iostream>

/*
* Define the value of this macro to 1 to use virtual functions
*/

#define USE_VIRTUAL_FUNC 0

class
myClassA {
protected
:

int
myData;
public
:
#if USE_VIRTUAL_FUNC
virtual
#endif //USE_VIRTUAL_FUNC
void setData(int inData)
{


std::cout<<"\nWe are in myClassA setData";
myData = inData;
}

#if USE_VIRTUAL_FUNC
virtual
#endif //USE_VIRTUAL_FUNC
int getData()
{

std::cout<<"\nWe are in myClassA getData";

return
myData;
}
};


class
myClassB : public myClassA {

public
:
void
setData(int inData)
{

std::cout<<"\nWe are in myClassB setData";

myData = inData;
}


int
getData()
{

std::cout<<"\nWe are in myClassB getData";

return
myData;
}
};


class
myClassC : public myClassA {

public
:
void
setData(int inData)
{

std::cout<<"\nWe are in myClassC setData";

myData = inData;
}


int
getData()
{

std::cout<<"\nWe are in myClassC getData";

return
myData;
}
};


int
main (int argc, char * const argv[])
{


int
option = 1;

std::cout<<"\nWhich case you want to run ? [1/2] : ";

std::cin>>option;

myClassA *myObjPointer;

if
(option == 1)

myObjPointer = new myClassB; // Case: 01
else
myObjPointer = new myClassC; // Case: 02


myObjPointer->setData(10);
myObjPointer->getData();

return
0;
}




Downloads Links:
  1. XCode project
  2. Eclipse project
  3. Source file

No comments: