i have 1 class:

class class1
    {
       void Created();

       calss1()
       {
          Created();
       }
    } class1;

    void class1::Created()
    {
      cout << "hello";
    }

imagine that i don't write:

void class1::Created()
{
  cout << "hello";
}

i get an error. i try these too:

class class1
    {
       void Created()
       {
          //do nothing
       }

       calss1()
       {
          Created();

       }

    } class1;


    void class1::Created()
    {
      cout << "hello";
    }

i get error too :(
what i want is the class have the Created() function without nothing and can be changed outside of class. can anyone advice me?
(if the question is confuse, please tell me)

Created() function without nothing and can be changed outside of class

It can not be change by anything outside the class unless it is a child class of the class in which Created() is declared. Then it's called inheritence. For example you inherit certain traits from your parents, but you can not inherit traits from a neighbor who lives down the street (unless he/she is one of your parents). Same idea in c++ classes

class foo
{
public:
    virtual void Created();
};

class bar : public foo
{
public:
    virtual void Created();
};

int main()
{
     foo f;
     f.Created(); // calls Created in class bar
     f.foo::Created(); // call Created in class foo
}

thanks for correct me
1 question: the child class can have the same class name?

sorry
errors:(
the construtor must be private?

see the code:

/*heres a nice class for work with console commands, properties and events*/

/*#ifndef myheadguard1
#define myheadguard1
//insert contents of header here
#endif*/

#include <iostream>
#include <string>
#include <Windows.h>
#include <conio.h>
#include <sstream>
#include <cstddef>
#include <typeinfo>
#include <stdio.h>

#define MY_BUFSIZE 1024
//#define NewLine std::endl
#define NewLine "\n"

#define BLACK           0
#define BLUE            1
#define GREEN           2
#define CYAN            3
#define RED             4
#define MAGENTA         5
#define BROWN           6
#define LIGHTGRAY       7
#define DARKGRAY        8
#define LIGHTBLUE       9
#define LIGHTGREEN      10
#define LIGHTCYAN       11
#define LIGHTRED        12
#define LIGHTMAGENTA    13
#define YELLOW          14
#define WHITE           15

using namespace std;

struct Position
{
    int X;
    int Y;
};

struct Size
{
    int Width;
    int Height;
};

class Console
{
    private:
    char pszOldWindowTitle[MY_BUFSIZE];
    HWND ConsoleHandle;
    HDC ConsoleDC;
    bool blnVisible;
    bool ReadEnter=false; //these boolean variable is for distinguish the functions used;)

//events
    virtual void Resized(Size SizeValue){}
    virtual void BufferResize(Size SizeValue){}
    virtual void Showed();
    virtual void Hide(){}
    virtual void Cleared(){}
    virtual void Positioned(Position PositionValue){}
    virtual void CaretPositionChanged(Position PositionValue){}
    virtual void MouseClick(int Button){}
    virtual void MouseDoubleClick(int Button){}
    virtual void MouseMove(Position MousePosition){}
    virtual void MouseWheeled(){}
    virtual void MouseHWheeled(){}
    virtual void MouseDown(int Button, Position MousePosition){}
    virtual void MouseUp(int Button, Position MousePosition){}
    virtual void KeyPressed(int Key){}
    virtual void KeyDown(int Key){}
    virtual void KeyUp(int key){}
    virtual void Created(){}
    virtual void Destroyed(){}



    //properties
    DWORD GetConsoleSize(HANDLE sout, COORD& size)
    {
        CONSOLE_FONT_INFO   cfinfo;

        COORD       fsize;

        WINDOWINFO  winfo;

        if (!GetCurrentConsoleFont(sout, FALSE, &cfinfo))
        {
            return (GetLastError());
        }

        fsize = GetConsoleFontSize(sout, cfinfo.nFont);
        if (!fsize.X && !fsize.Y)
        {
            return (GetLastError());
        }

        winfo.cbSize = sizeof(WINDOWINFO);
        if (!GetWindowInfo(GetConsoleWindow(), &winfo))
        {
            return (GetLastError());
        }

        size.Y = (SHORT)((winfo.rcClient.bottom - winfo.rcClient.top) / fsize.Y);
        size.X = (SHORT)((winfo.rcClient.right - winfo.rcClient.left) / fsize.X);

        return (0);
    }

    bool blCaretVisible;



public:
    //initializate
    Console ()
    {
        COORD    consize;
        HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
        GetConsoleSize(hcon, consize);
        SetConsoleScreenBufferSize(hcon, consize);
        Created();
        SetVisible(true);
    }
    ~Console()
    {
        Destroyed();
    }



    //HDC and Handle and Hwnd
    HDC hdc()
    {
        return GetDC(GetConsoleWindow());
    }

    HWND hwnd()
    {
        return GetConsoleWindow();
    }

    HANDLE handle()
    {
        return GetStdHandle(STD_OUTPUT_HANDLE);
    }

    //Console title
    string ConsoleTitle()
    {
        GetConsoleTitle( pszOldWindowTitle,MY_BUFSIZE);
        return string(pszOldWindowTitle);
    }

    void ConsoleTitle(string title)
    {
        SetConsoleTitle(title.c_str());
    }


    //Console Position
    POINT GetConsolePosition()
    {
        POINT a;
        RECT WindowRect;
        GetWindowRect(GetConsoleWindow() ,&WindowRect);
        a.x=WindowRect.left;
        a.y=WindowRect.top;
        return a;
    }

    void SetConsolePosition(POINT Position )
    {
        SetWindowPos(GetConsoleWindow(),HWND_TOP,Position.x,Position.y,0,0,SWP_SHOWWINDOW||SWP_NOOWNERZORDER);
    }

    //write
    void write()
    {
        cout <<"";
    }

    template <typename A, typename ...B>
    void write(A argHead, B... argTail)
    {
        cout << argHead;
        write(argTail...);
    }

    //Read
    //empty

    void read()
    {
        if (ReadEnter==false)
        {
            while(kbhit())
                    ;
            HANDLE hConIn = CreateFileW(L"CONIN$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr);

            DWORD oldMode;
            GetConsoleMode(hConIn, &oldMode);
            SetConsoleMode(hConIn, ENABLE_LINE_INPUT);

            FlushConsoleInputBuffer(hConIn);

            char buffer[1];
            DWORD read;
            ReadConsoleA(hConIn, buffer, sizeof(buffer), &read, nullptr);

            SetConsoleMode(hConIn, oldMode);
            CloseHandle(hConIn);
        }
    }

    template<typename ...B>
    void read(string &s, B&... tail)
    {
        ReadEnter=true;
        while(kbhit())
            ;
        getline(cin, s);
        read(tail...);
        ReadEnter=false;
    }

    template<typename ...B>
    void read(char s[256], B&... tail)
    {
        ReadEnter=true;
        while(kbhit())
            ;
        cin.getline(s,256);
        read(tail...);
        ReadEnter=false;
    }

    template <typename A, typename ...B>
    void read(A &argHead, B&... argTail)
    {
        ReadEnter=true;
        while(kbhit())
        cin >> argHead;
        read(argTail...);
        ReadEnter=false;
    }

    //clear the Console
    void Clear(int BackColor=0)
    {
         // home for the cursor
        COORD coordScreen = {0, 0};
        DWORD cCharsWritten;
        CONSOLE_SCREEN_BUFFER_INFO csbi;
        DWORD dwConSize;

        // Get the number of character cells in the current buffer
        if(!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
            return;
        dwConSize = csbi.dwSize.X * csbi.dwSize.Y;

        // Fill the entire screen with blanks
        if(!FillConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), (WCHAR)' ', dwConSize, coordScreen, &cCharsWritten))
            return;
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15|BackColor<<4 );
        // Get the current text attribute.
        if(!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
            return;

        // Set the buffer's attributes accordingly.
        if(!FillConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten))
            return;

        // Put the cursor at its home coordinates.
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coordScreen);
    }

    //Turn cursor on
    DWORD CursorOn(HANDLE sout)
    {
        CONSOLE_CURSOR_INFO cur;

        if (!GetConsoleCursorInfo(sout, &cur))
        {
            return (GetLastError());
        }

        cur.bVisible = TRUE;
        return (!SetConsoleCursorInfo(sout, &cur) ? GetLastError() : 0);
    }


    //Turn cursor off
    DWORD CursorOff(HANDLE sout)
    {
        CONSOLE_CURSOR_INFO cur;

        if (!GetConsoleCursorInfo(sout, &cur))
        {
            return (GetLastError());
        }

        cur.bVisible = FALSE;
        return (!SetConsoleCursorInfo(sout, &cur) ? GetLastError() : 0);
    }
    bool GetCaretVisible()
    {
        return blCaretVisible;
    }

    void SetCaretVisible(bool Visible)
    {
        if (Visible==true)
        {
            CursorOn(handle());
        }
        else
        {
            CursorOff(handle());
        }
        blCaretVisible=Visible;
    }


    void SetColorAndBackground(int ForgC, int BackC=0)
    {
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), ForgC|(BackC<<4) );
    }

    void SetConsoleIcon(char *strIconPath)
    {
        HANDLE lIcon;
        lIcon = LoadImage(0,strIconPath , 1, 0, 0, LR_LOADFROMFILE);
        SendMessage(GetConsoleWindow()  , WM_SETICON, 0,(LPARAM) lIcon) ;
    }

    COORD GetCaretPosition()
    {
        COORD s;
        CONSOLE_SCREEN_BUFFER_INFO csbi;

        GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
        s.X=csbi.dwCursorPosition.X;
        s.Y=csbi.dwCursorPosition.Y;
        return s;
    }

    void SetCaretPosition(COORD Position)
    {
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),Position);
    }


    void TimerBlink(string Text, short x, short y, int ForgC, int BackC, int milliseconds)
    {
        COORD   pos = {x,y};
        COORD CaretPos;

        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), ForgC|(BackC<<4) );
        CaretPos=GetCaretPosition();
        SetCaretPos(x,y);
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
        cout << Text;
        SetCaretPos(CaretPos.X,CaretPos.Y);
        Sleep(milliseconds);
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BLACK|(BLACK<<4) );
        CaretPos=GetCaretPosition();
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
        cout <<Text;
        SetCaretPos(CaretPos.X,CaretPos.Y);
        Sleep(milliseconds);
    }

    /*void BlinkText(string Text, int x, int y, int ForgC, int BackC, int milliseconds)
    {
        COORD a;
        a=GetCaretPosition();

    }*/

    bool GetVisible()
    {
        return blnVisible;
    }

    void SetVisible(bool visible)
    {
        if (visible== true)
        {
            ShowWindow(GetConsoleWindow(),SW_SHOWNORMAL);
            Showed();
        }
        else
        {
            ShowWindow(GetConsoleWindow(),SW_HIDE);
            Hide();
        }
        blnVisible=visible;
    }


    COORD GetConsoleWindowSize()
    {
        COORD    consize;
        HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
        GetConsoleSize(hcon, consize);
        //SetConsoleScreenBufferSize(hcon, consize);
        return consize;
    }

    void SetConsoleWindowSize(COORD Size)
    {
        SMALL_RECT s;
        POINT WindowPos;
        CONSOLE_SCREEN_BUFFER_INFO csbi;
        HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
        WindowPos=GetConsolePosition() ;
        s.Left=(SHORT)WindowPos.x;
        s.Top=(SHORT)WindowPos.y;
        s.Bottom=(SHORT)WindowPos.y+Size.Y;
        s.Right= (SHORT)WindowPos.x+Size.X;

        GetConsoleScreenBufferInfo( hcon, &csbi);
        if  (Size.X > (csbi.srWindow.Bottom- csbi.srWindow.Top) || Size.Y >   (csbi.srWindow.Right- csbi.srWindow.Left))
        {
            SetConsoleScreenBufferSize(hcon, Size);
        }
        SetConsoleWindowInfo(hcon,true,&s);
    }

};


//in main.cpp
#include <iostream>
#include "console.h"

using namespace std;
class console: public Console
{
    public:
    virtual void Created()
{
    cout << std::endl << "Console Created" << std::endl;
}
   virtual void  Showed()
{
    cout << std::endl << "i'm showed" << std::endl;
}

  virtual void  Hide()
{
    cout << std::endl << "i'm hided" << std::endl;
}

 virtual void  Destroyed()
{
    cout << std::endl << "Console Destroyed" << std::endl;
}
};


int main()
{
    console a;
    a.write("oi", NewLine);
    return 0;
}

error message:
"C:\Users\Joaquim\Documents\CodeBlocks\My Class\console.h|123|undefined reference to `vtable for Console'|"
on contrutor of Console

what compiler are you using? I get several errors from VC++ 2012, for example on line 59, can not initialize within class declaration. Check your compiler errors and fix them.

finally i put it to work. but i don't understand 1 thing:
if the console have directive of Console, why Console construtor isn't executed?

what makes you think it isn't executed? Did you use a debugger or put a print statement inside the constructure to see?

finally i resolve it:

..............
public:
    virtual void created(){}
    virtual void print(){}
    //initializate
    Console ()
    {
        COORD    consize;
        HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
        GetConsoleSize(hcon, consize);
        SetConsoleScreenBufferSize(hcon, consize);
        created();
        SetVisible(true);
    }
    .................

    //write
    void write()
    {
        cout <<"";
        print();
    }
    ................

and the child class:

class console: public Console
{
    public:
    //initializate
    console(): Console()
    {
        //o que quiseres fazer aqui, provavelmente nada.
        created();
    }
    virtual void created()
    {
        cout << "hello";
    }
     virtual void print()
    {
        cout << "printed";
    }

};

like you see i must call the virtual function in construtor class. because the other virtual function works fine.
1 question:
why i must use virtual void print(){}(empty function) for call it?

You should remove virtual keyword for functions that are not inhereted from parent and from the function in parent that you do not intend to overload in derived class.

i understand what you mean. i did that. but let me ask you something:
why i must use 'virtual void print(){}' instead 'virtual void print();'?
if i use the 2nd, the compiler give me an error on Console construtor.

virtual void printf(); means you are going to implement the function in the *.cpp file instead of inlining in the class.

virtual void printf() {} is a do-nothing function, the function name exists but does nothing because there isn't anything betweein the two braces { and }.

thanks for all. thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.