|
sir,
i am confused about pure virtual function .why it is needed in abstract base class. in which cases it is use.
plz tell me about pure virtual function & abstract base class
|
|
|
|
|
Pure virtual functions can be used to force other developper who will create classes inherited of your class to define this specific function. If this developper implemented a class that inherits yours and didn't implement the function, whenever you try to instantiate this class, the compiler will generate an error saying that the function has not been define in the inherited class.
It's a way to ensure that all child classes will implement their own virtual function.
|
|
|
|
|
You may think of an abstract base class as an interface. The interface defines how inherited classes are utilized.
The base class is abstract and hence cannot be created. The same goes for interfaces: there's no point in create an interface unless there's an object behind it.
This is also how COM interfaces are defined in C++.
aloktambi wrote:
why it is needed in abstract base class.
Without a pure virtual function the class is not abstract.
|
|
|
|
|
Here's an example of something I did with an abstract base class. This class was a thread class that would start up the thread. It was its own class because that would allow derived instances of it to have their own set of properties and capabilities in addition to those supplied by the base class. The pure virtual methd of the class is the thread procedure itself. One makes their own thread objects by deriving from this base class and implementing the thread procedure method. Other methods are available to create the thread, to initialize it, and to clean up after it. Each of those mentioned are virtual to allow them to be overridden but they also have default implementations. In the case of the thread procedure method a thread is useless without one so making it a pure virtual method requires all derived class to provide an implementation of it.
Does this makes sense ?
BTW - about this thread class, normally class methods can't be used as thread procedures but these threads use a static member method as the thread procedure and its argument is the this pointer of the thread object. The static member method calls the derived class' thread procedure method.
|
|
|
|
|
cedric moonen is right...
Here Why you need Abstract class..
Consider the basic senario of having shape classes like Line, Rectangle, etc.
In the real world, there is nothing called Shape... you cannot define a Shape,, The object could be regular, irregular... But you cannot not define Shape anywhere...
Line is a Shape
Circle is a Shape
Rectangle is a Shape
But Shape is not Shape... (U might confused more..if it is i am really sorry )
But if you look at Line, Circle, Rectangle they are measarable..ie you can find area, they will have line length, width, color, style etc.. So considering these features we have, all are put in to a Shape class in order to resuse the existing implementation...
But as I told Shape is not Shape... You cannot draw a Shape.. So you need to protect the user to create Shape object...For that you need to make that class as abstract ie it must to have a pure virtual function,
Pure virtual function doesn't has implementation (In c++ context)
" Action without vision is only passing time,
Vision without action is merely day dreaming,
But vision with action can change the world "
- Words from Nelson Mandela
Thanks & Regards,
Gopalakrishnan
|
|
|
|
|
Its quiet simple
It forces the programer to define the
function in the dervied class that is a pure
virtual fun in the base class.
Vikas Amin
Embin Technology
Bombay
vikas.amin@embin.com
|
|
|
|
|
Hi,
I have an private mem as char* author_name[30];
If I want to take input from user then How should I use this in cin>> syntax?
Error:
I m using cin>>author_name[30];
It shows an unexceptional error occur.
Rupali Sarda
|
|
|
|
|
You get an exception because you declared a pointer to a pointer, it's like declaring char** .
You have no memory allocated for that pointer so cin>> will atempt to write in some random memory location.
|
|
|
|
|
To complete previous response, it will work if you remove the *:
char author_name[30];
And then:
cin>>author_name;
|
|
|
|
|
As it is a private member, u cant use like this cin>>author_name.because private member r accesible throug h their member function ,friend function,friend class.
better u create char pointer say
char input_user[30];
cin>>input_user;
say the private member is defined in a class
Class AUTHOR_NAME
{
public:
author_name(char name) //constructor
{strcpy(author_name,name);}
private: char author_name[30];//private member
};
-- modified at 2:13 Friday 23rd September, 2005
|
|
|
|
|
Rupali sarda wrote:
have an private mem as char* author_name[30];
here is small program for you.....
void main()
{
char *name[30];
for(int i=0;i<2;i++)
{
name[i]=new char[30];
cin>>name[i];
}
for(i=0;i<2;i++)
{
cout<<name[i];
delete [] name[i];
}
}
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
|
|
|
|
|
welcome simple but nice one !
V
|
|
|
|
|
Hi,
In my application i've few edit boxes ,the value in them can be edited by the user during runtime.i've set a limit for the number of integers they can enter and after the user enters the value i want to trap them in a SINGLE CLICK message handler.I dont want to use EN_CHANGE handlerfor this.will it be possible for me to do it this way?
thank you in advance.
rajeev
|
|
|
|
|
A way to do that is to derive a class from CEdit and in that class trap the WM_LBUTTONDOWN message. From the WM_LBUTTONDOWN handler notify the parent about the event and that's it. The edit boxes from your application have to be instances of your derived class.
|
|
|
|
|
Dear all,
I have created a process and wait for it to complete a certain job. e.g. "Driver Installation".
When the driver installs, it will request the user to re-start (or skip) your PC. If you choose not to re-start your PC; the main process will continue the incomplete job. On the contrary, if you choose to re-start the PC, the system will cause “Circular Wait”. Because the process informs main process whether the session is ending. In this case- how should I know if the process shows that I need to re-start the computer? If the main process has received the reboot message and then just go to leave.
SHELLEXECUTEINFO ExecuteInfoDriver;
memset(&ExecuteInfoDriver, 0, sizeof(ExecuteInfoDriver));
LPCTSTR lpctDriverPath = (LPCTSTR)DriverPath;
ExecuteInfoDriver.cbSize = sizeof(ExecuteInfoDriver);
ExecuteInfoDriver.fMask = SEE_MASK_NOCLOSEPROCESS;
ExecuteInfoDriver.hwnd = 0;
ExecuteInfoDriver.lpVerb = "open";
ExecuteInfoDriver.lpFile = lpctDriverPath;
ExecuteInfoDriver.lpParameters = NULL;
ExecuteInfoDriver.lpDirectory = 0;
ExecuteInfoDriver.nShow = SW_SHOW;
ExecuteInfoDriver.hInstApp = 0;
......
if (::ShellExecuteEx(&ExecuteInfoDriver))
{
hDriverProcess = ExecuteInfoDriver.hProcess;
if (hDriverProcess != NULL)
{
::WaitForSingleObject(hDriverProcess, INFINITE);
::CloseHandle(hDriverProcess);
}
}
....
do something...
....
Thanks for your help, I really appreciate it.
Best Regards
Steve Chang.
|
|
|
|
|
Hi ,
I was trying to compile a DirectShow sample called the MultiVMR9 from the DirectShow SDK samples,
and I'm getting the following compile error message. Does anyknow how to get around this? or what I'm doing wrong here ?
I'm using VS.NET 2003
Thanks in advance.
g:\Program Files\Microsoft Platform SDK\Samples\Multimedia\DirectShow\Samples\C++\DirectShow\VMR9\MultiVMR9\MultiPlayer\VMR9Subgraph.cpp(78): error C2065: 'CLSID_VideoMixingRenderer9' : undeclared identifier
g:\Program Files\Microsoft Platform SDK\Samples\Multimedia\DirectShow\Samples\C++\DirectShow\VMR9\MultiVMR9\MultiPlayer\MultiPlayer.cpp(55): error C2065: 'CLSID_VideoMixingRenderer9' : undeclared identifier
-Cindy
|
|
|
|
|
the Operation System is Win NT/XP
i want to write a print preview and setup program, i've fount in msdn library,
I get the DEVMODE structure,and modify the dmFormName Field to my own FormName(which have been use AddForm() to insert into to system),but why my program cant work out ,can anybody give me some tips?
|
|
|
|
|
Hello, i need help in my project based on content based image retrieval system where i will have to extract shape from sketches submitted by the user n compare it to other images...i am actually searching for code in VC++ for shape matching(fourier,grid-based descriptor,etc) n also code to detect geometric shape like circle,triangle,ect...plz help me..thx u in advance
Yesha
|
|
|
|
|
hello
what do I do if I want to print a string like this:
("how")
with fprintf
because if I do this fprintf(stream,"("how")");
it gives me compiler errors??
|
|
|
|
|
In C/C++, the backslash \ is the escape. So, you'd do this:
fprintf(stream, "(\"how\")");
|
|
|
|
|
How would i write a trim(char *str) function??
|
|
|
|
|
run through the string from the start, find the first non-whitespace. run through it from the end, find the last whitespace. the result is everything between those two points.
Cleek | Image Toolkits | Thumbnail maker
|
|
|
|
|
|
example:
output:
Enter a sentence and I will strip out all spaces.
this is a test of the emergency broadcast system
Your sentence without spaces (8) is:
this.is.a.test.of.the.emergency.broadcast.system
code:
int stripWhite(char *str)
{
int len=0; // number of spaces
char *ptr = str;
int pos = 0;
while(*ptr)
{
if (isspace(*ptr))
{
len++;
*ptr = '.';
}
ptr++; pos++;
}
return len;
}
so i want to take all spaces out of a string instead of what i do now (replace with '.' so the string is:
thisisatestoftheemergencybroadcastsystem
how?
|
|
|
|
|
use ::strcpy to copy the rest of the string from that position down one. Make sure you copy the null at the end as well.
Christian Graus - Microsoft MVP - C++
-- modified at 23:21 Thursday 22nd September, 2005
Here's a solution:
#include "stdafx.h"
#include <iostream>
using namespace std;
size_t trim(char * str)
{
size_t len = ::strlen(str);
for(size_t i =0; i < len; ++i)
{
if (::isspace(str[i]))
{
::strcpy(&str[i], &str[i+1]);
-- len;
}
}
return len;
}
int _tmain(int argc, _TCHAR* argv[])
{
char * str1 = new char[200];
strcpy(str1, " This is a test of the veracity of my system..... ");
cout << strlen(str1);
cout << trim(str1);
cout << str1;
cin >> str1;
return 0;
}
|
|
|
|