Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C Visual-Studio
Can i have an example of function declared in base class and defined in derived class.?
Posted 11-Oct-12 6:29am
Comments
Wes Aday at 11-Oct-12 12:34pm
   
http://msdn.microsoft.com/en-us/library/0y01k918(v=VS.80).aspx
aasikRaja at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

One such example is pure virtual function:-
 
#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"/>
  Permalink  
v3
Comments
Tarun Batra at 11-Oct-12 13:06pm
   
can u solve mine error?
Tarun Batra at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 470
1 Gihan Liyanage 317
2 ClimerChinna 182
3 vikinghunter 176
4 OriginalGriff 141
0 Sergey Alexandrovich Kryukov 8,653
1 OriginalGriff 7,132
2 CPallini 2,598
3 Richard MacCutchan 2,060
4 Abhinav S 1,808


Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 11 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100