Thursday, April 23, 2015

Abstract Class in C# and Abstract VS Virtual

Abstract Class is one of the important idea of OOP. It is a special class that is not instantiated i.e. it represents only as base class from which you will not permitted to create objects. But you can inherit the abstract class to use it. Abstract Class consists of either abstract or non-abstract members or both. That means it may contains variables, constructors, functions or properties like other classes as well as abstract members. 

Now the questions is? What is abstract member? A member preceded with a keyword abstract with no definition in the base class, called abstract member. A derived class should override the abstract members of a base class to use it. Look at the following example:
public abstract class TestAbstractClass
{
     private string firstName;
     private string middleName;
     private string lastName;

     public TestAbstractClass(string firstName, string middleName, string lastName)
     {
          this.firstName = firstName;
          this.middleName = middleName;
          this.lastName = lastName;
     }

     public string GetFullName()
     {
          return firstName + " " + middleName + " " + lastName;
     }

     public abstract string ReverseName();
}
In this example, the abstract class('TestAbstractClass') contains only one abstract member function 'ReverseName()' and few non-abstract members like other classes. If any class want to inherit this abstract class, then that class should have to define(override) the abstract function 'ReverseName()' of 'AbsClass'. Look at the following class named 'SubAbsClass' which inherit the 'TestAbstractClass':
class SubAbsClass : TestAbstractClass
{
        public SubAbsClass(string firstName, string middleName, string lastName): base(firstName,
        middleName,lastName)
        {
        }       
        public override string ReverseName()
        {
              string fullName = GetFullName();
              string reverseName = "";
              for (int count = fullName.Length - 1; count >= 0; count--)
                   reverseName += fullName[count];
              return reverseName;
        }

}
Here, the derive class 'SubAbsClass' contains the definition of abstract function 'ReverseName()' of the base class 'TestAbstractClass'.

# We know that a member function preceded with a virtual keyword(virtual function) also override in derive class, then what is the difference between abstract and virtual functions?
  • Abstract methods have no implementation in the base class and must be overridden in the derive class. On the other hand, virtual methods may have implementation in the base class and can be overridden in derive class but it doesn't have to be overridden.
  • We cannot call base.method() for abstract methods but we can call for virtual methods.

Tuesday, March 17, 2015

How to run C graphics program in Code::Block

In this post I am going to illustrate the process to run C graphics program in Code::Block. If you want to run C graphics in Dev-C++, you may check out my previous blog post on how to run C graphics program in Dev-C++.

Process:


1) Download the Code::Blocks EDU-Portable Version(CodeBlocks-EP) and Install It.
   (http://codeblocks.codecutter.org/)

2) Create a new project on CodeBlocks-EP
    (File->New->project...-> Select WinBGIm Project Category->Click on Go->Next)

         

3) Then give your project name(say my_first_gproject) and click on Next->Finish


        

4) Add a file(say test_prog.c) to project
   (File->New->Empty FIle->Yes->test_prog.c->Save)


         

         

         

5) Write the following code on first_prog.c:

   #include<stdio.h>
   #include<conio.h>
   #include<graphics.h>

   int main()
   {
       int gMode, gDriver = DETECT;

       initgraph(&gDriver, &gMode, "");

       line(0, getmaxy()/2, getmaxx(),getmaxy()/2);
       line(getmaxx()/2, 0, getmaxx()/2,getmaxy());

       circle(getmaxx()/2, getmaxy()/2, 130);

       getch();
       closegraph();
       return 0;
   }

6) Compile and run program




















>>To learn more about C graphics program, you may visit the following links: 

   !!!Happy Coding!!!

Sunday, March 15, 2015

How to run C graphics program in Dev-C++

There are two ways to run C graphics program in Dev-C++:

First Process:

1) Download (http://bloodshed-dev-c.en.softonic.com/) and install Dev-C++.

2) Download WinBGIm 6.0 Dev-C++ package from      

     (http://www.nextwap.net/file/sRPS3XPN/winbgim-60-1g17l.html)

3) To Install WinBGIm 6.0 Dev-C++ package, go to Tools->Package Manager

            
            

Then Click ->Install Package->Add WinBGIm 6.0 Dev-C++ Package->Open->Next->Finish


4) Go to File->New->Project->Select WinBGIm->Project Name(my_first_gproject)->Ok

           

5) Write the following code:

               #include<stdio.h>
      #include<conio.h>
      #include<graphics.h>
      int main()
      {
        int gDriver=DETECT, gMode, midx=0,midy=0;
        initgraph(&gDriver, &gMode, " ");

        midx=getmaxx()/2;

        midy=getmaxy()/2;

        line(0,midy,getmaxx(),midy);

        line(midx,0,midx,getmaxy());

        circle(midx, midy, 100);


        getch();

        closegraph();
        return 0;
     }


6) Compile and run the program.
         
   

Second Process:

1) Download (http://bloodshed-dev-c.en.softonic.com/) and install Dev-C++.

2) Download WinBGIm (http://winbgim.codecutter.org/)

3) Extract the zip file.(Contains graphics.h winbgim.h and libbgi.a)

4) Copy graphics.h and winbgim.h files in include folder(C:\Dev-Cpp\include\) of your compiler directory.

5) Copy libbgi.a to lib folder (C:\Dev-Cpp\lib\) of your compiler directory.

6) Now Open Dev-C++ and create a new project. (Go to File-> New-> Project-> Project Name(my_first_gproject)->Ok)


           

7) Go to Project Menu->Project Options->Parameters

8) Copy the following code in the Linker field and click ok.

         -lbgi
        -lgdi32
        -lcomdlg32
        -luuid
        -loleaut32
        -lole32


           

9) Add a new file(first.c) to the project

            

10) Write the following code on first.c file:


        #include<stdio.h>
      #include<conio.h>
      #include<graphics.h>

      int main()
      {
        int gDriver=DETECT, gMode, midx=0,midy=0;

        initgraph(&gDriver, &gMode, " ");

        midx=getmaxx()/2;

        midy=getmaxy()/2;

        line(0,midy,getmaxx(),midy);

        line(midx,0,midx,getmaxy());

        circle(midx, midy, 100);


        getch();

        closegraph();
        return 0;
     }
11) Compile and run the program.

           


>>To know more about C graphics program, you may visit the following links:


     !!!Happy Programming!!!