View Full Version : C++ queries
Hi!
I have just started learning C++ and I have some queries regarding it. 1. Can a GNU C++ program run on Windows 2000?
2. What are virtual functions? How and when are they used? 3. Why do programmers use the source code control? 4. How can I write a C++ program that will run in Windows without the user starting it up everytime? 5. What are the C++ equivalent of malloc and free in C language? Thank you so much in advance for your attention.
Hello,
3 things I can answer:
1. Depends for what OS you write. If you write a Win2K program, it will run under Win2K. If you write a Linux program, you'll get problems getting it to run under Win2K.
4. If I understood you right, you want your program starting up every time windows is started, right ?
The easiest way is to create a shortcut to your program in autostart folder.
If you want the program to install itself, or write a setup routine, then write your program into the windows registry to HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
Just write there the path to your program.
5. In C++ you also can use malloc and free, but if you want to use the new ones, they're called NEW and DELETE
example:
int *pointer = new int;
delete pointer;
Hope this helps.
RJ
Paul Victorey
02-05-2001, 09:45 PM
1. It depends on which functions you use. You will certainly have to recompile the code on the new machine. You may or may not be able to directly reuse the code. Generally, you should try to separate the user interface code from the functional code; if you code the functional parts using only standard C++, then you only need to rewrite the interface code (which varies depending on operating system) and recompile.
2) Virtual functions are the key to abstraction. The idea is to create a base class, which you expand upon in child classes.
For example, I have the functionality to take a bitmapped image and reduce the number of colors in an intelligent manner. The Reduce() function takes a pointer to an object of type RGBImage.
In actuality, RGBImage is only an abstract class. Abstract classes have at least one of a special type of function called a pure virtual function, and you cannot create an object from them.
The only thing you can do with abstract classes is create children from them. For example, my BmpImage class is a child of RGBImage.
A child of an abstract class must implement all pure virtual functions. It MAY implement virtual functions that are not purely virtual.
A function that is virtual means that whatever the class of the OBJECT, the corresponding function will be called. Otherwise, the class of the POINTER determines it.
Maybe this makes more sense:
class Parent{
public:
void FunctionOne(){
printf("Parent::FunctionOne");
}
virtual void FunctionTwo(){
printf("Parent::FunctionTwo");
}
};
class Child : public Parent{
public:
void FunctionOne(){
printf("Child::FunctionOne");
}
void FunctionTwo(){
printf("Child::FunctionTwo");
}
};
Now, if you did the following:
Parent * c = new Child(); // you can make a pointer to Parent point to Child because Parent is a base class of Child.
If you executed c->FunctionOne() it would print "Parent::FunctionOne()" because the function is not virtual and it only considers the class of the pointer.
If you executed c->FunctionTwo() however, it would print "Child::FunctionOne()" because the actual object is a Child, and virtual functions call the OBJECT'S copy of the function.
This is really helpful when you want to make an interface class to reuse code. That's why my RGBImage class has all of its functions virtual. I know that I can take a pointer to an RGBImage (which will in fact point to some derived class, maybe BmpImage, maybe something else) and I can use, say GetImageDimensions() and know that, assuming the derived class implements the interface properly, it will return the appropriate dimensions.
This way, if I wanted to reduce the colors of, say, a GifImage object, I can make GifImage a subclass of RGBImage and implement the few functions that I need to implement, and it will work exactly as it should.
3. I don't understand the question
4. and 5. are already answered quite nicely.
[Edited by Paul Victorey on 02-05-2001 at 10:54 PM]
Thank you so much guys for the info. I really appereciate it.
Paul Victorey
02-06-2001, 01:11 PM
Glad we could be of assistance.
I hope you learn to truly use the power of virtual functions -- oh, BTW, a pure virtual function is declared like this:
class AbstractClass{
virtual int PureVirtual() = 0;
};
All a pure virtual means is that there is an '= 0' after the definition. This means that you DO NOT provide any code for this function -- all child classes must implement this. Use pure virtual to force child classes to implement routines; use virtual if you want to provide a default function but want to allow children to override this.
Also, generally I prefer to make my interface classes (which are all abstract classes) very simple, only providing the functionality that any potential subclass could do.
For example, RGBImage, which has only pure virtual functions, has only a very few functions as well (I'm not listing the arguments here):
GetPixel();
SetPixel();
GetDimensions();
SetDimensions();
This is because, regardless of what kind of image it is, these functions apply. RGBImage is used by me for any image that does not use a palette but uses direct RGB color codes to store each pixel's color. The related PaletteImage class has a few more functions, but again, its functionality should be implementable for ANY image that uses a palette.
By creating common interfaces, the same code can be used to operate on a multitude of object types, even ones you haven't thought up yet.
vBulletin® v3.7.0, Copyright ©2000-2008, Jelsoft Enterprises Ltd.