Index

C & C++ Programming - OOPS

//////////////////////////////////////////////////////////////////////
// BASE_H

#ifndef BASE_H
#define BASE_H

#include "iostream"
using namespace std;

class CBase
{
public:
 // Default Constructor
 CBase();
 CBase(int, int);

 // Destructor
 ~CBase();

 // Assignment operator
 CBase& operator = (const CBase& obj);

 // Copy Constructor
 CBase(const CBase&);

/*#################################################################################
The fundamental reason is that a copy constructor initializes uninitialize memory,
whereas the copy assignment operator must correctlly deal with a well-constructed
object.
#################################################################################*/
private:
 int x, y ;

 // A static variable is part of a class yet isnot the part of an object of that  class,
 // ie. there is exactly one copy of a static member instead of one copy per object.
 static int z;

public:
 void getData();
 void setData(int, int);

 // Overloaded function to see calling of copy constructor
 void CallingCopyConstructorFunction(CBase obj);
 void CallingCopyConstructorFunction(CBase *obj);
 void CallingCopyConstructorFunctionByRef(CBase& obj);
 void GetZNonStatic();

 // A function that needs access to members of a class, yet doesn't need to be
 // invoked for a particular object, is called a static member function.
 static void GetZStatic();

 // Copying data using External function
  CBase& CopyData(const CBase& aObj)
 {
  this->x = aObj.x;
  this->y = aObj.y;
  return *this;
 }
};

#endif

//////////////////////////////////////////////////////////////////////
// Implementation file for class CBase

#include "stdafx.h"
#include "Base.h"

CBase::CBase()
 : x(10)
 , y(10)
{
 cout << "Base Class Constructor" << endl;
}

CBase::~CBase()
{
 cout << "Base Class Destructor" << endl;
}

// Member initialization
CBase::CBase(int m, int n) : x(m), y(n)
{
 cout << "Base Class Overloaded Constructor" << endl;
}

void CBase::getData()
{
 z++;
 cout << "Value of X is " << x << endl;
 cout << "Value of Y is " << y << endl;
}

void CBase::setData(int m, int n)
{
 x = m;
 y = n;
}

CBase::CBase(const CBase& obj)
{
 cout << "Base Class Copy Constructor is called" << endl;
 this->x = obj.x;
 this->y = obj.y;
}
/*
CBase CBase::operator = (const CBase& obj)
{
 // Method 1
 cout << "Base Class Assignment Operator is called" << endl;

 CBase objAssignment;

  objAssignment.x = obj.x;
  objAssignment.y = obj.y;

  // Return type should be pointer type as to reflect the change
  return objAssignment;
}
*/
CBase& CBase::operator = (const CBase& obj)
{
 // Method 2
 cout << "Base Class Assignment Operator(this pointer) is called" << endl;

 if(this != &obj) // Beware of self-assignment
 {
  this->x = obj.x;
  this->y = obj.y;
 }
 // Return type should be pointer type as to reflect the change
 return *this;
}

// Overloaded function to check the calling of copy constructor
void CBase::CallingCopyConstructorFunction(CBase obj)
{
 // Doesnot reflect the change
  obj.x = 1;
 obj.y = 2;
}

//Doesnot call copy constructor
void CBase::CallingCopyConstructorFunction(CBase *obj)
{
 obj->x = 1;
 obj->y = 2;
}

//Doesnot call copy constructor
void CBase::CallingCopyConstructorFunctionByRef(CBase& obj)
{
 obj.x = 1;
 obj.y = 2;
}

int CBase::z = 0;

void CBase::GetZStatic()
{
 // Only static members can be used
 cout << "Value of (STATIC MEMBER) Z is " << z << endl;

 // Static function can use only static member variable
 //cout << "Value of (NON-STATIC MEMBER) X is " << x << endl;
  //cout << "Value of (NON-STATIC MEMBER) Y is " << y << endl;
}

void CBase::GetZNonStatic()
{
  cout << "Value of (NON-STATIC MEMBER) X is " << x << endl;
  cout << "Value of (NON-STATIC MEMBER) Y is " << y << endl;
  cout << "Value of (STATIC MEMBER) Z is " << z << endl;
}

// Oops.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "Base.h"

//////////////////////////////////////////////////////////////////////////////////
void Test()
{
 // Whenever the object is created the constructor
  // and destructor is called automatically. And it
  // can access only the public data of a class.
  CBase objBase1;
  objBase1.getData();
  CBase::GetZStatic(); // No object is required
  objBase1.setData(11, 22);
  objBase1.getData();
  objBase1.GetZStatic(); // Retains the value
  objBase1.GetZNonStatic(); // A Non-Static Member Function can access Static Variable
  // vice-versa is not true
  cout << "*******************************************" << endl;

  // Overload constructor is called
  CBase objBase2(100, 100);
  objBase2.getData();
  objBase2.GetZStatic(); // Retains the value irrespective of object

  cout << "*******************************************" << endl;

 // Creating the Object
  CBase *objBase3 = new CBase();
  objBase3->setData(55, 66);
  objBase3->getData();
  //objBase3 = &objBase1; Doesnot work
  delete objBase3; // the destructor is called immediately

  cout << "*******************************************" << endl;

  // Copying the Object
  CBase objBase4(33, 44);
  objBase4.getData();
  objBase4 = objBase4; // Works
  objBase4 = objBase1;
  objBase4.getData();

  cout << "*******************************************" << endl;

  // Call function to invoke copy constructor
  CBase objBase5(101, 202);
  // $$$---Sending the object by value calls the copy constructor also the destrustor is called
  objBase5.CallingCopyConstructorFunction(objBase5);
  objBase5.getData();

  // $$$---Sending the object by reference doesn't call copy constructor
  objBase5.CallingCopyConstructorFunctionByRef(objBase5);
  objBase5.getData();

  // $$$---Sending the object by pointer doesn't call copy constructor
  objBase5.CallingCopyConstructorFunction(&objBase5);
  objBase5.getData();

  CBase objBase6(10, 20);
  objBase6.CopyData(objBase5);
  objBase6.getData();
}

int _tmain(int argc, _TCHAR* argv[])
{
  Test();
  return 0;
}
Index