Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++CVisual-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 &lt;process.h&gt; #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-&gt;ai_next) { // Create a SOCKET for connecting to server sock_ = socket(ptr-&gt;ai_family, ptr-&gt;ai_socktype, ptr-&gt;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-&gt;ai_addr, (int)ptr-&gt;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-&gt;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&gt;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 Dnyaneshwar@Pune 1,300
1 Sergey Alexandrovich Kryukov 660
2 ravikhoda 300
3 OriginalGriff 299
4 Manas Bhardwaj 240
0 Sergey Alexandrovich Kryukov 11,194
1 OriginalGriff 6,867
2 Peter Leow 4,795
3 Abhinav S 3,838
4 Maciej Los 3,575


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