OOP in C++

Posted on


  • Generally  every  object  we  create  will  require  some  sort  of  initialization.  C++

allows  a  constructor  function  to  be  included  in  a  class  declaration. A class’s constructor  is  called  each  time  an  object  of  that  class  is  created.

  • Thus, any initialization  to  be  performed  on  an  object  can  be  done  automatically  by  the constructor function. A constructor function has the following characteristics : –
  • It should have the same name as that of the class.
  • It  should  not  have  any  return  type  even  void  however  they  can  take arguments.
  • onstructers can take default arguments
  • Constructers can be dynamically initialized.
  • Constructer function cannot be made virtual
  •  It should be always declared in the public section of the class.

Example: The following program makes use of a constructor.

#include <iostream.h >

#include <conio.h >

class  number

{int a;


number( ); //constructor

void show( );};

Public number( )

{cout<< “In constructor\n”;


void show( )

{cout << a;}

void main( )


number ob; // automatic call to constructor

ob.show( );



Output of the Program

In constructor


  • In this simple example the constructor is called when the object is created, and the constructor initializes the private variable a to 100 .For a global object, its constructor is called once, when the program first begins execution. For local objects, the constructor is called each time the declaration statement is executed.

Posted By-: Vissicomp Technology Pvt. Ltd.

Website -: http://www.vissicomp.com



OOP in C++

Posted on

For the students of FYBSc (IT), SYBSc (CS), SYBCA


The copy constructor is a constructor which creates an object by initializing it with an object of the same class, which has been created previously. The copy constructor is used to:

  • Initialize one object from another of the same type.
  • Copy an object to pass it as an argument to a function.
  • Copy an object to return it from a function.

If a copy constructor is not defined in a class, the compiler itself defines one. If the class has pointer variables and has some dynamic memory allocations, then it is a must to have a copy constructor. The most common form of copy constructor is shown here:

classname (const classname &obj){   // body of constructor}

Here, obj is a reference to an object that is being used to initialize another object.





int getLength(void);

Line(int len );             // simple constructor

Line(constLine&obj);  // copy constructor

~Line();                     // destructor




// Member functions definitions including constructor

Line::Line(int len)


cout <<“Normal constructor allocating ptr”<< endl;

// allocate memory for the pointer;

ptr =newint;

*ptr = len;




cout <<“Copy constructor allocating ptr.”<< endl;

ptr =newint;

*ptr =*obj.ptr;// copy the value




cout <<“Freeing memory!”<< endl;

delete ptr;






void display(Line obj)


cout <<“Length of line : “<< obj.getLength()<<endl;


// Main function for the program

void main()

{   Line line(10);


When the above code is compiled and executed, it produces the following result:

Normal constructor allocating ptr

Copy constructor allocating ptr.

Length of line : 10

Freeing memory!

Freeing memory!


Posted By-: Vissicomp Technology Pvt. Ltd.

Website -: http://www.vissicomp.com

OOP in C++

Posted on

For the students of FYBSc (IT), SYBSc (CS), SYBCA

C++ Identifiers:

  • A C++ identifier is a name used to identify a variable, function, class, module, or any other user-defined item. An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores, and digits (0 to 9).
  • C++  does  not  allow  punctuation  characters  such  as  @,  $,  and  %  within  identifiers.
  • C++ is a case-sensitive programming language. Thus, Manpower and manpower are two different identifiers in C++.


  • Here are some examples of acceptable identifiers:

Name1                        abc                  move_name                    a_123

Myname_50                num1              num2                           temp retVal


C++ Keywords:

  • The following list shows the reserved words in C++.  These reserved words may not be used as constant or variable or any other identifier names.

else      new    this  Auto         enum             operator          throw             Bool    explicit

private             true    Break              export   protected   try  Case   extern   public   typedef

Catch   false   register   typeid  Char   float   reinterpret_cast    typename

Class  for  return  union Const  friend  short         unsigned const_cast   goto  signed  using

continue  If      sizeof             virtual             Default  inline  Static  void Delete  int  static_cast              volatile

Do  long          Struct              Double            mutable                      Switch                          while  dynamic_cast  namespace  Template

Posted By-: Vissicomp Technology Pvt. Ltd.

Website -: http://www.vissicomp.com