Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Can i have an example of function declared in base class and defined in derived class.?
Posted
Comments
[no name] 11-Oct-12 12:34pm    
http://msdn.microsoft.com/en-us/library/0y01k918(v=VS.80).aspx
aasikRaja 12-Oct-12 0:44am    
Take a look at this code example:
class A
{
public:
int n;
public:
int Connect();
};
class B:public A // derived class from class A
{
int Connect()
{
//use ur Code here..

}
};
void main()
{
B obj;
obj.Connect();
_getch();
}

1 solution

One such example is pure virtual function:-

C++
#include <iostream>
using namespace std; 
class Exforsys 
{ 
public: 
        virtual void example()=0; //Denotes pure virtual Function Definition
}; 
 
class Exf1:public Exforsys 
{ 
public: 
        void example() 
        { 
                cout << "Welcome"; 
        } 
}; 
 
class Exf2:public Exforsys 
{ 
public: 
        void example() 
        { 
                cout << "To Training"; 
        } 
}; 
 
void main() 
{ 
        Exforsys* arra[2]; 
        Exf1 e1; 
        Exf2 e2; 
        arra[0]=&e1; 
        arra[1]=&e2; 
        arra[0]->example(); 
        arra[1]->example(); 
}



<img src="http://www.exforsys.com/images/cpp/34_2.jpg" alt="Output"/>
 
Share this answer
 
v3
Comments
Tarun Batra 11-Oct-12 13:06pm    
can u solve mine error?
Tarun Batra 11-Oct-12 13:13pm    
i did some thing like this now :
this is a.h
#define WIN32_LEAN_AND_MEAN
#include
#include
#include
#include <process.h>
#include
#include
#include
#include
using namespace std;
class IDialysisConnector
{
public :
struct addrinfo addr_;
unsigned short port_;
CString hostname_;
CRITICAL_SECTION sect_;
HANDLE threadHandle_;
HANDLE threadHandle1_;
bool connected_;

public :
SOCKET sock_;
IDialysisConnector()
{
//WSADATA wsaData;

//// Initialize Winsock
//int iResult;
//iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
//if (iResult != 0)
//{
// printf("WSAStartup failed with error: %d\n", iResult);

//}


}


public:
virtual int ConnectToMachine()=0;
};
class B:public A
{
public:
int ConnectToMachine()
{
int conResult,iResult;
struct addrinfo *result = NULL,*ptr = NULL;

u_long iMode = 0;
DWORD nTimeout = 5000; // 5 seconds
int port=22;
WSADATA wsaData;

// Initialize Winsock

iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0)
{
printf("WSAStartup failed with error: %d\n", iResult);

}

ZeroMemory( &addr_, sizeof(addr_) );
addr_.ai_family = AF_UNSPEC;
addr_.ai_socktype = SOCK_STREAM;
addr_.ai_protocol = IPPROTO_TCP;

// Resolve the server address and port
conResult = getaddrinfo("192.168.15.168", (PCSTR)22, &addr_, &result);
if ( conResult != 0 ) {
printf("getaddrinfo failed with error: %d\n", conResult);
WSACleanup();
return 1;
}

// Attempt to connect to an address until one succeeds
for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) {

// Create a SOCKET for connecting to server
sock_ = socket(ptr->ai_family, ptr->ai_socktype,
ptr->ai_protocol);
if (sock_ == INVALID_SOCKET) {
printf("socket failed with error: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
/////////////////////////////////////////////////////////////////////////////
// Set the socket I/O mode: In this case FIONBIO
// enables or disables the blocking mode for the
// socket based on the numerical value of iMode.
// If iMode = 0, blocking is enabled;
// If iMode != 0, non-blocking mode is enabled.

conResult = ioctlsocket(sock_, FIONBIO, &iMode);
if (conResult != NO_ERROR)
printf("ioctlsocket failed with error: %ld\n", conResult);
/////////////////////////////////////////////////////////////////////////////


conResult = setsockopt(sock_, SOL_SOCKET, SO_RCVTIMEO, (const char*)&nTimeout, sizeof(DWORD));
if (conResult != NO_ERROR)
{
printf("\nSetsocopt fail with error :%d\n",WSAGetLastError());

return 0;
}


// Connect to server.
conResult = connect(sock_, ptr->ai_addr, (int)ptr->ai_addrlen);
if (conResult == SOCKET_ERROR) {
closesocket(sock_);
sock_ = INVALID_SOCKET;
continue;
}
break;
}

freeaddrinfo(result);

if (sock_ == INVALID_SOCKET) {
printf("Unable to connect to server!\n");
WSACleanup();
return 1;
}
}
};
#include"a.h"
int main()
{
B *pChecking = new B;
pChecking->ConnectToMachine();
}
when i run it i get these errors:
error LNK2019: unresolved external symbol __imp__freeaddrinfo@4 referenced in function "public: virtual int __thiscall B::ConnectToMachine(void)" (?ConnectToMachine@CDiamaxConnector@@UAEHXZ)
1>Main.obj : error LNK2019: unresolved external symbol __imp__getaddrinfo@16 referenced in function "public: virtual int __thiscall B::ConnectToMachine(void)" (?ConnectToMachine@CDiamaxConnector@@UAEHXZ)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900