CPP All in one example. The example itself is a detailed C++ tutorial. Just go through the comments besides each line of code.
ProjectManager.h
#include <iostream> /*C++ standard header for iostream.h. When speciefied in
angular brackets
the file is presumed to be a project or standard header and the search starts
from environment path which could be changed */
#include <string.h> // C style of header file. The c++ library doesn't have .h
extension.
#include <cassert>
#include "Def.h" /* user defined header file. the search for this header starts
from current working directory when specified incide quatation mark.*/
using namespace std; /*the pupose of namespacees is to reduce name polution.
They can be nested.
std is the standard namespace of C++ library. */
class ProjectManager { /* classes are fundamental packcging unit to localize
data and methods(services)*/
private: /* most restrictive aqccess specifier. Can be accesed by only class
member and friend functions.*/
int *groupEmpCodes;
int groupSize;
public: /* most relaxed access specifier. Can be accessed by using dot '.'
operator with the object of this class*/
string name; //use char* if your compiler doesn't support strings.
static const int defaultGroupSize=0;
/* static: static members are associated with class rather than with object of
the class. That means we have only one copy of that variable or method even if
there are many instancees of the class. Its like global vaiable except static
member have scope and doesn't pollute global namespace*/
/* const: To avoid unwanted changes we use const correctness which gives
compiler error whenever unexpected changes occur.*/
/* All const static members are initialized in source file except integral
types*/
ProjectManager(string sname="XYZ",int sz=defaultGroupSize);
/*Constructors: A constructor is a special class member function with the same
name as class name used for intialization. No return type can be specified for a
constructor. When it doesn't require an argument to be supplied by user, its
called default constructor.*/
ProjectManager(string sname,int *ecodes,int groupsize);
/* Function overloading: functions could be overloaded by specifying different
signatures with the same function name. Signature is the number and type of
arguements passed to any function. Here constructors arre being overloaded*/
ProjectManager(const ProjectManager &PM);
/*References: An alternative to pointers in C++. Reference is an alias. It can't
be null and cannot be reassigned to. passing by refence doesn't make any local
copy. Taken from algol language*/
/* Const references: Making the argument a const reference will allow the
function to be used in all situations. This means that, for built-in types, the
function will not modify the argument, and for user-defined types the function
will call only const member functions, and won't modify any public data members.
The use of const references in function arguments is especially important
because your function may receive a temporary object, created as a return value
of another function or explicitly by the user of your function. Temporary
objects are always const, so if you don't use a const reference, that argument
won't be accepted by the compile*/
/* Copy constructor: These type of constructors are called copy constructors.
They are called when object is passed by value. assignment operator acts on
existing objects. Copy constructor creates a new object.*/
inline int gsize() const {return groupSize;}
/*Inline function: Function calls are more expensive than direct memory access.
Inline functions are expanded in place at its point of call. It doesn't involve
any function call atall but the ultimate decision to make a function is of
compiler's even we declare inline keyword before a function.*/
void Pm(string sname, int sz, int *group);
int& operator[](int groupmember);
/*Operator overloading: One of the feature of OOPs. It can transform complex
program listing to a user friendly statements. Use the operator keyword. pass
the required arguemnts only. Specify correct return type. There is no
distinction between overloaded prefix operator and postfix operator. Operator
overloading can be used with function overloading.Operators like member access
or dot operator (.), the scope resolution operator(::),the conditional
operator(?:) and the pointer to member operator (.*) cannot be overloaded.*/
~ProjectManager(){ delete []groupEmpCodes;}
/* Destructor: Is again a special function with the same name as class name with
~ appended to it. Its automatically invoked when the object oes out of scope.
Used for cleanup operations.*/
};
//Here are the implementation ProjectManager.cpp
void ProjectManager::Pm(string sname,int sz, int *group)
{
groupSize = sz;
name = sname;
groupEmpCodes = new int[sz];
for(int i=0; i< sz; ++i)
if(!group)
groupEmpCodes[i] = 0;
else groupEmpCodes[i] = group[i];
}
ProjectManager::ProjectManager(string sname,int sz) { Pm(sname,sz,0);}
ProjectManager::ProjectManager(string sname,int *ecodes,int groupsize)
{ Pm(sname,groupsize,ecodes);}
ProjectManager::ProjectManager(const ProjectManager &P)
{ Pm(P.name, P.groupSize, P.groupEmpCodes);}
int& ProjectManager::operator[](int groupmember)
{
assert(index >= 0 && index < groupSize);
return groupEmpCodes[index];
}
//A sample user of this class
void main()
{
ProjectManager pm1;//invokes the default constructor
int ec[] = {101,102,103,104};
string pmname;
cout<< "Enter project manager name \n"<<endl;//<< is a member is of ostream
cin>>pmname;//>> is a member of istream
ProjectManager pm2(pmname,*ec,4);/*invokes ProjectManager(string sname,int
*ecodes,int groupsize)*/
cout<< "the employee codes of group members of "<< pmname<< endl;
for(int x=0;x<pm2.gsize();x++)
{
cout<< pm2[x]<<", ";//invokes overloaded operator []
}
cout<<'\n';
ProjectManager pm3(pm2);//invokes copy constructor
Projectmanager pm4= pm2;//invokes copy constructor
}
