Showing posts with label how. Show all posts
Showing posts with label how. Show all posts

Monday, February 4, 2013

C++ 'in class variable' and 'static variable' initialization

#include <iostream>
using namespace std;

class Demo

{
  public:
  int a  = 1;
    // static int b=2;  //this gives error
  static int b;
};

int Demo::b=2;  //initialize the static variable of class

int main()
{
    Demo d;
    cout << "\n value of variable a: " << d.a << endl;
    cout << "\n value of static variable b: " << Demo::b << endl;
}


OUTPUT:

Wednesday, January 16, 2013

Smart Pointer (works as garbage collection) in c++

//smart (auto) pointer is a new feature of c++11
#include <memory>           //     <-- 0
#include <string>
using namespace std;

typedef auto_ptr<Car> CarPtr;


class Car{
    public:
        void startEngine()

        {
            //...................
        }
       
        void tuneRadioTo()

        {
            //...................
        }
};

void f()
{
  CarPtr p(new Car());         //    <-- 1
  p->startEngine();             //    <-- 2
  p->tuneRadioTo();            //    <-- 3
}                                    //    <-- 4

int main()
{
   f();
}
 

0: This gets the definition for auto_ptr
1: Create an object
2: Call a member function
3: Call another member function
4: Destroy the Car object

-----------------------------------------------------------------------------------------------

//Program to show drawback of auto pointer
#include <memory>             
#include <iostream>
using namespace std;

class Test{
    public:
        void print(){
            cout << "Test::print" << endl;
        }
};

int main(){
    auto_ptr<Test>  aptr1(new Test);
    aptr1 -> print();
   
    cout << aptr1.get();
   
    auto_ptr<Test>  aptr2(aptr1);
    aptr2 -> print();
   
    cout << aptr1.get();
    cout << aptr2.get();
   
    return 0;

}

OUTPUT:

To overcome this c++11 provide another mechanism called smart (shared) pointer.

Tuesday, October 23, 2012

How to install GCC 4.7.2 in ubuntu 12.04 for c++11

Ubuntu 12.04 comes with GCC compiler for C language only (not for c++).
First check which version of GCC ubuntu already installed. If it is <4.7.2 then proceed.
  • gcc is used to compile C program
  • g++ is used to compile C++ program
  1. gcc -v (to check the gcc version)
  2. g++ -v (to check the g++ version)
  3. sudo apt-get update
  4. sudo apt-get install build-essential (to install g++ & some other other stuff)
  5. sudo apt-get update
  6. g++ -v (now check g++ is installed)

(Now add source from which we get GCC latest stable version)
  1. sudo add-apt-repository ppa:ubuntu-toolchain-r/test
  2. sudo apt-get update
  3. sudo apt-get install gcc-4.7
  4. sudo apt-get install g++-4.7
  • ls -lh /usr/bin/g++*

Install Alternatives(remember the version of gcc/g++):
  1. sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.6 10
  2. sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.7 20
  1. sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.6 10
  2. sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.7 20
  1. sudo update-alternatives --install /usr/bin/cc cc /usr/bin/gcc 30 (avoid the warning)
  2. sudo update-alternatives --set cc /usr/bin/gcc
  1. sudo update-alternatives --install /usr/bin/c++ c++ /usr/bin/g++ 30
  2. sudo update-alternatives --set c++ /usr/bin/g++
  • gcc -v
  • g++ -v (conform that you successfully installed the GCC)

configuring the default commands for gcc, g++:
  1. sudo update-alternatives --config gcc
  2. sudo update-alternatives --config g++
Source:
  1. Repository
  2. Nice detailed explanation
  3. Install Alternatives

Thursday, September 20, 2012

How to count the number of objects created of a class

#include <iostream>
using namespace std;

class Demo
{
    public:
        static int totalObject;
       
        Demo()
        {
            totalObject++;
        }
};

int Demo::totalObject(0);    //static variable initialized to 0

int main()
{
    Demo d1;
    Demo d2;
    Demo d3;
    Demo d4;
   
    cout << "\n Total no of object of Demo class is: " << Demo::totalObject << endl;
    return 0;
}


OUTPUT:

Tuesday, September 11, 2012

How to make abstract class in c++

C++ has no abstract keyword or equivalent, to make a class abstract is to declare a pure virtual function in it (which prevents instantiation).

For this same reason, in C++ there is not much difference between an  interface and an abstract class - a C++ interface is simply a class containing only pure virtual functions.

C++ has no keyword abstract. However, you can write pure virtual functions; that's the C++ way of expressing abstract classes.

What is a Pure Virtual Function:
A Pure Virtual Function is a Virtual function with no body.

Declaration  of Pure Virtual Function:
Since pure virtual function has no body, the programmer must add the notation =0 for declaration of the pure virtual function in the base class.

Syntax of Pure Virtual Function:
class class_name //This denotes the base class of C++ virtual function
{
    public:
    virtual void virtualfunctioname() = 0 //This denotes the pure virtual function in C++
};

___________________________________________________________________
//Full code example
#include <iostream>
using namespace std;
class Sandeep
{
public:
        virtual void example()=0; //Denotes pure virtual Function Definition
};

class Demo1:public Sandeep
{
public:
        void example()
        {
                cout << " \n Welcome";
        }

};

class Demo2:public Sandeep
{
public:
        void example()
        {
                cout << " To My blog \n";
        }

};

int main()
{
        Sandeep* arra[2];
        Demo1 d1;
        Demo2 d2;
        arra[0]=&d1;
        arra[1]=&d2;
        arra[0]->example();
        arra[1]->example();
}


OUTPUT:

Monday, September 3, 2012

How do I create an instance of a class?

The methods called when a class is created are called contructors. There are four possible ways of specifying constructors; the fifth method is worth mentioning for clarifying reasons:
  • default constructor
  • copy constructor
  • value constructor
  • conversion constructor
  • copy assignment (not a constructor)
struct A
{
  A() { /* ... */ }                          // default constructor 
  A(const A &a) { /* ... */ }                // copy constructor 
  A(int i, int j) { /* ... */ }              // value constructor 
  A     &operator=(const A &a) { /* ... */ } // copy assignment 
};

struct B
{
  B() { /* ... */ }           // default constructor
  B(const A &a) { /* ... */ } // conversion constructor 
};

void    function()
{
  A     a0(0, 0); // shortcut, value constructor 
  A     a1(a0);   // shortcut, copy constructor 
  B     b1(a1);   // shortcut, conversion constructor 
  B     b;        // shortcut, default constructor 

  b1 = a0;        // conversion contructor 
  a0 = a1;        // copy assignment 
}

Tuesday, July 17, 2012

List of IDE/Compilers for c++ environment in linux or windows

Linux:
  1. GCC
  2. Code::Block IDE (using GCC for compilation)
  3. EclipseIDE
  4. Net beans IDE
  5. QT framework

Windows: 
  1. Visual c++
  2. Dev c++
  3. Code::block IDE (using GCC for compilation)
  4. MinGW (GCC version for windows)  
  5. Eclipse IDE
  6. Net beans IDE
  7. MinGW64
  8. Visual studio .net framework
  9. QT framework

Cross Platforms:
  1. Eclipse IDE (linux, windows, mac)
  2. Net beans IDE (linux, windows)
  3. Code::block IDE (linux, windows)
  4. QT framework