Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Tuesday, December 16, 2008

C++ inheritance - public, private, protected

Inheritance in C++ could be public, private, or public. It could be confusing for a beginner to find out accessibility of members and objects of these classes. Though public inheritance is mostly used, especially if there are virtual functions in the base class, other two types of inheritance have their uses as well. Here's an example program to make it clear how the 3 of them work i.e. public inheritance, private inheritance, and protected inheritance.



#include

class B{
char a;

public:
char b;

protected:
char c;
};

// public inheritance

class D:public B{

// private members of B can't be accessed
//can access public and protected members of B

char d;

public:

void f(){
//can't do this
// d = a;

d = b; // works
d = c; // works
}

// class D has the following additional members inherited from B
// public: char b
// and
// protected: char c
// this means that any class derived from D can access both b and c
// b is accessible by objects of derived class, c is not (#1)
};

// private inheritance

class E:private B{

// private members of B can't be accessed...
// can access public and protected members of B

char d;

public:
void f(){
//can't do this
// d = a;
d = b; // works
d = c; // works
}


// class E has the following additional data members inherited from B
// private:char b
// and
// private: char c
// this means that any class derived from E can't access any of the above derived data
// members, nor are they accessible to objects of derived class, public functions
// though are accessible to objects of this class (#2)

};

// protected inheritance

class F:protected B{

// private members of B can't be accessed in this class as above, so
// can access public and protected members of B

char d;

public:

void f(){
//can't do this
// d = a;
d = b; // works
d = c; // works
}

// class F has following additional members inherited from B
// protected:char b
// and
// protected: char c
// this means that any class derived from F can access
// the above members, but they can't be accessed from
// derived class objects (#3)
};

class DF: public F{
char d;

public:
void g(){
d = b; // works from #3 above
d = c; // works from #3
}
};



int main(){

char i;

B b;
D d;
E e;
F f;

// these won't work by definition
// b.c = i;
// b.a = i;


// for object d of publicly derived class
// this works from (#1)
d.b = i;
// this won't work from (#1)
// d.c = i;


// for object e of privately derived class
// these won't work from (#2)
// e.b = i;
// e.c = i;


// for object f of protected-ly derived class
// these won't work from (#3)
// f.b = i;
// f.c = i;

return 0;
}

Friday, October 24, 2008

pointer pitfall

Some C pointer fundamentals manage to fox even seasoned programmers.
For example, what is wrong with the following piece of code:

func() {

char *c;
c = "this is a string.";
*c = 'T';
printf("%s", c);
}


The code tries to capitalize the first letter of the string. It looks correct at first glance, but is not. It might even compile without any error, but will throw out an error when run.

Wednesday, November 28, 2007

Fun with JavaScript

This is a JavaScript I found while stumbling.
It works like this. Just search for an image in Google. Once it shows up images as the result of your search, enter the script (without quotes) in the address bar of your browser. It will rotate the images around. If you keep pressing enter again and again, the speed of rotation keeps increasing as well.

Saturday, June 16, 2007

Which "for loop" works better/faster

I was asked this question in an interview long ago and thought I would post it here.
Which of the following for loop works better assuming no special compiler play, just on programming logic. Will both code pieces execute equally fast?

1 -
for ( i=0; i <10; i++)
for (j=0; j<100; j++)
printf("hello\n");


2 -
for (j=0; j<100; j++)
for ( i=0; i <10; i++)
printf("hello\n");

Tuesday, June 5, 2007

Creating a dynamic library - example

We all use library functions in the programs we write. An example of library that is always used in Solaris and Unix like Operating systems is libc.so. But how to create a library? It is not hard. A dynamic library can be easily created as shown in the following example.

Let's say we want to create a library called libgeek.so. It will contain an example function called my_library_func() that we will use in our program. We will create a simple program called geek.c that has the function we wanted. We will compile this as a library and call it libgeek.so (library names begin with lib) :

$ cat geek.c

my_library_func()
{
printf("Inside my library function");
}

The above is a library function we wanted to create. We then compile it into a dynamic library by giving a -G option to compiler :

$ cc -o libgeek.so -G geek.c

Now, we can use the generated library libgeek.so in our programs like:

$ cat hellolibrary.c

int main()
{
my_library_func();
return 0;
}

Now, we can compile our program and tell the linker to link to the library we created for my_library_func() :

$ cc hellolibrary.c -L/home/osgeek -R/home/osgeek -lgeek

L and R tell linker the path to look up during link-time and run-time to find library libgeek.so. The library libgeek.so is used with "lib" part removed and "l" prefixed as "lgeek".

When we run this program, the output would look like:

$ a.out
Inside my library function

That's it. We created a library and used it in a program.

Friday, June 1, 2007

How to find address of stack top : C

In technical interviews, sometimes candidates are asked how they would find the address of the top of the stack in their system by programming in C. One simple program that should work mostly is:

int main()
{
int i;
printf("Top of the stack is %p", &i);
return 0;
}

As local variables are stored on stack, this would give an approximate top of stack. There can be variations of this program that are also few-liners like above and give more accurate results. Any more example piece of code to find stack top?

Saturday, May 19, 2007

Static linking : library options in command line

In my last post I asked why it's advised that library options be the last in the command line in case of static linking.
Here is the explanation:
The symbols on the command line are resolved from left to right.
Stating linking looks through the static library for "undefined" symbols when it is processed.
Now in case of

cc -lfoo hello.c

there are no undefined symbols when libfoo.a gets processed and so nothing gets extracted from it. When the object file is processed, it doesn't find any symbol and it gives an error "Undefined symbol"
If hello.c is put before -lfoo as in

cc hello.c -lfoo

there are undefined symbols when libfoo gets processed and so they get extracted. This works fine.

Dynamic linking doesn't have this issue as all symbols are available through the virtual address space of the output file.
Static libraries have other issues like bigger executable size, and lack of ABI ( application needs to be relinked with each new version of the library).
One advantage of having static libraries is that the executables linked to them are somewhat faster at runtime because all the linking occurs before loadtime. This helps in benchmarking. Math library libm is provided as a shared object (libm.so) as well as static library (archive libm.a) since benchmarking makes a heavy use of this library.

Friday, May 18, 2007

quirk of static linking

A question related to linking today.
Why is it advised to put the library options at the end of command line for compilation?

Hint: If we have a static library, say libfoo.a which we want to link to our program hello.c

cc hello.c -lfoo
rather than
cc -lfoo hello.c

-l option tells the compiler to link to library [lib]foo. Note that "lib" from libfoo is dropped and only "foo" part is given with -l.

Steps to install PyTorch on VMware workstation (Ubuntu guest)

  The following is the list of steps to install pytorch 2.0 in VMware workstation (Ubuntu guest): $ mkdir ~/pytorch $ mkdir ~/pytorch/as...